user_mgmt.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. Auditor UserRoleID = "auditor"
  91. NetworkAdmin UserRoleID = "network-admin"
  92. NetworkUser UserRoleID = "network-user"
  93. )
  94. func (r UserRoleID) String() string {
  95. return string(r)
  96. }
  97. func (g UserGroupID) String() string {
  98. return string(g)
  99. }
  100. func (n NetworkID) String() string {
  101. return string(n)
  102. }
  103. type RsrcPermissionScope struct {
  104. Create bool `json:"create"`
  105. Read bool `json:"read"`
  106. Update bool `json:"update"`
  107. Delete bool `json:"delete"`
  108. VPNaccess bool `json:"vpn_access"`
  109. SelfOnly bool `json:"self_only"`
  110. }
  111. type UserRolePermissionTemplate struct {
  112. ID UserRoleID `json:"id"`
  113. Name string `json:"name"`
  114. Default bool `json:"default"`
  115. MetaData string `json:"meta_data"`
  116. DenyDashboardAccess bool `json:"deny_dashboard_access"`
  117. FullAccess bool `json:"full_access"`
  118. NetworkID NetworkID `json:"network_id"`
  119. NetworkLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"network_level_access"`
  120. GlobalLevelAccess map[RsrcType]map[RsrcID]RsrcPermissionScope `json:"global_level_access"`
  121. }
  122. type CreateGroupReq struct {
  123. Group UserGroup `json:"user_group"`
  124. Members []string `json:"members"`
  125. }
  126. type UserGroup struct {
  127. ID UserGroupID `json:"id"`
  128. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  129. Default bool `json:"default"`
  130. Name string `json:"name"`
  131. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  132. ColorCode string `json:"color_code"`
  133. MetaData string `json:"meta_data"`
  134. }
  135. // User struct - struct for Users
  136. type User struct {
  137. UserName string `json:"username" bson:"username" validate:"min=3,in_charset|email"`
  138. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  139. IsMFAEnabled bool `json:"is_mfa_enabled"`
  140. TOTPSecret string `json:"totp_secret"`
  141. DisplayName string `json:"display_name"`
  142. AccountDisabled bool `json:"account_disabled"`
  143. Password string `json:"password" bson:"password" validate:"required,min=5"`
  144. IsAdmin bool `json:"isadmin" bson:"isadmin"` // deprecated
  145. IsSuperAdmin bool `json:"issuperadmin"` // deprecated
  146. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  147. AuthType AuthType `json:"auth_type"`
  148. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  149. PlatformRoleID UserRoleID `json:"platform_role_id"`
  150. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  151. LastLoginTime time.Time `json:"last_login_time"`
  152. }
  153. type ReturnUserWithRolesAndGroups struct {
  154. ReturnUser
  155. PlatformRole UserRolePermissionTemplate `json:"platform_role"`
  156. UserGroups map[UserGroupID]UserGroup `json:"user_group_ids"`
  157. }
  158. // ReturnUser - return user struct
  159. type ReturnUser struct {
  160. UserName string `json:"username"`
  161. ExternalIdentityProviderID string `json:"external_identity_provider_id"`
  162. IsMFAEnabled bool `json:"is_mfa_enabled"`
  163. DisplayName string `json:"display_name"`
  164. AccountDisabled bool `json:"account_disabled"`
  165. IsAdmin bool `json:"isadmin"`
  166. IsSuperAdmin bool `json:"issuperadmin"`
  167. AuthType AuthType `json:"auth_type"`
  168. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"` // deprecated
  169. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  170. PlatformRoleID UserRoleID `json:"platform_role_id"`
  171. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  172. LastLoginTime time.Time `json:"last_login_time"`
  173. NumAccessTokens int `json:"num_access_tokens"`
  174. }
  175. // UserAuthParams - user auth params struct
  176. type UserAuthParams struct {
  177. UserName string `json:"username"`
  178. Password string `json:"password"`
  179. }
  180. // UserIdentityValidationRequest - user identity validation request struct
  181. type UserIdentityValidationRequest struct {
  182. Password string `json:"password"`
  183. }
  184. // UserIdentityValidationResponse - user identity validation response struct
  185. type UserIdentityValidationResponse struct {
  186. IdentityValidated bool `json:"identity_validated"`
  187. }
  188. type UserTOTPVerificationParams struct {
  189. OTPAuthURL string `json:"otp_auth_url"`
  190. OTPAuthURLSignature string `json:"otp_auth_url_signature"`
  191. TOTP string `json:"totp"`
  192. }
  193. // UserClaims - user claims struct
  194. type UserClaims struct {
  195. Role UserRoleID
  196. UserName string
  197. Api string
  198. TokenType TokenType
  199. RacAutoDisable bool
  200. jwt.RegisteredClaims
  201. }
  202. type InviteUsersReq struct {
  203. UserEmails []string `json:"user_emails"`
  204. PlatformRoleID string `json:"platform_role_id"`
  205. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  206. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  207. }
  208. // UserInvite - model for user invite
  209. type UserInvite struct {
  210. Email string `json:"email"`
  211. PlatformRoleID string `json:"platform_role_id"`
  212. UserGroups map[UserGroupID]struct{} `json:"user_group_ids"`
  213. NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
  214. InviteCode string `json:"invite_code"`
  215. InviteURL string `json:"invite_url"`
  216. }