Documentation
¶
Index ¶
- Variables
- func GenerateState() string
- type Claims
- type JWTAuth
- func (j *JWTAuth) GenerateToken(userID, username, email string, extra ...map[string]interface{}) (string, error)
- func (j *JWTAuth) ParseToken(tokenString string) (*Claims, error)
- func (j *JWTAuth) RefreshToken(tokenString string) (string, error)
- func (j *JWTAuth) ValidateToken(tokenString string) bool
- type JWTConfig
- type OAuthConfig
- type OAuthProvider
- func (o *OAuthProvider) ExchangeToken(ctx context.Context, code string) (*OAuthToken, error)
- func (o *OAuthProvider) GetAuthURL(state string) string
- func (o *OAuthProvider) GetUserInfo(ctx context.Context, accessToken string) (map[string]interface{}, error)
- func (o *OAuthProvider) RefreshAccessToken(ctx context.Context, refreshToken string) (*OAuthToken, error)
- type OAuthToken
- type Permission
- type RBAC
- func (r *RBAC) AddPermission(perm *Permission)
- func (r *RBAC) AddRole(role *Role)
- func (r *RBAC) AssignRole(userID, roleID string)
- func (r *RBAC) GetUserPermissions(userID string) []*Permission
- func (r *RBAC) GetUserRoles(userID string) []*Role
- func (r *RBAC) HasPermission(userID, permID string) bool
- func (r *RBAC) HasRole(userID, roleID string) bool
- func (r *RBAC) RevokeRole(userID, roleID string)
- type Role
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrTokenExpired = errors.New("token has expired") ErrTokenInvalid = errors.New("token is invalid") )
View Source
var ( // GitHub OAuth 配置 GitHubOAuth = func(clientID, clientSecret, redirectURL string) OAuthConfig { return OAuthConfig{ ClientID: clientID, ClientSecret: clientSecret, RedirectURL: redirectURL, Scopes: []string{"user:email"}, AuthURL: "https://github.com/login/oauth/authorize", TokenURL: "https://github.com/login/oauth/access_token", UserInfoURL: "https://api.github.com/user", } } // Google OAuth 配置 GoogleOAuth = func(clientID, clientSecret, redirectURL string) OAuthConfig { return OAuthConfig{ ClientID: clientID, ClientSecret: clientSecret, RedirectURL: redirectURL, Scopes: []string{"openid", "profile", "email"}, AuthURL: "https://accounts.google.com/o/oauth2/v2/auth", TokenURL: "https://oauth2.googleapis.com/token", UserInfoURL: "https://www.googleapis.com/oauth2/v2/userinfo", } } // Facebook OAuth 配置 FacebookOAuth = func(clientID, clientSecret, redirectURL string) OAuthConfig { return OAuthConfig{ ClientID: clientID, ClientSecret: clientSecret, RedirectURL: redirectURL, Scopes: []string{"email", "public_profile"}, AuthURL: "https://www.facebook.com/v12.0/dialog/oauth", TokenURL: "https://graph.facebook.com/v12.0/oauth/access_token", UserInfoURL: "https://graph.facebook.com/me?fields=id,name,email", } } )
预定义的 OAuth 提供商配置
View Source
var ( ErrInvalidCode = errors.New("oauth: invalid authorization code") ErrInvalidToken = errors.New("oauth: invalid token") )
Functions ¶
Types ¶
type Claims ¶
type Claims struct {
UserID string `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Extra map[string]interface{} `json:"extra,omitempty"`
jwt.RegisteredClaims
}
Claims JWT 声明
type JWTAuth ¶
type JWTAuth struct {
// contains filtered or unexported fields
}
JWTAuth JWT 认证
func (*JWTAuth) GenerateToken ¶
func (j *JWTAuth) GenerateToken(userID, username, email string, extra ...map[string]interface{}) (string, error)
GenerateToken 生成 Token
func (*JWTAuth) ParseToken ¶
ParseToken 解析 Token
func (*JWTAuth) RefreshToken ¶
RefreshToken 刷新 Token
func (*JWTAuth) ValidateToken ¶
ValidateToken 验证 Token
type OAuthConfig ¶
type OAuthConfig struct {
ClientID string
ClientSecret string
RedirectURL string
Scopes []string
AuthURL string
TokenURL string
UserInfoURL string
}
OAuthConfig OAuth 配置
type OAuthProvider ¶
type OAuthProvider struct {
// contains filtered or unexported fields
}
OAuthProvider OAuth 提供商
func NewOAuthProvider ¶
func NewOAuthProvider(config OAuthConfig) *OAuthProvider
NewOAuthProvider 创建 OAuth 提供商
func (*OAuthProvider) ExchangeToken ¶
func (o *OAuthProvider) ExchangeToken(ctx context.Context, code string) (*OAuthToken, error)
ExchangeToken 用授权码交换 Token
func (*OAuthProvider) GetAuthURL ¶
func (o *OAuthProvider) GetAuthURL(state string) string
GetAuthURL 获取授权URL
func (*OAuthProvider) GetUserInfo ¶
func (o *OAuthProvider) GetUserInfo(ctx context.Context, accessToken string) (map[string]interface{}, error)
GetUserInfo 获取用户信息
func (*OAuthProvider) RefreshAccessToken ¶
func (o *OAuthProvider) RefreshAccessToken(ctx context.Context, refreshToken string) (*OAuthToken, error)
RefreshAccessToken 刷新访问令牌
type OAuthToken ¶
type OAuthToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
OAuthToken OAuth Token 响应
type RBAC ¶
type RBAC struct {
// contains filtered or unexported fields
}
RBAC 基于角色的访问控制
func (*RBAC) GetUserPermissions ¶
func (r *RBAC) GetUserPermissions(userID string) []*Permission
GetUserPermissions 获取用户所有权限
func (*RBAC) GetUserRoles ¶
GetUserRoles 获取用户所有角色
func (*RBAC) HasPermission ¶
HasPermission 检查用户是否有指定权限
Click to show internal directories.
Click to hide internal directories.