token.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 mcclient
  15. import (
  16. "time"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/gotypes"
  19. "yunion.io/x/pkg/util/rbacscope"
  20. )
  21. type ExternalService struct {
  22. Name string `json:"name"`
  23. Url string `json:"url"`
  24. Service string `json:"service"`
  25. }
  26. type Endpoint struct {
  27. Id string `json:"id"`
  28. RegionId string `json:"region_id"`
  29. ServiceId string `json:"service_id"`
  30. ServiceName string `json:"service_name"`
  31. Url string `json:"url"`
  32. Interface string `json:"interface"`
  33. }
  34. func OwnerIdString(owner IIdentityProvider, scope rbacscope.TRbacScope) string {
  35. switch scope {
  36. case rbacscope.ScopeDomain:
  37. return owner.GetProjectDomainId()
  38. case rbacscope.ScopeProject:
  39. return owner.GetProjectId()
  40. case rbacscope.ScopeUser:
  41. return owner.GetUserId()
  42. default:
  43. return ""
  44. }
  45. }
  46. // interface for owner
  47. type IIdentityProvider interface {
  48. GetProjectId() string
  49. GetUserId() string
  50. GetTenantId() string
  51. GetProjectDomainId() string
  52. GetTenantName() string
  53. GetProjectName() string
  54. GetProjectDomain() string
  55. GetUserName() string
  56. GetDomainId() string
  57. GetDomainName() string
  58. }
  59. // interface for identity of user with project and roles
  60. type IUserIdentity interface {
  61. IIdentityProvider
  62. GetRoleIds() []string
  63. GetRoles() []string
  64. }
  65. // interface for full keystone token
  66. type TokenCredential interface {
  67. gotypes.ISerializable
  68. IServiceCatalog
  69. IUserIdentity
  70. GetTokenString() string
  71. GetRoles() []string
  72. // GetRoleIds() []string
  73. GetExpires() time.Time
  74. IsValid() bool
  75. ValidDuration() time.Duration
  76. // IsAdmin() bool
  77. HasSystemAdminPrivilege() bool
  78. // IsAllow(scope rbacscope.TRbacScope, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult
  79. GetRegions() []string
  80. GetServiceCatalog() IServiceCatalog
  81. GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject
  82. GetEndpoints(region string, endpointType string) []Endpoint
  83. ToJson() jsonutils.JSONObject
  84. GetLoginSource() string
  85. GetLoginIp() string
  86. IsSystemAccount() bool
  87. }