safe

package
v1.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package safe 在本文件中负责校验 HMAC Provider 返回的身份并构造框架可信访问上下文。

本文件提供显式展示 UID;认证 UID 始终保留在 Claims.Uid 和验签后的身份中。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeriveJWTKey

func DeriveJWTKey(password, userID string) string

🔧 改进:更安全的JWT密钥派生

func DeriveJWTKeyWithCustomSalt

func DeriveJWTKeyWithCustomSalt(password, userID, customSalt string) string

🔧 新增:支持自定义盐值的JWT密钥派生

func DeriveKeyForPurpose

func DeriveKeyForPurpose(password, userID, purpose string) string

🔧 新增:为不同用途派生不同的密钥

func GetJWTExpiry

func GetJWTExpiry(tokenString string) int64

func ValidateHMACAuthClaims

func ValidateHMACAuthClaims(claims map[string]string) error

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

func ValidateJWTToken(tokenString string, auth config.AuthSecret) (string, string, error)

ValidateJWTToken 保留为最终用户 Access Token 的兼容包装。 Deprecated: 使用 ValidateAccessToken 并显式传入期望的认证类型和当前时间。

func VerifiedSecretClaimsFromContext

func VerifiedSecretClaimsFromContext(ctx context.Context) map[string]string

VerifiedSecretClaimsFromContext 返回当前请求的服务端秘密 Claim 快照。

func WithVerifiedSecretClaims

func WithVerifiedSecretClaims(ctx context.Context, claims map[string]string) context.Context

WithVerifiedSecretClaims 将已验签并解密的 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 Black

type Black struct {
}

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 NewClaims

func NewClaims(userId string, username string) *Claims

func (*Claims) AddData

func (own *Claims) AddData(key string, value string)

func (*Claims) AddSecretData

func (own *Claims) AddSecretData(key, value string) error

AddSecretData 立即使用服务端域密钥加密数据,不在 Claims 中保留明文。

func (*Claims) ConfigureSecretData

func (own *Claims) ConfigureSecretData(accessSecret string, authType types.AuthType) error

ConfigureSecretData 使用当前认证域 AccessSecret 准备秘密 Claim 的直接加密上下文。

func (*Claims) GetData

func (own *Claims) GetData(key string) (string, error)

func (*Claims) GetToken

func (own *Claims) GetToken(secret string, expire int64) (string, error)

GetToken 保留用于源代码兼容;新代码应使用 IssueTokenPair 生成带用途隔离的 Token。 Deprecated: 使用 IssueTokenPair。

func (*Claims) SetEncryptKey

func (own *Claims) SetEncryptKey(key string) *Claims

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

func (own *Claims) SetTokenUID(uid string) error

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。

type White

type White struct {
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL