Documentation
¶
Index ¶
- Variables
- func AuthenticateRefreshJWT(next http.Handler) http.Handler
- func Authenticator(next http.Handler) http.Handler
- func ErrUnauthorized(err error) render.Renderer
- func ParseStructToMap(c any) (map[string]any, error)
- func RefreshTokenFromCtx(ctx context.Context) string
- type AppClaims
- type CommonClaims
- type ErrResponse
- type RefreshClaims
- type Token
- type TokenAuth
- func (a *TokenAuth) CreateJWT(c AppClaims) (string, error)
- func (a *TokenAuth) CreateRefreshJWT(c RefreshClaims) (string, error)
- func (a *TokenAuth) GenTokenPair(accessClaims AppClaims, refreshClaims RefreshClaims) (string, string, error)
- func (a *TokenAuth) Verifier() func(http.Handler) http.Handler
Constants ¶
This section is empty.
Variables ¶
var ( ErrTokenExpired = errors.New("token expired") ErrInvalidAccessToken = errors.New("invalid access token") ErrInvalidRefreshToken = errors.New("invalid refresh token") ErrWeakSecret = errors.New("JWT secret uses a known default value — set AUTH_JWT_SECRET: openssl rand -base64 64") ErrSecretTooShort = errors.New("JWT secret is too short — must be at least 32 characters") )
The list of jwt token errors presented to the end user.
Functions ¶
func AuthenticateRefreshJWT ¶
AuthenticateRefreshJWT checks validity of refresh tokens and is only used for access token refresh and logout requests. It responds with 401 Unauthorized for invalid or expired refresh tokens.
func Authenticator ¶
Authenticator is a default authentication middleware to enforce access from the Verifier middleware request context values. The Authenticator sends a 401 Unauthorized response for any unverified tokens and passes the good ones through.
func ErrUnauthorized ¶
ErrUnauthorized renders status 401 Unauthorized with custom error message.
func RefreshTokenFromCtx ¶
RefreshTokenFromCtx retrieves the parsed refresh token from context.
Types ¶
type AppClaims ¶
type AppClaims struct {
ID int `json:"id,omitempty"`
Sub string `json:"sub,omitempty"`
Roles []string `json:"roles,omitempty"`
CommonClaims
}
AppClaims represent the claims parsed from JWT access token.
func ClaimsFromCtx ¶
ClaimsFromCtx retrieves the parsed AppClaims from request context.
type CommonClaims ¶
type ErrResponse ¶
type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
}
ErrResponse renderer type for handling all sorts of errors.
func (*ErrResponse) Render ¶
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error
Render sets the application-specific error code in AppCode.
type RefreshClaims ¶
type RefreshClaims struct {
ID int `json:"id,omitempty"`
Token string `json:"token,omitempty"`
CommonClaims
}
RefreshClaims represents the claims parsed from JWT refresh token.
func (*RefreshClaims) ParseClaims ¶
func (c *RefreshClaims) ParseClaims(claims map[string]any) error
ParseClaims parses the JWT claims into RefreshClaims.
type Token ¶
type Token struct {
ID int `bun:"id,pk,autoincrement" json:"id,omitempty"`
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at,omitempty"`
UpdatedAt time.Time `bun:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at,omitempty"`
AccountID int `bun:"account_id,notnull" json:"-"`
Token string `bun:"token,notnull" json:"-"`
Expiry time.Time `bun:"expiry,notnull" json:"-"`
Mobile bool `bun:"mobile,notnull" json:"mobile"`
Identifier string `bun:"identifier" json:"identifier,omitempty"`
}
Token holds refresh jwt information.
func (*Token) BeforeInsert ¶
BeforeInsert hook executed before database insert operation.
func (*Token) BeforeUpdate ¶
BeforeUpdate hook executed before database update operation.
func (*Token) Claims ¶
func (t *Token) Claims() RefreshClaims
Claims returns the token claims to be signed
type TokenAuth ¶
type TokenAuth struct {
JwtAuth *jwtauth.JWTAuth
JwtExpiry time.Duration
JwtRefreshExpiry time.Duration
}
TokenAuth implements JWT authentication flow.
func NewTokenAuth ¶
NewTokenAuth configures and returns a JWT authentication instance.
func (*TokenAuth) CreateRefreshJWT ¶
func (a *TokenAuth) CreateRefreshJWT(c RefreshClaims) (string, error)
CreateRefreshJWT returns a refresh token for provided token Claims.
func (*TokenAuth) GenTokenPair ¶
func (a *TokenAuth) GenTokenPair(accessClaims AppClaims, refreshClaims RefreshClaims) (string, string, error)
GenTokenPair returns both an access token and a refresh token.