auth

package
v4.0.0-...-0be493c Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthorizationHeader = "Authorization"
	TokenHeaderPrefix   = "Bearer "
	RevokeTokenPrefix   = "jwt_revoke_"
)
View Source
const (
	TokenHeaderPrefixCr = "Bearer Cr "
)

Variables

View Source
var (
	ErrAuthFailed        = serializer.NewError(serializer.CodeInvalidSign, "invalid sign", nil)
	ErrAuthHeaderMissing = serializer.NewError(serializer.CodeNoPermissionErr, "authorization header is missing", nil)
	ErrExpiresMissing    = serializer.NewError(serializer.CodeNoPermissionErr, "expire timestamp is missing", nil)
	ErrExpired           = serializer.NewError(serializer.CodeSignExpired, "signature expired", nil)
)
View Source
var (
	TokenTypeAccess  = TokenType("access")
	TokenTypeRefresh = TokenType("refresh")

	ErrInvalidRefreshToken = errors.New("invalid refresh token")
	ErrUserNotFound        = errors.New("user not found")
)

Functions

func CheckRequest

func CheckRequest(ctx context.Context, instance Auth, r *http.Request) error

CheckRequest 对复杂请求进行签名验证

func CheckScope

func CheckScope(c *gin.Context, requiredScopes ...string) error

func CheckURI

func CheckURI(ctx context.Context, instance Auth, url *url.URL) error

CheckURI 对URI进行鉴权

func GetScopesFromContext

func GetScopesFromContext(ctx context.Context) (bool, []string)

func RedactSensitiveValues

func RedactSensitiveValues(errorMessage string) string

func SignRequest

func SignRequest(ctx context.Context, instance Auth, r *http.Request, expires *time.Time) *http.Request

SignRequest 对PUT\POST等复杂HTTP请求签名,只会对URI部分、 请求正文、`X-Cr-`开头的header进行签名

func SignRequestDeprecated

func SignRequestDeprecated(instance Auth, r *http.Request, expires int64) *http.Request

SignRequestDeprecated 对PUT\POST等复杂HTTP请求签名,只会对URI部分、 请求正文、`X-Cr-`开头的header进行签名

func SignURI

func SignURI(ctx context.Context, instance Auth, uri string, expires *time.Time) (*url.URL, error)

SignURI 对URI进行签名

func SignURIDeprecated

func SignURIDeprecated(instance Auth, uri string, expires int64) (*url.URL, error)

SignURIDeprecated 对URI进行签名,签名只针对Path部分,query部分不做验证 Deprecated

func ValidateScopes

func ValidateScopes(requestedScopes, allowedScopes []string) bool

ValidateScopes checks if all requested scopes are a subset of the allowed scopes. Returns true if all requested scopes are valid, false otherwise.

Types

type Auth

type Auth interface {
	// 对给定Body进行签名,expires为0表示永不过期
	Sign(body string, expires int64) string
	// 对给定Body和Sign进行检查
	Check(body string, sign string) error
}

Auth 鉴权认证

var General Auth

General 通用的认证接口 Deprecated

type Claims

type Claims struct {
	TokenType TokenType `json:"token_type"`
	jwt.RegisteredClaims
	StateHash   []byte     `json:"state_hash,omitempty"`
	RootTokenID *uuid.UUID `json:"root_token_id,omitempty"`
	Scopes      []string   `json:"scopes,omitempty"`
	ClientID    string     `json:"client_id,omitempty"`
}

type HMACAuth

type HMACAuth struct {
	SecretKey []byte
}

HMACAuth HMAC算法鉴权

func (HMACAuth) Check

func (auth HMACAuth) Check(body string, sign string) error

Check 对给定Body和Sign进行鉴权,包括对expires的检查

func (HMACAuth) Sign

func (auth HMACAuth) Sign(body string, expires int64) string

Sign 对给定Body生成expires后失效的签名,expires为过期时间戳, 填写为0表示不限制有效期

type IssueTokenArgs

type IssueTokenArgs struct {
	User               *ent.User
	RootTokenID        *uuid.UUID
	ClientID           string
	Scopes             []string
	RefreshTTLOverride time.Duration
}

type ScopeContextKey

type ScopeContextKey struct{}

type Token

type Token struct {
	AccessToken    string    `json:"access_token"`
	RefreshToken   string    `json:"refresh_token"`
	AccessExpires  time.Time `json:"access_expires"`
	RefreshExpires time.Time `json:"refresh_expires"`

	UID int `json:"-"`
}

Token stores token pair for authentication

type TokenAuth

type TokenAuth interface {
	// Issue issues a new pair of credentials for the given user.
	Issue(ctx context.Context, args *IssueTokenArgs) (*Token, error)
	// VerifyAndRetrieveUser verifies the given token and inject the user into current context.
	// Returns if upper caller should continue process other session provider.
	VerifyAndRetrieveUser(c *gin.Context) (bool, error)
	// Refresh refreshes the given refresh token and returns a new pair of credentials.
	Refresh(ctx context.Context, refreshToken string) (*Token, error)
	// Claims parses the given token string and returns the claims.
	Claims(ctx context.Context, tokenStr string) (*Claims, error)
}

func NewTokenAuth

func NewTokenAuth(idEncoder hashid.Encoder, s setting.Provider, secret []byte, userClient inventory.UserClient,
	l logging.Logger, kv cache.Driver, oAuthClient inventory.OAuthClientClient) TokenAuth

NewTokenAuth creates a new token based auth provider.

type TokenIDContextKey

type TokenIDContextKey struct{}

type TokenType

type TokenType string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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