Documentation
¶
Overview ¶
Package auth provides JWT authentication middleware for gin.
Index ¶
- Constants
- Variables
- func Auth(opts ...AuthOption) gin.HandlerFunc
- func DecodeSignedCookie(secretKeyBase string, decodedCookie string, cookieName string) (map[string]any, error)
- func GenerateToken(uid string, opts ...GenerateTokenOption) (string, error)
- func GetClaims(c *gin.Context) (*jwt.Claims, bool)
- func InitAuth(signingKey []byte, expire time.Duration, opts ...InitAuthOption)
- func ParseToken(token string) (*jwt.Claims, error)
- func RefreshToken(claims *jwt.Claims) (string, error)
- func UserIDFromSession(session map[string]any) (any, bool)
- type AuthOption
- type Claims
- type ExtraVerifyFn
- type GenerateTokenOption
- type InitAuthOption
- type SigningMethodHMAC
Constants ¶
const HeaderAuthorizationKey = "Authorization"
HeaderAuthorizationKey http header authorization key, value is "Bearer token"
Variables ¶
Functions ¶
func Auth ¶
func Auth(opts ...AuthOption) gin.HandlerFunc
Auth authorization middleware, support custom extra verify.
func DecodeSignedCookie ¶ added in v1.15.3
func DecodeSignedCookie(secretKeyBase string, decodedCookie string, cookieName string) (map[string]any, error)
DecodeSignedCookie decrypts a Rails 7.1+ encrypted cookie using the provided secretKeyBase and validates that its purpose matches the given cookieName. It returns the decoded session payload (the JSON contained in _rails.message).
The Rails encrypted cookie format is: base64(data)--base64(iv)--base64(authTag) Key derivation: PBKDF2-HMAC-SHA256(secret_key_base, "authenticated encrypted cookie", 1000, 32) Cipher: AES-256-GCM, AAD: empty
func GenerateToken ¶
func GenerateToken(uid string, opts ...GenerateTokenOption) (string, error)
GenerateToken generates a jwt token with the given uid and options.
func InitAuth ¶
func InitAuth(signingKey []byte, expire time.Duration, opts ...InitAuthOption)
InitAuth initializes jwt options.
func ParseToken ¶
ParseToken parses the given token and returns the claims.
func RefreshToken ¶
RefreshToken create a new token with the given claims.
Types ¶
type AuthOption ¶
type AuthOption func(*authOptions)
AuthOption set the auth options.
func WithExtraVerify ¶
func WithExtraVerify(fn ExtraVerifyFn) AuthOption
WithExtraVerify set extra verify function
func WithReturnErrReason ¶
func WithReturnErrReason() AuthOption
WithReturnErrReason set return error reason
type ExtraVerifyFn ¶
ExtraVerifyFn extra verify function
type GenerateTokenOption ¶
type GenerateTokenOption func(*generateTokenOptions)
GenerateTokenOption set the jwt options.
func WithGenerateTokenFields ¶
func WithGenerateTokenFields(fields map[string]interface{}) GenerateTokenOption
WithGenerateTokenFields set custom fields value
type InitAuthOption ¶
type InitAuthOption func(*initAuthOptions)
InitAuthOption set the jwt initAuthOptions.
func WithInitAuthIssuer ¶
func WithInitAuthIssuer(issuer string) InitAuthOption
WithInitAuthIssuer set issuer value
func WithInitAuthSigningMethod ¶
func WithInitAuthSigningMethod(sm *jwt.SigningMethodHMAC) InitAuthOption
WithInitAuthSigningMethod set signing method value
type SigningMethodHMAC ¶
type SigningMethodHMAC = jwt.SigningMethodHMAC