Documentation
¶
Index ¶
- Variables
- func CreateAccessToken(secret string, accessKey string, expires time.Duration) (string, error)
- func CreateLoginToken(secret string, username string, expires time.Duration) (string, error)
- func CreateTokenWithClaims(secret string, claims jwt.Claims) (string, error)
- func EncryptPassword(password string) string
- func GeneratePassword() string
- func GetTokenFromEchoContext(c echo.Context) (*jwt.Token, error)
- func NewEnforcer(driverName string, dataSourceName string) (*casbin.Enforcer, error)
- func ParseSchemaAndName(data string) (string, string)
- type ApiRule
- type AuditLog
- type GroupRule
- type IAMServer
- func (s *IAMServer) AddApiRuleForRole(domain string, role string, rule ApiRule) (bool, error)
- func (s *IAMServer) AddGroupPolicyForRole(domain string, role string, rule GroupRule) (bool, error)
- func (s *IAMServer) AddRoleForUser(domain string, user string, role string) (bool, error)
- func (s *IAMServer) AddRolesForUser(domain string, user string, roles []string) (bool, error)
- func (s *IAMServer) CreateDefaultRole(schema string, name string, alias string, patterns []string) (*Role, error)
- func (s *IAMServer) CreateDefaultUser(schema, name string, alias string, password string) (*User, error)
- func (s *IAMServer) CreateRole(schema string, name string, alias string, patterns []string) (*Role, error)
- func (s *IAMServer) CreateUser(schema, name string, alias string, password string) (*User, error)
- func (s *IAMServer) DeleteAllApiRulesForRole(domain string, role string) (bool, error)
- func (s *IAMServer) DeleteAllGroupRulesForRole(domain string, role string) (bool, error)
- func (s *IAMServer) DeleteAllRolesForUser(domain string, user string) (bool, error)
- func (s *IAMServer) Enforce(dom string, sub string, obj string, act string) bool
- func (s *IAMServer) EnforceApi(domain string, user string, path string, method string) bool
- func (s *IAMServer) GetApiRuleIdByRule(rule ApiRule) string
- func (s *IAMServer) GetApiRuleIds(patterns ...string) []string
- func (s *IAMServer) GetApiRuleIdsByRule(rules []ApiRule) []string
- func (s *IAMServer) GetApiRules(patterns ...string) []ApiRule
- func (s *IAMServer) GetApiRulesForRole(domain string, role string) []ApiRule
- func (s *IAMServer) GetApiRulesForUser(domain string, user string) []ApiRule
- func (s *IAMServer) GetDB() *gorm.DB
- func (s *IAMServer) GetGroupRulesForRole(domain string, role string) []GroupRule
- func (s *IAMServer) GetRolesForUser(domain string, user string) ([]string, error)
- func (s *IAMServer) HandleAddUserRoles(c echo.Context) error
- func (s *IAMServer) HandleAuthenticate(c echo.Context) error
- func (s *IAMServer) HandleChangePassword(c echo.Context) error
- func (s *IAMServer) HandleChangeUserPassword(c echo.Context) error
- func (s *IAMServer) HandleCreateRole(c echo.Context) error
- func (s *IAMServer) HandleCreateUser(c echo.Context) error
- func (s *IAMServer) HandleDeleteRole(c echo.Context) error
- func (s *IAMServer) HandleDeleteUser(c echo.Context) error
- func (s *IAMServer) HandleGetAuditLog(c echo.Context) error
- func (s *IAMServer) HandleGetRole(c echo.Context) error
- func (s *IAMServer) HandleGetUser(c echo.Context) error
- func (s *IAMServer) HandleGetUserRoles(c echo.Context) error
- func (s *IAMServer) HandleListApiRules(c echo.Context) error
- func (s *IAMServer) HandleListAuditLogs(c echo.Context) error
- func (s *IAMServer) HandleListRoles(c echo.Context) error
- func (s *IAMServer) HandleListUsers(c echo.Context) error
- func (s *IAMServer) HandleResetUserPassword(c echo.Context) error
- func (s *IAMServer) HandleSetRoleApiRules(c echo.Context) error
- func (s *IAMServer) HandleSetRoleDisable(c echo.Context) error
- func (s *IAMServer) HandleSetRoleEnable(c echo.Context) error
- func (s *IAMServer) HandleSetUserDisable(c echo.Context) error
- func (s *IAMServer) HandleSetUserEnable(c echo.Context) error
- func (s *IAMServer) HandleSetUserRoles(c echo.Context) error
- func (s *IAMServer) HandleUpdateRole(c echo.Context) error
- func (s *IAMServer) HandleUpdateUser(c echo.Context) error
- func (s *IAMServer) HandleWhoAmI(c echo.Context) error
- func (s *IAMServer) InsertAuditLog(user *User, method string, registerPath string, path string, status int, ...) error
- func (s *IAMServer) MakeAuditLogHandler() echo.MiddlewareFunc
- func (s *IAMServer) MakeJwtHandler() echo.MiddlewareFunc
- func (s *IAMServer) MakeLoginHandler() echo.MiddlewareFunc
- func (s *IAMServer) SetApiRulesForRole(domain string, role string, rules []ApiRule) (bool, error)
- func (s *IAMServer) SetAuditLogIgnoreIds(ids []string)
- func (s *IAMServer) SetGroupPoliciesForRole(domain string, role string, rules []GroupRule) (bool, error)
- func (s *IAMServer) SetRolesForUser(domain string, user string, roles []string) (bool, error)
- func (s *IAMServer) SetupAdmin(schema string, password string) error
- func (s *IAMServer) SetupEchoGroup(group *echo.Group) *echo.Group
- type Role
- type Tags
- type User
- func (m *User) AfterFind(tx *gorm.DB) (err error)
- func (m *User) BeforeSave(tx *gorm.DB) (err error)
- func (s *User) ChangePassword(old string, password string) bool
- func (s User) CheckPassword(password string) bool
- func (m *User) GetRolesAndRules(s *IAMServer)
- func (s *User) SetPassword(password string)
Constants ¶
This section is empty.
Variables ¶
var AuditLogIndexNames = []string{"schema", "api_name"}
var AuditLogUserIndexNames = []string{"schema", "user_id", "user_name", "api_name"}
Functions ¶
func CreateAccessToken ¶
func CreateLoginToken ¶
func CreateTokenWithClaims ¶
func EncryptPassword ¶
func GeneratePassword ¶
func GeneratePassword() string
func GetTokenFromEchoContext ¶
func GetTokenFromEchoContext(c echo.Context) (*jwt.Token, error)
func NewEnforcer ¶
func ParseSchemaAndName ¶
Types ¶
type AuditLog ¶
type AuditLog struct { Time jsontype.JSONTime `json:"Time" gorm:"autoCreateTime;uniqueIndex:idx_user_audit_log_unique;not null"` Schema string `json:"Schema" gorm:"uniqueIndex:idx_user_audit_log_unique;not null"` UserID uint `json:"UserId" gorm:"uniqueIndex:idx_user_audit_log_unique;not null"` User *User `json:"User" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Updated jsontype.JSONTime `json:"Updated" gorm:"autoUpdateTime"` UserName string `json:"UserName"` UserAlias string `json:"UserAlias"` ApiName string `json:"ApiName" gorm:"uniqueIndex:idx_user_audit_log_unique;not null"` Method string `json:"Method"` Path string `json:"Path"` Status int `json:"Status"` MetaDataRaw jsontype.JSONType[*jsontype.Tags] `json:"-" gorm:"column:meta_data"` MetaData *jsontype.Tags `json:"MetaData" gorm:"-"` }
type IAMServer ¶
type IAMServer struct {
// contains filtered or unexported fields
}
func NewIAMServer ¶
func (*IAMServer) AddApiRuleForRole ¶
func (*IAMServer) AddGroupPolicyForRole ¶
func (*IAMServer) AddRoleForUser ¶
func (*IAMServer) AddRolesForUser ¶
func (*IAMServer) CreateDefaultRole ¶
func (*IAMServer) CreateDefaultUser ¶
func (*IAMServer) CreateRole ¶
func (*IAMServer) CreateUser ¶
func (*IAMServer) DeleteAllApiRulesForRole ¶
func (*IAMServer) DeleteAllGroupRulesForRole ¶
func (*IAMServer) DeleteAllRolesForUser ¶
func (*IAMServer) EnforceApi ¶
func (*IAMServer) GetApiRuleIdByRule ¶
func (*IAMServer) GetApiRuleIds ¶
func (*IAMServer) GetApiRuleIdsByRule ¶
func (*IAMServer) GetApiRules ¶
func (*IAMServer) GetApiRulesForRole ¶
func (*IAMServer) GetApiRulesForUser ¶
func (*IAMServer) GetGroupRulesForRole ¶
func (*IAMServer) GetRolesForUser ¶
func (*IAMServer) HandleAddUserRoles ¶
HandleAddUserRoles godoc @Summary Add roles for user @Tags Users @ID add-user-roles @Security Bearer @Param id path int true "User ID" @Param body body addUserRolesBody true "Roles to add" @Success 200 {object} User @Failure 400 {object} echo.HTTPError "Bad Request" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal Server Error" @Router /iam/users/{id}/roles [post]
func (*IAMServer) HandleAuthenticate ¶
@Summary Authenticate a user @Tags Auth @Accept json @Produce json @Param body body authBody true "Authenticate request" @Success 200 {object} authResponseBody "Returns a login token" @Failure 400 {object} echo.HTTPError "Bad request" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /auth [POST]
func (*IAMServer) HandleChangePassword ¶
@Summary Change current user password @Tags Current @ID change-current-password @Accept json @Produce json @Param body body changePasswordBody true "Change password request" @Security Bearer @Success 200 "Password changed successfully" @Failure 400 {object} echo.HTTPError "Bad request" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/current/change-password [PUT]
func (*IAMServer) HandleChangeUserPassword ¶
HandleChangeUserPassword changes a user's password. @Summary Change user password @ID change-user-password @Tags Users @Produce json @Security Bearer @Param id path int true "User ID" @Param body body changePasswordBody true "Change password request" @Success 200 {object} User @Failure 400 {object} echo.HTTPError "Invalid input parameter" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 404 {object} echo.HTTPError "User not found" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users/{id}/change-password [put]
func (*IAMServer) HandleCreateRole ¶
HandleCreateRole godoc @Summary Create a role @Tags Roles @ID create-role @Security Bearer @Param body body createRoleBody true "Create Role Body" @Success 200 {object} Role @Failure 400 {object} echo.HTTPError "Bad Request" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/roles [post]
func (*IAMServer) HandleCreateUser ¶
HandleCreateUser creates a new user. @Summary Create user @ID create-user @Tags Users @Produce json @Security Bearer @Param body body createUserBody true "User" @Success 200 {object} User @Failure 400 {object} echo.HTTPError "Bad Request: invalid input parameter" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users [post]
func (*IAMServer) HandleDeleteRole ¶
HandleDeleteRole godoc @Summary Delete role @Tags Roles @ID delete-role @Security Bearer @Param id path int true "Role ID" @Success 204 "No Content" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/roles/{id} [delete]
func (*IAMServer) HandleDeleteUser ¶
HandleDeleteUser deletes a user. @Summary Delete user @ID delete-user @Tags Users @Produce json @Security Bearer @Param id path int true "User ID" @Success 204 @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 403 {object} echo.HTTPError "Forbidden" @Failure 404 {object} echo.HTTPError "User not found" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users/{id} [delete]
func (*IAMServer) HandleGetAuditLog ¶
HandleGetAuditLog retrieves a single audit log. @Summary Get audit log @ID get-audit-log @Produce json @Security Bearer @Param ts path int true "Timestamp" @Success 200 {object} AuditLog @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal Server error" @Router /iam/audit-logs/{ts} [get] @Tags AuditLogs
func (*IAMServer) HandleGetRole ¶
HandleGetRole godoc @Summary Get role @Tags Roles @ID get-role @Security Bearer @Param id path int true "Role ID" @Success 200 {object} Role @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/roles/{id} [get]
func (*IAMServer) HandleGetUser ¶
HandleGetUser retrieves a single user from the database. @Summary Get user @ID get-user @Tags Users @Produce json @Security Bearer @Param id path int true "User ID" @Success 200 {object} User @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 404 {object} echo.HTTPError "User not found" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users/{id} [get]
func (*IAMServer) HandleGetUserRoles ¶
HandleGetUserRoles godoc @Summary Get roles for user @Tags Users @ID get-user-roles @Security Bearer @Param id path int true "User ID" @Success 200 {array} string @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal Server Error" @Router /iam/users/{id}/roles [get]
func (*IAMServer) HandleListApiRules ¶
HandleListApiRules godoc @Summary List rules @ID list-rules @Tags Rules @Security Bearer @Success 200 {object} listRulesBody @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/rules [get]
func (*IAMServer) HandleListAuditLogs ¶
HandleListAuditLogs lists all audit logs. @Summary List audit logs @ID list-audit-logs @Produce json @Security Bearer @Param page query int false "Page" default(1) @Param page_size query int false "Page size" default(20) @Param order_by query string false "Sort order" default() @Param q query string false "Query" default() @Success 200 {object} listAuditLogsData @Header 200 {int} X-Total "Total number" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal Server error" @Router /iam/audit-logs [get] @Tags AuditLogs
func (*IAMServer) HandleListRoles ¶
HandleListRoles godoc @Summary List roles @ID list-roles @Tags Roles @Security Bearer @Param page query int false "Page" default(1) @Param page_size query int false "Page size" default(20) @Param order_by query string false "Sort order" default() @Param q query string false "Query" default() @Success 200 {object} listRolesData @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/roles [get]
func (*IAMServer) HandleListUsers ¶
HandleListUsers lists users based on specified filters. @Summary List users @ID list-users @Tags Users @Produce json @Param page query int false "Page" default(1) @Param page_size query int false "Page size" default(20) @Param order_by query string false "Sort order" default() @Param q query string false "Query" default() @Security Bearer @Success 200 {object} listUsersData @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users [get]
func (*IAMServer) HandleResetUserPassword ¶
HandleResetUserPassword resets a user's password @Summary Reset user password @ID reset-user-password @Tags Users @Produce json @Security Bearer @Param id path int true "User ID" @Success 200 {object} echo.Map @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 404 {object} echo.HTTPError "User not found" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users/{id}/reset-password [put]
func (*IAMServer) HandleSetRoleApiRules ¶
HandleSetRoleApiRules godoc @Summary Set rules for role @Tags Roles @ID set-role-rules @Security Bearer @Param id path int true "Role ID" @Param body body setRoleApiRulesBody true "rules to set" @Success 200 {object} User @Failure 400 {object} echo.HTTPError "Bad Request" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal Server Error" @Router /iam/roles/{id}/rules [put]
func (*IAMServer) HandleSetRoleDisable ¶
HandleSetRoleDisable godoc @Summary Set role disable @Tags Roles @ID set-role-disable @Security Bearer @Param id path int true "Role ID" @Success 200 {object} Role @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 403 {object} echo.HTTPError "Forbidden" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/roles/{id}/disable [put]
func (*IAMServer) HandleSetRoleEnable ¶
HandleSetRoleEnable godoc @Summary Set role enable @Tags Roles @ID set-role-enable @Security Bearer @Param id path int true "Role ID" @Success 200 {object} Role @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 403 {object} echo.HTTPError "Forbidden" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/roles/{id}/enable [put]
func (*IAMServer) HandleSetUserDisable ¶
HandleSetUserDisable godoc @Summary Set user disable @Tags Users @ID set-user-disable @Security Bearer @Param id path int true "User ID" @Success 200 {object} User @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 403 {object} echo.HTTPError "Forbidden" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users/{id}/disable [put]
func (*IAMServer) HandleSetUserEnable ¶
HandleSetUserEnable godoc @Summary Set user enable @Tags Users @ID set-user-enable @Security Bearer @Param id path int true "User ID" @Success 200 {object} User @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 403 {object} echo.HTTPError "Forbidden" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users/{id}/enable [put]
func (*IAMServer) HandleSetUserRoles ¶
HandleSetUserRoles godoc @Summary Set roles for user @Tags Users @ID set-user-roles @Security Bearer @Param id path int true "User ID" @Param body body setUserRoles true "Roles to set" @Success 200 {object} User @Failure 400 {object} echo.HTTPError "Bad Request" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal Server Error" @Router /iam/users/{id}/roles [put]
func (*IAMServer) HandleUpdateRole ¶
HandleUpdateRole godoc @Summary Update role @Tags Roles @ID update-role @Security Bearer @Param id path int true "Role ID" @Param body body updateRoleBody true "Update Role Body" @Success 200 {object} Role @Failure 400 {object} echo.HTTPError "Bad Request" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/roles/{id} [put]
func (*IAMServer) HandleUpdateUser ¶
HandleUpdateUser updates a user @Summary Update user @ID update-user @Tags Users @Produce json @Security Bearer @Param id path int true "User ID" @Param body body updateUserBody true "User" @Success 200 {object} User @Failure 400 {object} echo.HTTPError "Bad Request: invalid input parameter" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 404 {object} echo.HTTPError "User not found" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/users/{id} [put]
func (*IAMServer) HandleWhoAmI ¶
@Summary Get current user information @Tags Current @ID get-current @Accept json @Produce json @Security Bearer @Success 200 {object} User "User information" @Failure 401 {object} echo.HTTPError "Unauthorized" @Failure 500 {object} echo.HTTPError "Internal server error" @Router /iam/current [GET]
func (*IAMServer) InsertAuditLog ¶
func (*IAMServer) MakeAuditLogHandler ¶
func (s *IAMServer) MakeAuditLogHandler() echo.MiddlewareFunc
func (*IAMServer) MakeJwtHandler ¶
func (s *IAMServer) MakeJwtHandler() echo.MiddlewareFunc
func (*IAMServer) MakeLoginHandler ¶
func (s *IAMServer) MakeLoginHandler() echo.MiddlewareFunc
func (*IAMServer) SetApiRulesForRole ¶
func (*IAMServer) SetAuditLogIgnoreIds ¶
func (*IAMServer) SetGroupPoliciesForRole ¶
func (*IAMServer) SetRolesForUser ¶
func (*IAMServer) SetupEchoGroup ¶
func (s *IAMServer) SetupEchoGroup(group *echo.Group) *echo.Group
type Role ¶
type Role struct { ID uint `json:"Id" gorm:"primarykey"` Updated int64 `json:"Updated" gorm:"autoUpdateTime"` Created int64 `json:"Created" gorm:"autoCreateTime"` Deleted gorm.DeletedAt `json:"Deleted" gorm:"index"` Schema string `json:"Schema" gorm:"uniqueIndex:idx_iam_role_unique"` Name string `json:"Name" gorm:"uniqueIndex:idx_iam_role_unique,<-:create"` Alias string `json:"Alias"` Default bool `json:"Default" gorm:"<-:create,default:false"` Enable bool `json:"Enable" gorm:"index"` MetaDataRaw jsontype.JSONType[*jsontype.Tags] `json:"-" gorm:"column:meta_data"` MetaData *jsontype.Tags `json:"MetaData" gorm:"-"` Rules []string `json:"Rules" gorm:"-"` }
type User ¶
type User struct { ID uint `json:"Id" gorm:"primarykey"` Updated int64 `json:"Updated" gorm:"autoUpdateTime"` Created int64 `json:"Created" gorm:"autoCreateTime"` Deleted gorm.DeletedAt `json:"Deleted" gorm:"index"` Schema string `json:"Schema" gorm:"uniqueIndex:idx_iam_user_unique"` Name string `json:"Name" gorm:"uniqueIndex:idx_iam_user_unique,<-:create"` Alias string `json:"Alias"` Password string `json:"-"` PasswordExpireAt int64 `json:"PasswordExpireAt"` Default bool `json:"Default" gorm:"<-:create,default:false"` Enable bool `json:"Enable" gorm:"index"` MetaDataRaw jsontype.JSONType[*jsontype.Tags] `json:"-" gorm:"column:meta_data"` MetaData *jsontype.Tags `json:"MetaData" gorm:"-"` Roles []string `json:"Roles" gorm:"-"` Rules []string `json:"Rules" gorm:"-"` }