authentication

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthProvider

type AuthProvider struct {
	// contains filtered or unexported fields
}

AuthProvider 是认证提供者,负责创建和管理认证器

func NewAuthProvider

func NewAuthProvider() *AuthProvider

NewAuthProvider 创建认证提供者

func (*AuthProvider) GetAuthenticator

func (p *AuthProvider) GetAuthenticator(authType AuthType) (Authenticator, error)

GetAuthenticator 获取指定类型的认证器

func (*AuthProvider) GetDefaultAuthenticator

func (p *AuthProvider) GetDefaultAuthenticator() (Authenticator, error)

GetDefaultAuthenticator 获取默认认证器

func (*AuthProvider) RegisterAuthenticator

func (p *AuthProvider) RegisterAuthenticator(a Authenticator)

RegisterAuthenticator 注册认证器

func (*AuthProvider) SetDefaultAuth

func (p *AuthProvider) SetDefaultAuth(authType AuthType)

SetDefaultAuth 设置默认认证类型

func (*AuthProvider) StreamClientInterceptor

func (p *AuthProvider) StreamClientInterceptor(authType AuthType) grpc.StreamClientInterceptor

StreamClientInterceptor 创建用于拦截流RPC的客户端拦截器

func (*AuthProvider) StreamServerInterceptor

func (p *AuthProvider) StreamServerInterceptor(authType AuthType) grpc.StreamServerInterceptor

StreamServerInterceptor 创建用于拦截流RPC的服务端拦截器

func (*AuthProvider) UnaryClientInterceptor

func (p *AuthProvider) UnaryClientInterceptor(authType AuthType) grpc.UnaryClientInterceptor

UnaryClientInterceptor 创建用于拦截一元RPC的客户端拦截器

func (*AuthProvider) UnaryServerInterceptor

func (p *AuthProvider) UnaryServerInterceptor(authType AuthType) grpc.UnaryServerInterceptor

UnaryServerInterceptor 创建用于拦截一元RPC的服务端拦截器

type AuthType

type AuthType int

AuthType 表示支持的认证类型

const (
	// AuthNone 表示不需要认证
	AuthNone AuthType = iota
	// AuthBasic 表示基本认证
	AuthBasic
	// AuthJWT 表示JWT认证
	AuthJWT
	// AuthOAuth2 表示OAuth2认证
	AuthOAuth2
)

type Authenticator

type Authenticator interface {
	// Authenticate 执行认证
	Authenticate(ctx context.Context) (context.Context, error)
	// GetAuthType 返回认证类型
	GetAuthType() AuthType
	// Name 返回认证器名称
	Name() string
}

Authenticator 是认证接口

type BasicAuthCredentials

type BasicAuthCredentials struct {
	Username string
	Password string
}

BasicAuthCredentials 表示基本认证凭据

type BasicAuthenticator

type BasicAuthenticator struct {
	// contains filtered or unexported fields
}

BasicAuthenticator 是基本认证实现

func NewBasicAuthenticator

func NewBasicAuthenticator(verify func(username, password string) bool) *BasicAuthenticator

NewBasicAuthenticator 创建基本认证器

func (*BasicAuthenticator) Authenticate

func (a *BasicAuthenticator) Authenticate(ctx context.Context) (context.Context, error)

Authenticate 实现Authenticator接口

func (*BasicAuthenticator) GetAuthType

func (a *BasicAuthenticator) GetAuthType() AuthType

GetAuthType 实现Authenticator接口

func (*BasicAuthenticator) Name

func (a *BasicAuthenticator) Name() string

Name 实现Authenticator接口

type JWTAuthenticator

type JWTAuthenticator struct {
	// contains filtered or unexported fields
}

JWTAuthenticator 是JWT认证实现

func NewJWTAuthenticator

func NewJWTAuthenticator(signingKey []byte, opts ...JWTOption) *JWTAuthenticator

NewJWTAuthenticator 创建JWT认证器

func (*JWTAuthenticator) Authenticate

func (a *JWTAuthenticator) Authenticate(ctx context.Context) (context.Context, error)

Authenticate 实现Authenticator接口

func (*JWTAuthenticator) GenerateToken

func (a *JWTAuthenticator) GenerateToken(subject string, additionalClaims map[string]interface{}) (string, error)

GenerateToken 生成JWT令牌

func (*JWTAuthenticator) GetAuthType

func (a *JWTAuthenticator) GetAuthType() AuthType

GetAuthType 实现Authenticator接口

func (*JWTAuthenticator) Name

func (a *JWTAuthenticator) Name() string

Name 实现Authenticator接口

type JWTClaims

type JWTClaims struct {
	Username string `json:"username"`
	Role     string `json:"role"`
	jwt.RegisteredClaims
}

JWTClaims 表示JWT声明

type JWTOption

type JWTOption func(*JWTAuthenticator)

JWTOption 是JWTAuthenticator的选项

func WithCustomClaims

func WithCustomClaims(generator func(subject string, additionalClaims map[string]interface{}) jwt.Claims) JWTOption

WithCustomClaims 设置自定义声明生成器

func WithCustomValidation

func WithCustomValidation(validationFunc func(token *jwt.Token) (interface{}, error)) JWTOption

WithCustomValidation 设置自定义验证函数

func WithExpiration

func WithExpiration(expiration time.Duration) JWTOption

WithExpiration 设置过期时间

func WithIssuer

func WithIssuer(issuer string) JWTOption

WithIssuer 设置颁发者

func WithSigningMethod

func WithSigningMethod(method jwt.SigningMethod) JWTOption

WithSigningMethod 设置签名方法

type OAuth2Authenticator

type OAuth2Authenticator struct {
	// contains filtered or unexported fields
}

OAuth2Authenticator 是OAuth2认证实现

func NewOAuth2Authenticator

func NewOAuth2Authenticator(cfg OAuth2Config, verify func(token *oauth2.Token) bool) *OAuth2Authenticator

NewOAuth2Authenticator 创建OAuth2认证器

func (*OAuth2Authenticator) Authenticate

func (a *OAuth2Authenticator) Authenticate(ctx context.Context) (context.Context, error)

Authenticate 实现Authenticator接口

func (*OAuth2Authenticator) Exchange

func (a *OAuth2Authenticator) Exchange(ctx context.Context, code string) (*oauth2.Token, error)

Exchange 交换授权码获取令牌

func (*OAuth2Authenticator) GetAuthType

func (a *OAuth2Authenticator) GetAuthType() AuthType

GetAuthType 实现Authenticator接口

func (*OAuth2Authenticator) GetAuthURL

func (a *OAuth2Authenticator) GetAuthURL(state string) string

GetAuthURL 获取授权URL

func (*OAuth2Authenticator) Name

func (a *OAuth2Authenticator) Name() string

Name 实现Authenticator接口

func (*OAuth2Authenticator) TokenSource

func (a *OAuth2Authenticator) TokenSource(ctx context.Context, token *oauth2.Token) oauth2.TokenSource

TokenSource 获取令牌源

type OAuth2Config

type OAuth2Config struct {
	ClientID     string
	ClientSecret string
	RedirectURL  string
	Scopes       []string
	Endpoint     oauth2.Endpoint
}

OAuth2Config 表示OAuth2配置

Jump to

Keyboard shortcuts

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