jwtauth

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultAccessTokenExpires  = time.Hour * 2
	DefaultRefreshTokenExpires = time.Hour * 24 * 30
	DefaultSigningMethod       = jwt.SigningMethodHS256
)

Functions

This section is empty.

Types

type Claims

type Claims struct {
	// The identity of this token, which can be any data that is json serializable.
	Identity interface{}

	// Every time a user authenticates by providing a username and password,
	// they receive a Fresh access token that can access any route.
	//
	// But after some time, that token should no longer be considered fresh,
	// and some critical or dangerous routes will be blocked until the user verifies their password again.
	Fresh bool

	// TokenType includes AccessTokenType and RefreshTokenType.
	TokenType TokenType

	jwt.RegisteredClaims
}

Claims is the custom claims type.

func (*Claims) GetIdentity added in v0.8.1

func (c *Claims) GetIdentity() interface{}

func (*Claims) HasExpired

func (c *Claims) HasExpired() bool

HasExpired returns true if the token has expired.

type JwtAuth

type JwtAuth struct {
	*Options
}

func New

func New(secretKey string, opts ...Option) *JwtAuth

New returns new JwtAuth.

The SecretKey is used to sign the token.

The default options are:

  • AccessTokenExpires: DefaultAccessTokenExpires
  • RefreshTokenExpires: DefaultRefreshTokenExpires
  • SigningMethod: DefaultSigningMethod

func (*JwtAuth) CreateAccessToken

func (j *JwtAuth) CreateAccessToken(identity interface{}, fresh bool) (string, error)

CreateAccessToken create a new access token. The identity of this token, which can be any data that is json serializable.

func (*JwtAuth) CreateRefreshToken

func (j *JwtAuth) CreateRefreshToken(identity interface{}) (string, error)

CreateRefreshToken create a new refresh token. The identity of this token, which can be any data that is json serializable.

func (*JwtAuth) ParseJwtToken

func (j *JwtAuth) ParseJwtToken(tokenString string) (*Claims, error)

func (*JwtAuth) ParseToken

func (j *JwtAuth) ParseToken(tokenString string) (auth.Claims, error)

ParseToken parse token string to Claims.

type Option

type Option func(*Options)

func WithAccessTokenExpires

func WithAccessTokenExpires(expires time.Duration) Option

WithAccessTokenExpires sets the expiration time of the access token.

func WithAudience

func WithAudience(audience ...string) Option

WithAudience sets the audience of the token.

func WithIssuer

func WithIssuer(issuer string) Option

WithIssuer sets the issuer of the token.

func WithRefreshTokenExpires

func WithRefreshTokenExpires(expires time.Duration) Option

WithRefreshTokenExpires sets the expiration time of the refresh token.

func WithSubject

func WithSubject(subject string) Option

WithSubject sets the subject of the token.

type Options

type Options struct {
	// SecretKey is the secret key used to sign the token.
	// It is required.
	SecretKey string

	// Issuer is the issuer of the token.
	Issuer string

	// Subject is the subject of the token.
	Subject string

	// Audience is the audience of the token.
	Audience []string

	// AccessTokenExpires is the expiration time of the access token.
	AccessTokenExpires time.Duration

	// RefreshTokenExpires is the expiration time of the refresh token.
	RefreshTokenExpires time.Duration
}

Options is a struct for specifying configuration options for the JWT.

type TokenType

type TokenType int
const (
	AccessTokenType TokenType = iota + 1
	RefreshTokenType
)

Jump to

Keyboard shortcuts

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