Documentation
¶
Index ¶
- Variables
- func GenerateToken(uid string, opts ...GenerateTokenOption) (jwtID string, tokenStr string, err error)
- func RefreshToken(tokenString string, opts ...RefreshTokenOption) (jwtID string, tokenStr string, err error)
- type Claims
- func (c *Claims) Get(key string) (val interface{}, isExist bool)
- func (c *Claims) GetBool(key string) (b bool, isExist bool)
- func (c *Claims) GetFloat64(key string) (float64, bool)
- func (c *Claims) GetInt(key string) (int, bool)
- func (c *Claims) GetInt64(key string) (uint64, bool)
- func (c *Claims) GetString(key string) (string, bool)
- func (c *Claims) NewToken(d time.Duration, signMethod jwt.SigningMethod, signKey []byte) (string, error)
- type GenerateTokenOption
- type GenerateTwoTokensOption
- func WithGenerateTwoTokensAccessTokenClaims(opts ...RegisteredClaimsOption) GenerateTwoTokensOption
- func WithGenerateTwoTokensFields(fields map[string]interface{}) GenerateTwoTokensOption
- func WithGenerateTwoTokensRefreshTokenClaims(opts ...RegisteredClaimsOption) GenerateTwoTokensOption
- func WithGenerateTwoTokensSignKey(key []byte) GenerateTwoTokensOption
- func WithGenerateTwoTokensSignMethod(sm jwt.SigningMethod) GenerateTwoTokensOption
- type RefreshTokenOption
- type RefreshTwoTokensOption
- type RegisteredClaimsOption
- func WithAudience(audience ...string) RegisteredClaimsOption
- func WithDeadline(expiresAt time.Time) RegisteredClaimsOption
- func WithExpires(d time.Duration) RegisteredClaimsOption
- func WithIssuedAt(issuedAt time.Time) RegisteredClaimsOption
- func WithIssuer(issuer string) RegisteredClaimsOption
- func WithJwtID(id string) RegisteredClaimsOption
- func WithNotBefore(notBefore time.Time) RegisteredClaimsOption
- func WithSubject(subject string) RegisteredClaimsOption
- type SigningMethodHMAC
- type Tokens
- type ValidateTokenOption
Constants ¶
This section is empty.
Variables ¶
var ( HS256 = jwt.SigningMethodHS256 HS384 = jwt.SigningMethodHS384 HS512 = jwt.SigningMethodHS512 )
var (
ErrTokenExpired = jwt.ErrTokenExpired
)
Functions ¶
func GenerateToken ¶
func GenerateToken(uid string, opts ...GenerateTokenOption) (jwtID string, tokenStr string, err error)
GenerateToken create token by uid and name, use universal Claims
func RefreshToken ¶
func RefreshToken(tokenString string, opts ...RefreshTokenOption) (jwtID string, tokenStr string, err error)
RefreshToken refresh token
Types ¶
type Claims ¶
type Claims struct { UID string `json:"uid,omitempty"` // user id Fields map[string]interface{} `json:"fields,omitempty"` // custom fields jwt.RegisteredClaims }
Claims universal claims
func GetClaimsUnverified ¶ added in v1.13.0
GetClaimsUnverified get claims from token, not verifying signature
func ValidateToken ¶ added in v1.13.0
func ValidateToken(tokenString string, opts ...ValidateTokenOption) (*Claims, error)
ValidateToken validate token, return error if token is invalid
func (*Claims) GetBool ¶ added in v1.13.0
GetBool custom field value by key, if not found, return false
func (*Claims) GetFloat64 ¶ added in v1.13.0
GetFloat64 custom field value by key, if not found, return false
func (*Claims) GetInt ¶ added in v1.13.0
GetInt custom field value by key, if not found, return false
func (*Claims) GetInt64 ¶ added in v1.13.0
GetInt64 custom field value by key, if not found, return false
type GenerateTokenOption ¶ added in v1.13.0
type GenerateTokenOption func(*generateTokenOptions)
GenerateTokenOption set the jwt options.
func WithGenerateTokenClaims ¶ added in v1.13.0
func WithGenerateTokenClaims(opts ...RegisteredClaimsOption) GenerateTokenOption
WithGenerateTokenClaims set token claims value
func WithGenerateTokenFields ¶ added in v1.13.0
func WithGenerateTokenFields(fields map[string]interface{}) GenerateTokenOption
WithGenerateTokenFields set custom fields value
func WithGenerateTokenSignKey ¶ added in v1.13.0
func WithGenerateTokenSignKey(key []byte) GenerateTokenOption
WithGenerateTokenSignKey set sign key value
func WithGenerateTokenSignMethod ¶ added in v1.13.0
func WithGenerateTokenSignMethod(sm jwt.SigningMethod) GenerateTokenOption
WithGenerateTokenSignMethod set sign method value
type GenerateTwoTokensOption ¶ added in v1.13.0
type GenerateTwoTokensOption func(*generateTwoTokensOptions)
GenerateTwoTokensOption set the jwt options.
func WithGenerateTwoTokensAccessTokenClaims ¶ added in v1.13.0
func WithGenerateTwoTokensAccessTokenClaims(opts ...RegisteredClaimsOption) GenerateTwoTokensOption
WithGenerateTwoTokensAccessTokenClaims set Access token claims value
func WithGenerateTwoTokensFields ¶ added in v1.13.0
func WithGenerateTwoTokensFields(fields map[string]interface{}) GenerateTwoTokensOption
WithGenerateTwoTokensFields set custom fields value
func WithGenerateTwoTokensRefreshTokenClaims ¶ added in v1.13.0
func WithGenerateTwoTokensRefreshTokenClaims(opts ...RegisteredClaimsOption) GenerateTwoTokensOption
WithGenerateTwoTokensRefreshTokenClaims set refresh token claims value
func WithGenerateTwoTokensSignKey ¶ added in v1.13.0
func WithGenerateTwoTokensSignKey(key []byte) GenerateTwoTokensOption
WithGenerateTwoTokensSignKey set sign key value
func WithGenerateTwoTokensSignMethod ¶ added in v1.13.0
func WithGenerateTwoTokensSignMethod(sm jwt.SigningMethod) GenerateTwoTokensOption
WithGenerateTwoTokensSignMethod set sign method value
type RefreshTokenOption ¶ added in v1.13.0
type RefreshTokenOption func(*refreshTokenOptions)
RefreshTokenOption set refresh token options.
func WithRefreshTokenExpire ¶ added in v1.13.0
func WithRefreshTokenExpire(expire time.Duration) RefreshTokenOption
WithRefreshTokenExpire set expire value
func WithRefreshTokenSignKey ¶ added in v1.13.0
func WithRefreshTokenSignKey(key []byte) RefreshTokenOption
WithRefreshTokenSignKey set sign key value
type RefreshTwoTokensOption ¶ added in v1.13.0
type RefreshTwoTokensOption func(*refreshTwoTokensOptions)
RefreshTwoTokensOption set refresh token options.
func WithRefreshTwoTokensAccessTokenExpires ¶ added in v1.13.0
func WithRefreshTwoTokensAccessTokenExpires(d time.Duration) RefreshTwoTokensOption
WithRefreshTwoTokensAccessTokenExpires set access token expire value
func WithRefreshTwoTokensRefreshTokenExpires ¶ added in v1.13.0
func WithRefreshTwoTokensRefreshTokenExpires(d time.Duration) RefreshTwoTokensOption
WithRefreshTwoTokensRefreshTokenExpires set refresh token expire value
func WithRefreshTwoTokensSignKey ¶ added in v1.13.0
func WithRefreshTwoTokensSignKey(key []byte) RefreshTwoTokensOption
WithRefreshTwoTokensSignKey set sign key value
type RegisteredClaimsOption ¶ added in v1.13.0
type RegisteredClaimsOption func(*registeredClaimsOptions)
RegisteredClaimsOption set the registered claims options.
func WithAudience ¶ added in v1.13.0
func WithAudience(audience ...string) RegisteredClaimsOption
WithAudience set audience (aud) value
func WithDeadline ¶ added in v1.13.0
func WithDeadline(expiresAt time.Time) RegisteredClaimsOption
WithDeadline set expires (exp) value
func WithExpires ¶ added in v1.13.0
func WithExpires(d time.Duration) RegisteredClaimsOption
WithExpires set expires (exp) value
func WithIssuedAt ¶ added in v1.13.0
func WithIssuedAt(issuedAt time.Time) RegisteredClaimsOption
WithIssuedAt set issued at (iat) value
func WithIssuer ¶
func WithIssuer(issuer string) RegisteredClaimsOption
WithIssuer set issuer (iss) value
func WithJwtID ¶ added in v1.13.0
func WithJwtID(id string) RegisteredClaimsOption
WithJwtID set jwt id (jti) value
func WithNotBefore ¶ added in v1.13.0
func WithNotBefore(notBefore time.Time) RegisteredClaimsOption
WithNotBefore set not before (nbf) value
func WithSubject ¶ added in v1.13.0
func WithSubject(subject string) RegisteredClaimsOption
WithSubject set subject (sub) value
type SigningMethodHMAC ¶ added in v1.13.2
type SigningMethodHMAC = jwt.SigningMethodHMAC
type Tokens ¶ added in v1.13.0
type Tokens struct { RefreshToken string `json:"refreshToken"` AccessToken string `json:"accessToken"` JwtID string `json:"jwtID"` // used to prevent replay attacks, identifying specific tokens }
func GenerateTwoTokens ¶ added in v1.13.0
func GenerateTwoTokens(uid string, opts ...GenerateTwoTokensOption) (*Tokens, error)
GenerateTwoTokens create accessToken and refreshToken
func RefreshTwoTokens ¶ added in v1.13.0
func RefreshTwoTokens(refreshToken string, accessToken string, opts ...RefreshTwoTokensOption) (*Tokens, error)
RefreshTwoTokens refresh access token, if refresh token is expired time is less than 3 hours, will auto refresh token too. if return err is ErrTokenExpired, you need to login again to get token.
type ValidateTokenOption ¶ added in v1.13.0
type ValidateTokenOption func(*validateTokenOptions)
ValidateTokenOption set parse token options.
func WithValidateTokenSignKey ¶ added in v1.13.0
func WithValidateTokenSignKey(key []byte) ValidateTokenOption
WithValidateTokenSignKey set sign key value
Directories
¶
Path | Synopsis |
---|---|
Package jwt is deprecated, old package path is "github.com/go-dev-frame/sponge/pkg/jwt/old_jwt" Please use new jwt package instead, new package path is "github.com/go-dev-frame/sponge/pkg/jwt"
|
Package jwt is deprecated, old package path is "github.com/go-dev-frame/sponge/pkg/jwt/old_jwt" Please use new jwt package instead, new package path is "github.com/go-dev-frame/sponge/pkg/jwt" |