token

package
v0.0.0-...-94c8c1a Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrUnauthorized                   apperror.ErrorType = "ER0001 unauthorized"
	ErrExpiredToken                   apperror.ErrorType = "ER0002 the token is expired"
	ErrTokenAlreadyRefreshed          apperror.ErrorType = "ER0003 the token is already refreshed"
	ErrRefreshTokenNotFoundInDatabase apperror.ErrorType = "ER0004 refresh token not found in database"
	ErrReadingJWTClaims               apperror.ErrorType = "ER0005 error reading jwt claims"
	ErrFetchingJWTClaims              apperror.ErrorType = "ER0006 error fetching claims"
	ErrParsingRefreshTokenWithClaims  apperror.ErrorType = "ER0007 could not parse refresh token with claims"
	ErrReadingRefreshTokenClaims      apperror.ErrorType = "ER0008 could not read refresh token claims"
)
View Source
const (
	TableName = "refresh_token"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	UserID string `json:"user_id"`
	Csrf   string `json:"csrf"`
	jwt.StandardClaims
}

type GinMiddleware

type GinMiddleware struct {
	// contains filtered or unexported fields
}

func NewGinMiddleware

func NewGinMiddleware(log logger.Logger) GinMiddleware

func (GinMiddleware) Authentication

func (g GinMiddleware) Authentication(jwt JWT) gin.HandlerFunc

func (GinMiddleware) GetAccessTokenFromHeader

func (g GinMiddleware) GetAccessTokenFromHeader(c *gin.Context) (token string, err error)

type JWT

type JWT interface {
	GenerateToken(ctx context.Context, userId string, sub string) (accessToken, refreshToken, csrfSecret string, expiresAt int64, err error)
	RenewToken(ctx context.Context, oldAccessTokenString string, oldRefreshTokenString, oldCsrfSecret string) (newAccessToken, newRefreshToken, newCsrfSecret string, expiresAt int64, userId string, err error)
	DeleteRefreshToken(ctx context.Context, sub string, jti string) error
	VerifyToken(token string) (*Claims, error)
}

func NewHS256JWT

func NewHS256JWT(ctx context.Context, cfg *configs.Config, repo Repository, refreshTokenValidTime time.Duration, accessTokenValidTime time.Duration) (JWT, error)

func NewRS256JWT

func NewRS256JWT(ctx context.Context, fileName string, repo Repository, refreshTokenValidTime time.Duration, accessTokenValidTime time.Duration) (JWT, error)

type JWTToken

type JWTToken struct {
	// contains filtered or unexported fields
}

func (*JWTToken) DeleteRefreshToken

func (t *JWTToken) DeleteRefreshToken(ctx context.Context, sub, jti string) (err error)

func (*JWTToken) GenerateToken

func (t *JWTToken) GenerateToken(ctx context.Context, userID string, sub string) (accessToken, refreshToken, csrfSecret string, expiresAt int64, err error)

func (*JWTToken) RenewToken

func (t *JWTToken) RenewToken(ctx context.Context, oldAccessTokenString string, oldRefreshTokenString, oldCsrfSecret string) (newAuthTokenString, newRefreshTokenString, newCsrfSecret string, expiresAt int64, userId string, err error)

func (*JWTToken) VerifyToken

func (t *JWTToken) VerifyToken(authToken string) (*Claims, error)

type RedisRepository

type RedisRepository struct {
	// contains filtered or unexported fields
}

func NewRedisRepository

func NewRedisRepository(addr, pass string) *RedisRepository

func (RedisRepository) DeleteRefreshToken

func (r RedisRepository) DeleteRefreshToken(ctx context.Context, jti string) error

func (RedisRepository) FindAllRefreshTokens

func (r RedisRepository) FindAllRefreshTokens(ctx context.Context) ([]RefreshToken, error)

func (RedisRepository) FindRefreshToken

func (r RedisRepository) FindRefreshToken(ctx context.Context, jti string) (sub string, err error)

func (RedisRepository) StoreRefreshToken

func (r RedisRepository) StoreRefreshToken(ctx context.Context, sub, jti string) error

type RefreshToken

type RefreshToken struct {
	Subject string `json:"subject" bson:"subject"`
	JTI     string `json:"jti" bson:"jti"`
}

type RefreshTokenClaims

type RefreshTokenClaims struct {
	Csrf string `json:"csrf"`
	jwt.StandardClaims
}

type Repository

type Repository interface {
	StoreRefreshToken(ctx context.Context, sub, jti string) error
	DeleteRefreshToken(ctx context.Context, jti string) error
	FindRefreshToken(ctx context.Context, jti string) (sub string, err error)
	FindAllRefreshTokens(ctx context.Context) ([]RefreshToken, error)
}

Jump to

Keyboard shortcuts

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