tokensimple.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. "fmt"
  17. "reflect"
  18. "strings"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/gotypes"
  22. api "yunion.io/x/onecloud/pkg/apis/identity"
  23. )
  24. type SSimpleToken struct {
  25. Token string
  26. Domain string
  27. DomainId string
  28. User string
  29. UserId string
  30. Project string `json:"tenant"`
  31. ProjectId string `json:"tenant_id"`
  32. ProjectDomain string
  33. ProjectDomainId string
  34. Roles string
  35. RoleIds string
  36. Expires time.Time
  37. SystemAccount bool
  38. Context SAuthContext
  39. }
  40. func (self *SSimpleToken) GetTokenString() string {
  41. return self.Token
  42. }
  43. func (self *SSimpleToken) GetDomainId() string {
  44. return self.DomainId
  45. }
  46. func (self *SSimpleToken) GetDomainName() string {
  47. return self.Domain
  48. }
  49. func (self *SSimpleToken) GetTenantId() string {
  50. return self.ProjectId
  51. }
  52. func (self *SSimpleToken) GetTenantName() string {
  53. return self.Project
  54. }
  55. func (self *SSimpleToken) GetProjectId() string {
  56. return self.ProjectId
  57. }
  58. func (self *SSimpleToken) GetProjectName() string {
  59. return self.Project
  60. }
  61. func (self *SSimpleToken) GetProjectDomainId() string {
  62. if len(self.ProjectDomainId) == 0 {
  63. return api.DEFAULT_DOMAIN_ID
  64. }
  65. return self.ProjectDomainId
  66. }
  67. func (self *SSimpleToken) GetProjectDomain() string {
  68. if len(self.ProjectDomain) == 0 {
  69. return api.DEFAULT_DOMAIN_NAME
  70. }
  71. return self.ProjectDomain
  72. }
  73. func (self *SSimpleToken) GetUserId() string {
  74. return self.UserId
  75. }
  76. func (self *SSimpleToken) GetUserName() string {
  77. return self.User
  78. }
  79. func (self *SSimpleToken) GetRoles() []string {
  80. return strings.Split(self.Roles, ",")
  81. }
  82. func (self *SSimpleToken) GetRoleIds() []string {
  83. return strings.Split(self.RoleIds, ",")
  84. }
  85. func (self *SSimpleToken) GetExpires() time.Time {
  86. return self.Expires
  87. }
  88. func (self *SSimpleToken) IsValid() bool {
  89. return self.ValidDuration() > 0
  90. }
  91. func (self *SSimpleToken) ValidDuration() time.Duration {
  92. return time.Until(self.Expires)
  93. }
  94. func (self *SSimpleToken) IsAdmin() bool {
  95. roles := self.GetRoles()
  96. for i := 0; i < len(roles); i++ {
  97. if roles[i] == "admin" {
  98. return true
  99. }
  100. }
  101. return false
  102. }
  103. func (self *SSimpleToken) HasSystemAdminPrivilege() bool {
  104. return self.IsAdmin() && self.Project == "system"
  105. }
  106. /*func (this *SSimpleToken) IsAllow(scope rbacscope.TRbacScope, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult {
  107. if this.isAllow(scope, service, resource, action, extra...) {
  108. return rbacutils.PolicyAllow
  109. } else {
  110. return rbacutils.PolicyDeny
  111. }
  112. }
  113. func (this *SSimpleToken) isAllow(scope rbacscope.TRbacScope, service string, resource string, action string, extra ...string) bool {
  114. if scope == rbacscope.ScopeSystem || scope == rbacscope.ScopeDomain {
  115. return this.HasSystemAdminPrivilege()
  116. } else {
  117. return true
  118. }
  119. }*/
  120. func (self *SSimpleToken) GetRegions() []string {
  121. return nil
  122. }
  123. func (self *SSimpleToken) Len() int {
  124. return 0
  125. }
  126. func (self *SSimpleToken) getServiceURL(service, region, zone, endpointType string) (string, error) {
  127. return "", fmt.Errorf("Not available")
  128. }
  129. func (self *SSimpleToken) getServiceURLs(service, region, zone, endpointType string) ([]string, error) {
  130. return nil, fmt.Errorf("Not available")
  131. }
  132. func (this *SSimpleToken) GetServicesByInterface(region string, infType string) []ExternalService {
  133. return nil
  134. }
  135. func (self *SSimpleToken) GetInternalServices(region string) []string {
  136. return nil
  137. }
  138. func (self *SSimpleToken) GetExternalServices(region string) []ExternalService {
  139. return nil
  140. }
  141. func (this *SSimpleToken) GetEndpoints(region string, endpointType string) []Endpoint {
  142. return nil
  143. }
  144. func (this *SSimpleToken) GetServiceCatalog() IServiceCatalog {
  145. return nil
  146. }
  147. func (this *SSimpleToken) IsSystemAccount() bool {
  148. return this.SystemAccount
  149. }
  150. func (this *SSimpleToken) GetLoginSource() string {
  151. return this.Context.Source
  152. }
  153. func (this *SSimpleToken) GetLoginIp() string {
  154. return this.Context.Ip
  155. }
  156. func SimplifyToken(token TokenCredential) TokenCredential {
  157. simToken, ok := token.(*SSimpleToken)
  158. if ok {
  159. return simToken
  160. }
  161. return &SSimpleToken{Token: token.GetTokenString(),
  162. Domain: token.GetDomainName(),
  163. DomainId: token.GetDomainId(),
  164. User: token.GetUserName(),
  165. UserId: token.GetUserId(),
  166. Project: token.GetProjectName(),
  167. ProjectId: token.GetProjectId(),
  168. ProjectDomain: token.GetProjectDomain(),
  169. ProjectDomainId: token.GetProjectDomainId(),
  170. Roles: strings.Join(token.GetRoles(), ","),
  171. RoleIds: strings.Join(token.GetRoleIds(), ","),
  172. Expires: token.GetExpires(),
  173. Context: SAuthContext{
  174. Source: token.GetLoginSource(),
  175. Ip: token.GetLoginIp(),
  176. },
  177. SystemAccount: token.IsSystemAccount(),
  178. }
  179. }
  180. func (self *SSimpleToken) GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject {
  181. return nil
  182. }
  183. func (self *SSimpleToken) String() string {
  184. return self.ToJson().String()
  185. }
  186. func (self *SSimpleToken) IsZero() bool {
  187. return len(self.UserId) == 0 && len(self.ProjectId) == 0
  188. }
  189. func (self *SSimpleToken) ToJson() jsonutils.JSONObject {
  190. return jsonutils.Marshal(self)
  191. }
  192. var TokenCredentialType reflect.Type
  193. func init() {
  194. TokenCredentialType = reflect.TypeOf((*TokenCredential)(nil)).Elem()
  195. gotypes.RegisterSerializable(TokenCredentialType, func() gotypes.ISerializable {
  196. return &SSimpleToken{}
  197. })
  198. }