user_mgmt.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 = "host"
  54. RelayRsrc RsrcType = "relay"
  55. RemoteAccessGwRsrc RsrcType = "remote_access_gw"
  56. GatewayRsrc RsrcType = "gateway"
  57. ExtClientsRsrc RsrcType = "extclient"
  58. InetGwRsrc RsrcType = "inet_gw"
  59. EgressGwRsrc RsrcType = "egress"
  60. NetworkRsrc RsrcType = "network"
  61. EnrollmentKeysRsrc RsrcType = "enrollment_key"
  62. UserRsrc RsrcType = "user"
  63. AclRsrc RsrcType = "acl"
  64. TagRsrc RsrcType = "tag"
  65. DnsRsrc RsrcType = "dns"
  66. FailOverRsrc RsrcType = "fail_over"
  67. MetricRsrc RsrcType = "metric"
  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. ColorCode string `json:"color_code"`
  132. MetaData string `json:"meta_data"`
  133. }
  134. // User struct - struct for Users
  135. type User struct {
  136. UserName string `json:"username" bson:"username" validate:"min=3,in_charset|email"`
  137. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  138. IsMFAEnabled bool `json:"is_mfa_enabled"`
  139. TOTPSecret string `json:"totp_secret"`
  140. DisplayName string `json:"display_name"`
  141. AccountDisabled bool `json:"account_disabled"`
  142. Password string `json:"password" bson:"password" validate:"required,min=5"`
  143. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  144. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  145. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  146. AuthType AuthType `json:"auth_type"`
  147. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  148. PlatformRoleID UserRoleID `json:"platform_role_id"`
  149. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  150. LastLoginTime time.Time `json:"last_login_time"`
  151. }
  152. type ReturnUserWithRolesAndGroups struct {
  153. ReturnUser
  154. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  155. UserGroups map[UserGroupID]UserGroup `json:"user_group_ids"`
  156. }
  157. // ReturnUser - return user struct
  158. type ReturnUser struct {
  159. UserName string `json:"username"`
  160. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  161. IsMFAEnabled bool `json:"is_mfa_enabled"`
  162. DisplayName string `json:"display_name"`
  163. AccountDisabled bool `json:"account_disabled"`
  164. IsAdmin bool `json:"isadmin"`
  165. IsSuperAdmin bool `json:"issuperadmin"`
  166. AuthType AuthType `json:"auth_type"`
  167. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  168. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  169. PlatformRoleID UserRoleID `json:"platform_role_id"`
  170. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  171. LastLoginTime time.Time `json:"last_login_time"`
  172. NumAccessTokens int `json:"num_access_tokens"`
  173. }
  174. // UserAuthParams - user auth params struct
  175. type UserAuthParams struct {
  176. UserName string `json:"username"`
  177. Password string `json:"password"`
  178. }
  179. // UserIdentityValidationRequest - user identity validation request struct
  180. type UserIdentityValidationRequest struct {
  181. Password string `json:"password"`
  182. }
  183. // UserIdentityValidationResponse - user identity validation response struct
  184. type UserIdentityValidationResponse struct {
  185. IdentityValidated bool `json:"identity_validated"`
  186. }
  187. type UserTOTPVerificationParams struct {
  188. OTPAuthURL string `json:"otp_auth_url"`
  189. OTPAuthURLSignature string `json:"otp_auth_url_signature"`
  190. TOTP string `json:"totp"`
  191. }
  192. // UserClaims - user claims struct
  193. type UserClaims struct {
  194. Role UserRoleID
  195. UserName string
  196. Api string
  197. TokenType TokenType
  198. RacAutoDisable bool
  199. jwt.RegisteredClaims
  200. }
  201. type InviteUsersReq struct {
  202. UserEmails []string `json:"user_emails"`
  203. PlatformRoleID string `json:"platform_role_id"`
  204. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  205. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  206. }
  207. // UserInvite - model for user invite
  208. type UserInvite struct {
  209. Email string `json:"email"`
  210. PlatformRoleID string `json:"platform_role_id"`
  211. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  212. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  213. InviteCode string `json:"invite_code"`
  214. InviteURL string `json:"invite_url"`
  215. }