Documentation
¶
Overview ¶
Package safe 在本文件中负责校验 HMAC Provider 返回的身份并构造框架可信访问上下文。
本文件提供显式展示 UID;认证 UID 始终保留在 Claims.Uid 和验签后的身份中。
Index ¶
- func DeriveJWTKey(password, userID string) string
- func DeriveJWTKeyWithCustomSalt(password, userID, customSalt string) string
- func DeriveKeyForPurpose(password, userID, purpose string) string
- func GetJWTExpiry(tokenString string) int64
- func ValidateHMACAuthClaims(claims map[string]string) error
- func ValidateJWTToken(tokenString string, auth config.AuthSecret) (string, string, error)
- func VerifiedSecretClaimsFromContext(ctx context.Context) map[string]string
- func WithVerifiedSecretClaims(ctx context.Context, claims map[string]string) context.Context
- type AccessTokenIdentity
- type Black
- type Claims
- func (own *Claims) AddData(key string, value string)
- func (own *Claims) AddSecretData(key, value string) error
- func (own *Claims) ConfigureSecretData(accessSecret string, authType types.AuthType) error
- func (own *Claims) GetData(key string) (string, error)
- func (own *Claims) GetToken(secret string, expire int64) (string, error)
- func (own *Claims) SetEncryptKey(key string) *Claims
- func (own *Claims) SetManageRoles(roles []types.ManageRoleRef) error
- func (own *Claims) SetTokenUID(uid string) error
- type RefreshTokenIdentity
- type TokenIssueRequest
- type TokenPairResponse
- type White
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeriveJWTKeyWithCustomSalt ¶
🔧 新增:支持自定义盐值的JWT密钥派生
func DeriveKeyForPurpose ¶
🔧 新增:为不同用途派生不同的密钥
func GetJWTExpiry ¶
func ValidateHMACAuthClaims ¶
ValidateHMACAuthClaims 供消费方在返回 HMACAuthResult 前预检查业务 Claims。 保留键为 uid、uname、auth_type、token_use、iat、exp、auth_provider、 provider_subject、auth_generation、auth_authority_service、args 和 secret_args。
func ValidateJWTToken ¶
ValidateJWTToken 保留为最终用户 Access Token 的兼容包装。 Deprecated: 使用 ValidateAccessToken 并显式传入期望的认证类型和当前时间。
func VerifiedSecretClaimsFromContext ¶
VerifiedSecretClaimsFromContext 返回当前请求的服务端秘密 Claim 快照。
Types ¶
type AccessTokenIdentity ¶
type AccessTokenIdentity struct {
UID string
Username string
AuthType types.AuthType
IssuedAt time.Time
ExpiresAt time.Time
Identity types.AuthIdentity
Claims map[string]interface{}
SecretClaims map[string]string `json:"-"`
}
AccessTokenIdentity 是从已验证 Access Token 中提取的身份。
func BuildHMACAccessIdentity ¶
func BuildHMACAccessIdentity( result *types.HMACAuthResult, accessKey string, expectedAuthType types.AuthType, now time.Time, maxLifetime time.Duration, ) (*AccessTokenIdentity, error)
BuildHMACAccessIdentity 校验服务返回的 HMAC 身份,并构造 REST 与 WebSocket 共用的可信访问身份。
func ValidateAccessToken ¶
func ValidateAccessToken(tokenString, secret string, expectedAuthType types.AuthType, now time.Time) (*AccessTokenIdentity, error)
ValidateAccessToken 验证签名、算法、用途、认证类型和有效期后返回身份。
type Claims ¶
type Claims struct {
Uid string `json:"userid"`
Uname string `json:"username"`
Args map[string]string `json:"args"`
EncryptKey string `json:"-"`
// contains filtered or unexported fields
}
func (*Claims) AddSecretData ¶
AddSecretData 立即使用服务端域密钥加密数据,不在 Claims 中保留明文。
func (*Claims) ConfigureSecretData ¶
ConfigureSecretData 使用当前认证域 AccessSecret 准备秘密 Claim 的直接加密上下文。
func (*Claims) GetToken ¶
GetToken 保留用于源代码兼容;新代码应使用 IssueTokenPair 生成带用途隔离的 Token。 Deprecated: 使用 IssueTokenPair。
func (*Claims) SetEncryptKey ¶
func (*Claims) SetManageRoles ¶ added in v1.3.0
func (own *Claims) SetManageRoles(roles []types.ManageRoleRef) error
SetManageRoles 设置由 Core 签发流程管理的标准 RoleCode Claim。 该值只会进入 Manage Access Token,不会写入 Refresh Token。
func (*Claims) SetTokenUID ¶ added in v1.11.0
SetTokenUID 只设置普通用户 Token 的展示 uid,不改变认证、撤销或秘密 Claim 的身份。 非法值会阻止签发,即使调用方忽略本方法返回的错误。
type RefreshTokenIdentity ¶
type RefreshTokenIdentity struct {
UID string
Username string
AuthType types.AuthType
IssuedAt time.Time
ExpiresAt time.Time
Identity types.AuthIdentity
Claims map[string]interface{}
// ManageTestIdentity 只由 Core TestToken 签发链写入并从 Refresh Token 恢复。
ManageTestIdentity bool
}
RefreshTokenIdentity 是从已验证 Refresh Token 中提取的不可信任边界后身份。
func ValidateRefreshToken ¶
func ValidateRefreshToken(tokenString, secret string, expectedAuthType types.AuthType, now time.Time) (*RefreshTokenIdentity, error)
ValidateRefreshToken 验证签名、算法、用途、认证类型和有效期后返回身份。
type TokenIssueRequest ¶
type TokenIssueRequest struct {
Claims *Claims
Identity types.AuthIdentity
AuthType types.AuthType
IssuedAt time.Time
AccessSecret string
AccessExpireSeconds int64
RefreshSecret string
RefreshExpireSeconds int64
IssueRefresh bool
// ManageTestIdentity 只允许 Core Manage TestToken 签发链设置。
ManageTestIdentity bool
}
TokenIssueRequest 包含签名 Token 所需的全部已验证输入。
type TokenPairResponse ¶
type TokenPairResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token,omitempty"`
TokenType string `json:"token_type"`
AccessExpiresIn int64 `json:"access_expires_in"`
RefreshExpiresIn int64 `json:"refresh_expires_in,omitempty"`
}
TokenPairResponse 是 Callback、TestToken 和 Refresh 共用的 Token 响应。
func IssueTokenPair ¶
func IssueTokenPair(req TokenIssueRequest) (TokenPairResponse, error)
IssueTokenPair 使用同一 IssuedAt 颁发 Access Token 和可选的 Refresh Token。