user_mgmt.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. jwt "github.com/golang-jwt/jwt/v4"
  6. )
  7. type NetworkID string
  8. type RsrcType string
  9. type RsrcID string
  10. type UserRoleID string
  11. type UserGroupID string
  12. type AuthType string
  13. type TokenType string
  14. var (
  15. BasicAuth AuthType = "basic_auth"
  16. OAuth AuthType = "oauth"
  17. )
  18. func (r RsrcType) String() string {
  19. return string(r)
  20. }
  21. func (rid RsrcID) String() string {
  22. return string(rid)
  23. }
  24. func GetRAGRoleName(netID, hostName string) string {
  25. return fmt.Sprintf("netID-%s-rag-%s", netID, hostName)
  26. }
  27. func GetRAGRoleID(netID, hostID string) UserRoleID {
  28. return UserRoleID(fmt.Sprintf("netID-%s-rag-%s", netID, hostID))
  29. }
  30. func (t TokenType) String() string {
  31. return string(t)
  32. }
  33. var (
  34. UserIDTokenType TokenType = "user_id_token"
  35. AccessTokenType TokenType = "access_token"
  36. )
  37. var RsrcTypeMap = map[RsrcType]struct{}{
  38. HostRsrc: {},
  39. RelayRsrc: {},
  40. RemoteAccessGwRsrc: {},
  41. ExtClientsRsrc: {},
  42. InetGwRsrc: {},
  43. EgressGwRsrc: {},
  44. NetworkRsrc: {},
  45. EnrollmentKeysRsrc: {},
  46. UserRsrc: {},
  47. AclRsrc: {},
  48. DnsRsrc: {},
  49. FailOverRsrc: {},
  50. }
  51. const AllNetworks NetworkID = "all_networks"
  52. const (
  53. HostRsrc RsrcType = "hosts"
  54. RelayRsrc RsrcType = "relays"
  55. RemoteAccessGwRsrc RsrcType = "remote_access_gw"
  56. GatewayRsrc RsrcType = "gateways"
  57. ExtClientsRsrc RsrcType = "extclients"
  58. InetGwRsrc RsrcType = "inet_gw"
  59. EgressGwRsrc RsrcType = "egress"
  60. NetworkRsrc RsrcType = "networks"
  61. EnrollmentKeysRsrc RsrcType = "enrollment_key"
  62. UserRsrc RsrcType = "users"
  63. AclRsrc RsrcType = "acl"
  64. TagRsrc RsrcType = "tag"
  65. DnsRsrc RsrcType = "dns"
  66. FailOverRsrc RsrcType = "fail_over"
  67. MetricRsrc RsrcType = "metrics"
  68. )
  69. const (
  70. AllHostRsrcID RsrcID = "all_host"
  71. AllRelayRsrcID RsrcID = "all_relay"
  72. AllRemoteAccessGwRsrcID RsrcID = "all_remote_access_gw"
  73. AllExtClientsRsrcID RsrcID = "all_extclients"
  74. AllInetGwRsrcID RsrcID = "all_inet_gw"
  75. AllEgressGwRsrcID RsrcID = "all_egress"
  76. AllNetworkRsrcID RsrcID = "all_network"
  77. AllEnrollmentKeysRsrcID RsrcID = "all_enrollment_key"
  78. AllUserRsrcID RsrcID = "all_user"
  79. AllDnsRsrcID RsrcID = "all_dns"
  80. AllFailOverRsrcID RsrcID = "all_fail_over"
  81. AllAclsRsrcID RsrcID = "all_acl"
  82. AllTagsRsrcID RsrcID = "all_tag"
  83. )
  84. // Pre-Defined User Roles
  85. const (
  86. SuperAdminRole UserRoleID = "super-admin"
  87. AdminRole UserRoleID = "admin"
  88. ServiceUser UserRoleID = "service-user"
  89. PlatformUser UserRoleID = "platform-user"
  90. NetworkAdmin UserRoleID = "network-admin"
  91. NetworkUser UserRoleID = "network-user"
  92. )
  93. func (r UserRoleID) String() string {
  94. return string(r)
  95. }
  96. func (g UserGroupID) String() string {
  97. return string(g)
  98. }
  99. func (n NetworkID) String() string {
  100. return string(n)
  101. }
  102. type RsrcPermissionScope struct {
  103. Create bool `json:"create"`
  104. Read bool `json:"read"`
  105. Update bool `json:"update"`
  106. Delete bool `json:"delete"`
  107. VPNaccess bool `json:"vpn_access"`
  108. SelfOnly bool `json:"self_only"`
  109. }
  110. type UserRolePermissionTemplate struct {
  111. ID UserRoleID `json:"id"`
  112. Name string `json:"name"`
  113. Default bool `json:"default"`
  114. MetaData string `json:"meta_data"`
  115. DenyDashboardAccess bool `json:"deny_dashboard_access"`
  116. FullAccess bool `json:"full_access"`
  117. NetworkID NetworkID `json:"network_id"`
  118. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  119. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  120. }
  121. type CreateGroupReq struct {
  122. Group UserGroup `json:"user_group"`
  123. Members []string `json:"members"`
  124. }
  125. type UserGroup struct {
  126. ID UserGroupID `json:"id"`
  127. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  128. Default bool `json:"default"`
  129. Name string `json:"name"`
  130. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  131. MetaData string `json:"meta_data"`
  132. }
  133. // User struct - struct for Users
  134. type User struct {
  135. UserName string `json:"username" bson:"username" validate:"min=3,in_charset|email"`
  136. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  137. IsMFAEnabled bool `json:"is_mfa_enabled"`
  138. TOTPSecret string `json:"totp_secret"`
  139. DisplayName string `json:"display_name"`
  140. AccountDisabled bool `json:"account_disabled"`
  141. Password string `json:"password" bson:"password" validate:"required,min=5"`
  142. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  143. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  144. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  145. AuthType AuthType `json:"auth_type"`
  146. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  147. PlatformRoleID UserRoleID `json:"platform_role_id"`
  148. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  149. LastLoginTime time.Time `json:"last_login_time"`
  150. }
  151. type ReturnUserWithRolesAndGroups struct {
  152. ReturnUser
  153. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  154. UserGroups map[UserGroupID]UserGroup `json:"user_group_ids"`
  155. }
  156. // ReturnUser - return user struct
  157. type ReturnUser struct {
  158. UserName string `json:"username"`
  159. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  160. IsMFAEnabled bool `json:"is_mfa_enabled"`
  161. DisplayName string `json:"display_name"`
  162. AccountDisabled bool `json:"account_disabled"`
  163. IsAdmin bool `json:"isadmin"`
  164. IsSuperAdmin bool `json:"issuperadmin"`
  165. AuthType AuthType `json:"auth_type"`
  166. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  167. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  168. PlatformRoleID UserRoleID `json:"platform_role_id"`
  169. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  170. LastLoginTime time.Time `json:"last_login_time"`
  171. NumAccessTokens int `json:"num_access_tokens"`
  172. }
  173. // UserAuthParams - user auth params struct
  174. type UserAuthParams struct {
  175. UserName string `json:"username"`
  176. Password string `json:"password"`
  177. }
  178. // UserIdentityValidationRequest - user identity validation request struct
  179. type UserIdentityValidationRequest struct {
  180. Password string `json:"password"`
  181. }
  182. // UserIdentityValidationResponse - user identity validation response struct
  183. type UserIdentityValidationResponse struct {
  184. IdentityValidated bool `json:"identity_validated"`
  185. }
  186. type UserTOTPVerificationParams struct {
  187. OTPAuthURL string `json:"otp_auth_url"`
  188. OTPAuthURLSignature string `json:"otp_auth_url_signature"`
  189. TOTP string `json:"totp"`
  190. }
  191. // UserClaims - user claims struct
  192. type UserClaims struct {
  193. Role UserRoleID
  194. UserName string
  195. Api string
  196. TokenType TokenType
  197. RacAutoDisable bool
  198. jwt.RegisteredClaims
  199. }
  200. type InviteUsersReq struct {
  201. UserEmails []string `json:"user_emails"`
  202. PlatformRoleID string `json:"platform_role_id"`
  203. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  204. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  205. }
  206. // UserInvite - model for user invite
  207. type UserInvite struct {
  208. Email string `json:"email"`
  209. PlatformRoleID string `json:"platform_role_id"`
  210. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  211. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  212. InviteCode string `json:"invite_code"`
  213. InviteURL string `json:"invite_url"`
  214. }