Documentation
¶
Overview ¶
@Author daixk 2025/12/22 15:56:00
@Author daixk 2025/12/22 15:56:00
Index ¶
- Constants
- type AccessToken
- type AuthorizationCode
- type Client
- type Config
- type GrantType
- type OAuth2Server
- func NewDefaultOAuth2Server(authType, prefix string, storage adapter.Storage, serializer adapter.Codec) *OAuth2Server
- func NewOAuth2Server(authType, prefix string, storage adapter.Storage, serializer adapter.Codec) *OAuth2Server
- func NewOAuth2ServerWithConfig(authType, prefix string, storage adapter.Storage, serializer adapter.Codec, ...) *OAuth2Server
- func (s *OAuth2Server) ClientCredentialsToken(ctx context.Context, clientID, clientSecret string, scopes []string) (*AccessToken, error)
- func (s *OAuth2Server) ExchangeCodeForToken(ctx context.Context, code, clientID, clientSecret, redirectURI string) (*AccessToken, error)
- func (s *OAuth2Server) GenerateAuthorizationCode(ctx context.Context, clientID, userID, redirectURI string, scopes []string) (*AuthorizationCode, error)
- func (s *OAuth2Server) GetClient(clientID string) (*Client, error)
- func (s *OAuth2Server) PasswordGrantToken(ctx context.Context, clientID, clientSecret, username, password string, ...) (*AccessToken, error)
- func (s *OAuth2Server) RefreshAccessToken(ctx context.Context, clientID, refreshToken, clientSecret string) (*AccessToken, error)
- func (s *OAuth2Server) RegisterClient(client *Client) error
- func (s *OAuth2Server) RevokeToken(ctx context.Context, accessToken string) error
- func (s *OAuth2Server) Token(ctx context.Context, req *TokenRequest, validateUser UserValidator) (*AccessToken, error)
- func (s *OAuth2Server) UnregisterClient(clientID string) error
- func (s *OAuth2Server) ValidateAccessToken(ctx context.Context, accessToken string) bool
- func (s *OAuth2Server) ValidateAccessTokenAndGetInfo(ctx context.Context, accessToken string) (*AccessToken, error)
- type TokenRequest
- type UserValidator
Constants ¶
const ( // DefaultCodeExpiration stores code expiration DefaultCodeExpiration 存储授权码过期时间 DefaultCodeExpiration = 10 * time.Minute // DefaultTokenExpiration stores token expiration DefaultTokenExpiration 存储访问令牌过期时间 DefaultTokenExpiration = 2 * time.Hour // DefaultRefreshTTL stores refresh expiration DefaultRefreshTTL 存储刷新令牌过期时间 DefaultRefreshTTL = 30 * 24 * time.Hour // CodeLength stores code byte length CodeLength 存储授权码字节长度 CodeLength = 32 // AccessTokenLength stores access token byte length AccessTokenLength 存储访问令牌字节长度 AccessTokenLength = 32 // RefreshTokenLength stores refresh token byte length RefreshTokenLength 存储刷新令牌字节长度 RefreshTokenLength = 32 // CodeKeySuffix stores code key suffix CodeKeySuffix 存储授权码键后缀 CodeKeySuffix = "oauth2:code:" // TokenKeySuffix stores token key suffix TokenKeySuffix 存储令牌键后缀 TokenKeySuffix = "oauth2:token:" // RefreshKeySuffix stores refresh key suffix RefreshKeySuffix 存储刷新令牌键后缀 RefreshKeySuffix = "oauth2:refresh:" // ClientKeySuffix stores client key suffix. ClientKeySuffix 存储客户端键后缀。 ClientKeySuffix = "oauth2:client:" // TokenTypeBearer stores bearer token type TokenTypeBearer 存储 Bearer 令牌类型 TokenTypeBearer = "Bearer" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccessToken ¶
type AccessToken struct {
Token string // Access token 访问令牌
TokenType string // Token type (Bearer) 令牌类型(Bearer)
ExpiresIn int64 // Expiration time in seconds 过期时间(秒)
RefreshToken string // Refresh token 刷新令牌
Scopes []string // Granted scopes 授予的权限范围
UserID string // User ID 用户ID
ClientID string // Client ID 客户端ID
}
AccessToken access token information 访问令牌信息
type AuthorizationCode ¶
type AuthorizationCode struct {
Code string // Authorization code 授权码
ClientID string // Client ID 客户端ID
RedirectURI string // Redirect URI 回调URI
UserID string // User ID 用户ID
Scopes []string // Requested scopes 请求的权限范围
CreateTime int64 // Creation time 创建时间
ExpiresIn int64 // Expiration time in seconds 过期时间(秒)
Used bool // Whether used 是否已使用
}
AuthorizationCode authorization code information 授权码信息
type Client ¶
type Client struct {
ClientID string // Client ID 客户端ID
ClientSecret string // Client secret 客户端密钥
RedirectURIs []string // Allowed redirect URIs 允许的回调URI
GrantTypes []GrantType // Allowed grant types 允许的授权类型
Scopes []string // Allowed scopes 允许的权限范围
}
Client OAuth2 client configuration OAuth2客户端配置
type Config ¶
type Config struct {
// CodeExpiration stores authorization code ttl CodeExpiration 存储授权码有效期
CodeExpiration time.Duration
// TokenExpiration stores access token ttl TokenExpiration 存储访问令牌有效期
TokenExpiration time.Duration
// RefreshExpiration stores refresh token ttl RefreshExpiration 存储刷新令牌有效期
RefreshExpiration time.Duration
}
Config defines OAuth2 server config Config 定义 OAuth2 服务端配置
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns default OAuth2 config DefaultConfig 返回默认 OAuth2 配置
type GrantType ¶
type GrantType string
GrantType defines oauth2 grant type GrantType 定义 OAuth2 授权类型
const ( // GrantTypeAuthorizationCode stores authorization code mode GrantTypeAuthorizationCode 存储授权码模式 GrantTypeAuthorizationCode GrantType = "authorization_code" // GrantTypeRefreshToken stores refresh token mode GrantTypeRefreshToken 存储刷新令牌模式 GrantTypeRefreshToken GrantType = "refresh_token" // GrantTypeClientCredentials stores client credentials mode GrantTypeClientCredentials 存储客户端凭证模式 GrantTypeClientCredentials GrantType = "client_credentials" // GrantTypePassword stores password mode GrantTypePassword 存储密码模式 GrantTypePassword GrantType = "password" )
type OAuth2Server ¶
type OAuth2Server struct {
// contains filtered or unexported fields
}
OAuth2Server OAuth2 authorization server OAuth2授权服务器
func NewDefaultOAuth2Server ¶
func NewDefaultOAuth2Server(authType, prefix string, storage adapter.Storage, serializer adapter.Codec) *OAuth2Server
NewDefaultOAuth2Server creates OAuth2 server with default config NewDefaultOAuth2Server 使用默认配置创建 OAuth2 服务端
func NewOAuth2Server ¶
func NewOAuth2Server(authType, prefix string, storage adapter.Storage, serializer adapter.Codec) *OAuth2Server
NewOAuth2Server Creates a new OAuth2 server 创建新的OAuth2服务器
func NewOAuth2ServerWithConfig ¶
func NewOAuth2ServerWithConfig(authType, prefix string, storage adapter.Storage, serializer adapter.Codec, cfg *Config) *OAuth2Server
NewOAuth2ServerWithConfig creates OAuth2 server with config NewOAuth2ServerWithConfig 使用配置创建 OAuth2 服务端
func (*OAuth2Server) ClientCredentialsToken ¶
func (s *OAuth2Server) ClientCredentialsToken(ctx context.Context, clientID, clientSecret string, scopes []string) (*AccessToken, error)
ClientCredentialsToken Gets access token using client credentials grant 使用客户端凭证模式获取访问令牌
func (*OAuth2Server) ExchangeCodeForToken ¶
func (s *OAuth2Server) ExchangeCodeForToken(ctx context.Context, code, clientID, clientSecret, redirectURI string) (*AccessToken, error)
ExchangeCodeForToken Exchanges authorization code for access token 用授权码换取访问令牌
func (*OAuth2Server) GenerateAuthorizationCode ¶
func (s *OAuth2Server) GenerateAuthorizationCode(ctx context.Context, clientID, userID, redirectURI string, scopes []string) (*AuthorizationCode, error)
GenerateAuthorizationCode Generates authorization code 生成授权码
func (*OAuth2Server) GetClient ¶
func (s *OAuth2Server) GetClient(clientID string) (*Client, error)
GetClient Gets client by ID 根据ID获取客户端
func (*OAuth2Server) PasswordGrantToken ¶
func (s *OAuth2Server) PasswordGrantToken(ctx context.Context, clientID, clientSecret, username, password string, scopes []string, validateUser UserValidator) (*AccessToken, error)
PasswordGrantToken Gets access token using resource owner password credentials grant 使用密码模式获取访问令牌
func (*OAuth2Server) RefreshAccessToken ¶
func (s *OAuth2Server) RefreshAccessToken(ctx context.Context, clientID, refreshToken, clientSecret string) (*AccessToken, error)
RefreshAccessToken Refreshes access token using refresh token 使用刷新令牌刷新访问令牌
func (*OAuth2Server) RegisterClient ¶
func (s *OAuth2Server) RegisterClient(client *Client) error
RegisterClient Registers an OAuth2 client 注册OAuth2客户端
func (*OAuth2Server) RevokeToken ¶
func (s *OAuth2Server) RevokeToken(ctx context.Context, accessToken string) error
RevokeToken Revokes access token and its refresh token 撤销访问令牌及其刷新令牌
func (*OAuth2Server) Token ¶
func (s *OAuth2Server) Token(ctx context.Context, req *TokenRequest, validateUser UserValidator) (*AccessToken, error)
Token Unified token endpoint that dispatches to appropriate handler based on grant type 统一的令牌端点,根据授权类型分发到相应的处理逻辑
func (*OAuth2Server) UnregisterClient ¶
func (s *OAuth2Server) UnregisterClient(clientID string) error
UnregisterClient Unregisters an OAuth2 client 注销OAuth2客户端
func (*OAuth2Server) ValidateAccessToken ¶
func (s *OAuth2Server) ValidateAccessToken(ctx context.Context, accessToken string) bool
ValidateAccessToken Validates access token 验证访问令牌
func (*OAuth2Server) ValidateAccessTokenAndGetInfo ¶
func (s *OAuth2Server) ValidateAccessTokenAndGetInfo(ctx context.Context, accessToken string) (*AccessToken, error)
ValidateAccessTokenAndGetInfo Validates access token and get info 验证访问令牌并获取信息
type TokenRequest ¶
type TokenRequest struct {
GrantType GrantType // Required: grant type 必需:授权类型
ClientID string // Required: client ID 必需:客户端ID
ClientSecret string // Required: client secret 必需:客户端密钥
Code string // For authorization_code: authorization code 授权码模式:授权码
RedirectURI string // For authorization_code: redirect URI 授权码模式:回调URI
RefreshToken string // For refresh_token: refresh token 刷新令牌模式:刷新令牌
Username string // For password: username 密码模式:用户名
Password string // For password: password 密码模式:密码
Scopes []string // Optional: requested scopes 可选:请求的权限范围
}
TokenRequest Unified token request structure 统一的令牌请求结构
type UserValidator ¶
UserValidator Function type for validating user credentials 验证用户凭证的函数类型