Documentation
¶
Index ¶
Constants ¶
const ( // ContextKeyUID is the context key for storing user ID. ContextKeyUID = "uid" // ContextKeySubject is the context key for storing user subject/username. ContextKeySubject = "subject" // ContextKeyRoles is the context key for storing user roles. ContextKeyRoles = "roles" )
Variables ¶
var ( TokenNotFoundError = statuserr.UnauthorizedError( errors.New("AuthorizerError:TOKEN_NOT_FOUND"), "没有提供有效的认证信息", ) NeedLoginError = statuserr.UnauthorizedError( errors.New("AuthorizerError:NEED_LOGIN"), "需要登录才能访问", ) PermissionError = statuserr.ForbiddenError( errors.New("AuthorizerError:PERMISSION_DENIED"), "没有权限访问该资源", ) )
Functions ¶
func GetCurrentRoles ¶
GetCurrentRoles retrieves the current user's roles from the context. It returns nil if no roles are set or if the user is not authenticated.
Types ¶
type Claims ¶
type Claims[T UID] interface { GetUID() (T, error) GetSubject() (string, error) GetRoles() ([]string, error) }
Claims represents the interface for extracting user information from authentication tokens. Implementations should provide methods to extract user ID, subject, and roles.
type ContextUtils ¶
type ContextUtils[I UID] struct{}
ContextUtils provides utility functions for working with authentication context. It is parameterized by the user ID type for type safety.
func (ContextUtils[I]) CheckAuthID ¶
func (c ContextUtils[I]) CheckAuthID(ctx context.Context, id I) error
CheckAuthID verifies that the current user ID matches the provided ID. It ensures the user can only access resources belonging to them.
func (ContextUtils[I]) CheckAuthStatus ¶
func (c ContextUtils[I]) CheckAuthStatus(ctx context.Context) error
CheckAuthStatus verifies that the user is authenticated. It returns an error if authentication is required but not present.
func (ContextUtils[I]) GetCurrentID ¶
func (ContextUtils[I]) GetCurrentID(ctx context.Context) (I, error)
GetCurrentID retrieves the current user ID from the context. It returns NeedLoginError if the user is not authenticated or the ID type is incorrect.
type Generator ¶
type Generator[I UID, T Claims[I]] interface { GenerateToken(ctx context.Context, claims T) (string, error) }
Generator defines the interface for generating authentication tokens from claims.
type Parser ¶
type Parser[I UID, T Claims[I]] interface { ParseToken(ctx context.Context, token string) (T, error) }
Parser defines the interface for parsing authentication tokens into claims.
type ParserFunc ¶
ParserFunc is a function type that implements the Parser interface. This allows functions to be used directly as parsers without defining new types.
func (ParserFunc[I, T]) ParseToken ¶
func (f ParserFunc[I, T]) ParseToken(ctx context.Context, token string) (T, error)
ParseToken implements the Parser interface for ParserFunc.
type TokenAuthorizer ¶
TokenAuthorizer combines token parsing and generation capabilities.
type UID ¶
type UID interface {
constraints.Integer | ~string
}
UID represents valid user identifier types, supporting both integer and string IDs.