middleware.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 auth
  15. import (
  16. "context"
  17. "net/http"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/appctx"
  20. api "yunion.io/x/onecloud/pkg/apis/identity"
  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/util/rbacutils"
  25. )
  26. var (
  27. GUEST_USER = "guest"
  28. GUEST_TOKEN = rbacutils.GUEST_TOKEN // "guest_token"
  29. GuestToken = mcclient.SSimpleToken{
  30. User: GUEST_USER,
  31. Token: GUEST_TOKEN,
  32. }
  33. DefaultTokenVerifier = Verify
  34. )
  35. const (
  36. AUTH_TOKEN = appctx.APP_CONTEXT_KEY_AUTH_TOKEN
  37. )
  38. type TokenVerifyFunc func(string) (mcclient.TokenCredential, error)
  39. func Authenticate(f appsrv.FilterHandler) appsrv.FilterHandler {
  40. return AuthenticateWithDelayDecision(f, false)
  41. }
  42. func AuthenticateWithDelayDecision(f appsrv.FilterHandler, delayDecision bool) appsrv.FilterHandler {
  43. return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  44. tokenStr := r.Header.Get(api.AUTH_TOKEN_HEADER)
  45. var token mcclient.TokenCredential
  46. if len(tokenStr) == 0 {
  47. log.Errorf("no auth_token found! delayDecision=%v", delayDecision)
  48. if !delayDecision {
  49. httperrors.UnauthorizedError(ctx, w, "Unauthorized")
  50. return
  51. }
  52. token = &GuestToken
  53. } else {
  54. var err error
  55. token, err = DefaultTokenVerifier(ctx, tokenStr)
  56. if err != nil {
  57. log.Errorf("Verify token failed: %s", err)
  58. if !delayDecision {
  59. httperrors.UnauthorizedError(ctx, w, "InvalidToken")
  60. return
  61. }
  62. token = &GuestToken
  63. }
  64. }
  65. ctx = context.WithValue(ctx, appctx.APP_CONTEXT_KEY_AUTH_TOKEN, token)
  66. if taskId := r.Header.Get(mcclient.TASK_ID); taskId != "" {
  67. ctx = context.WithValue(ctx, appctx.APP_CONTEXT_KEY_TASK_ID, taskId)
  68. }
  69. if taskNotifyUrl := r.Header.Get(mcclient.TASK_NOTIFY_URL); taskNotifyUrl != "" {
  70. ctx = context.WithValue(ctx, appctx.APP_CONTEXT_KEY_TASK_NOTIFY_URL, taskNotifyUrl)
  71. }
  72. f(ctx, w, r)
  73. }
  74. }
  75. func FetchUserCredential(ctx context.Context, filter func(mcclient.TokenCredential) mcclient.TokenCredential) mcclient.TokenCredential {
  76. tokenValue := ctx.Value(appctx.APP_CONTEXT_KEY_AUTH_TOKEN)
  77. if tokenValue != nil {
  78. token := tokenValue.(mcclient.TokenCredential)
  79. if filter != nil {
  80. token = filter(token)
  81. }
  82. return token
  83. }
  84. return nil
  85. }
  86. func IsGuestToken(userCred rbacutils.IRbacIdentity) bool {
  87. return userCred.GetTokenString() == GUEST_TOKEN
  88. }