invalidate.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 tokens
  15. import (
  16. "context"
  17. "database/sql"
  18. "net/http"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/rbacscope"
  22. api "yunion.io/x/onecloud/pkg/apis/identity"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/keystone/models"
  27. )
  28. // swagger:route DELETE /v3/auth/tokens authentication invalidateTokensV3
  29. //
  30. // keystone v3删除token API
  31. //
  32. // keystone v3删除token API
  33. func invalidateTokenV3(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  34. tokenStr := r.Header.Get(api.AUTH_SUBJECT_TOKEN_HEADER)
  35. err := invalidateToken(ctx, tokenStr)
  36. if err != nil {
  37. httperrors.GeneralServerError(ctx, w, err)
  38. return
  39. }
  40. appsrv.SendNoContent(w)
  41. }
  42. func invalidateToken(ctx context.Context, tokenStr string) error {
  43. adminToken := policy.FetchUserCredential(ctx)
  44. if adminToken == nil || len(tokenStr) == 0 {
  45. return httperrors.NewForbiddenError("missing auth token")
  46. }
  47. token, err := TokenStrDecode(ctx, tokenStr)
  48. if err != nil {
  49. return httperrors.NewInvalidCredentialError("invalid token: %v", err)
  50. }
  51. if adminToken.GetUserId() != token.UserId && policy.PolicyManager.Allow(rbacscope.ScopeSystem, adminToken, api.SERVICE_TYPE, "tokens", "delete").Result.IsDeny() {
  52. return httperrors.NewForbiddenError("%s not allow to delete token", adminToken.GetUserName())
  53. }
  54. err = models.TokenCacheManager.Invalidate(ctx, adminToken, tokenStr)
  55. if err != nil {
  56. if errors.Cause(err) == sql.ErrNoRows {
  57. return errors.Wrap(ErrTokenNotFound, tokenStr)
  58. } else {
  59. return errors.Wrap(err, "Invalidate")
  60. }
  61. }
  62. return nil
  63. }
  64. // swagger:route GET /v3/auth/tokens/invalid authentication fetchInvalidTokensV3
  65. //
  66. // keystone v3获取被删除的token的列表API
  67. //
  68. // keystone v3获取被删除的token的列表API
  69. func fetchInvalidTokensV3(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  70. tokens, err := fetchInvalidTokens(ctx)
  71. if err != nil {
  72. httperrors.GeneralServerError(ctx, w, err)
  73. return
  74. }
  75. resp := jsonutils.NewDict()
  76. resp.Add(jsonutils.NewStringArray(tokens), "tokens")
  77. appsrv.SendJSON(w, resp)
  78. }
  79. func fetchInvalidTokens(ctx context.Context) ([]string, error) {
  80. adminToken := policy.FetchUserCredential(ctx)
  81. if adminToken == nil {
  82. return nil, httperrors.NewForbiddenError("missing auth token")
  83. }
  84. if policy.PolicyManager.Allow(rbacscope.ScopeSystem, adminToken, api.SERVICE_TYPE, "tokens", "list", "invalid").Result.IsDeny() {
  85. return nil, httperrors.NewForbiddenError("%s not allow to list invalid tokens", adminToken.GetUserName())
  86. }
  87. tokens, err := models.TokenCacheManager.FetchInvalidTokens()
  88. if err != nil {
  89. return nil, errors.Wrap(err, "TokenCacheManager.FetchInvalidTokens")
  90. }
  91. return tokens, nil
  92. }