assume.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package handler
  15. import (
  16. "context"
  17. "net/http"
  18. "strings"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/apigateway/clientman"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. "yunion.io/x/onecloud/pkg/httperrors"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/util/netutils2"
  26. )
  27. const (
  28. ASSUME_TOKEN_HEADER = "X-Assume-Token"
  29. )
  30. type SAssumeToken struct {
  31. Token string `json:"token"`
  32. UserId string `json:"user_id"`
  33. ProjectId string `json:"project_id"`
  34. }
  35. func getAssumeToken(r *http.Request) (*SAssumeToken, error) {
  36. auth := r.Header.Get(ASSUME_TOKEN_HEADER)
  37. if auth == "" {
  38. return nil, errors.Wrap(httperrors.ErrInputParameter, "Assume token header is empty")
  39. }
  40. parts := strings.Split(strings.TrimSpace(auth), ":")
  41. if len(parts) != 3 {
  42. return nil, errors.Wrap(httperrors.ErrInputParameter, "Assume token header is invalid")
  43. }
  44. return &SAssumeToken{
  45. Token: parts[0],
  46. UserId: parts[1],
  47. ProjectId: parts[2],
  48. }, nil
  49. }
  50. func doAssumeLogin(assumeToken *SAssumeToken, w http.ResponseWriter, req *http.Request) (mcclient.TokenCredential, error) {
  51. cliIp := netutils2.GetHttpRequestIp(req)
  52. token, err := auth.Client().AuthenticateAssume(assumeToken.Token, assumeToken.UserId, assumeToken.ProjectId, cliIp)
  53. if err != nil {
  54. return nil, errors.Wrapf(httperrors.ErrInvalidCredential, "AuthenticateAssume fail %s", err)
  55. }
  56. authToken := clientman.NewAuthToken(token.GetTokenString(), false, false, false)
  57. saveLoginCookies(w, authToken, token, nil)
  58. return token, nil
  59. }
  60. func (h *AuthHandlers) handleAssumeLogin(ctx context.Context, w http.ResponseWriter, req *http.Request) {
  61. // no auth cookie, try to get get assume user token from header
  62. assumeToken, err := getAssumeToken(req)
  63. if err != nil {
  64. appsrv.Send(w, err.Error())
  65. return
  66. }
  67. token, err := doAssumeLogin(assumeToken, w, req)
  68. if err != nil {
  69. appsrv.Send(w, err.Error())
  70. return
  71. }
  72. _, query, _ := appsrv.FetchEnv(ctx, w, req)
  73. redirect := ""
  74. if query != nil {
  75. queryMap, err := query.GetMap()
  76. if err != nil {
  77. appsrv.Send(w, err.Error())
  78. return
  79. }
  80. for k, v := range queryMap {
  81. if k == "p" {
  82. redirect, _ = v.GetString()
  83. } else {
  84. value, _ := v.GetString()
  85. saveCookie(w, k, value, "", token.GetExpires(), false)
  86. }
  87. }
  88. }
  89. if !strings.HasPrefix(redirect, "/") {
  90. redirect = "/" + redirect
  91. }
  92. appsrv.SendRedirect(w, redirect)
  93. }