Skip to content

jwt

import "github.com/Mini-Vending/go-services/packages/jwt"

Index

Constants

const (
    AuthorizationHeader            = "authorization"
    BearerPrefix                   = "Bearer "
    TokenDataKey        contextKey = "token_data"
)

Variables

var (
    ErrorUnexpectedSigningMethod   = errors.New("unexpected signing method")
    ErrorFailedToParseToken        = errors.New("failed to parse token")
    ErrorInvalidToken              = errors.New("invalid token")
    ErrorFailedToValidateToken     = errors.New("failed to validate token")
    ErrorFailedToGenerateTokenPair = errors.New("failed to generate token pair")
    ErrorFailedToSignToken         = errors.New("failed to sign token")
    ErrorFailedToSignRefreshToken  = errors.New("failed to sign refresh token")

    GrpcErrMetadataNotFound                 = status.Error(codes.Unauthenticated, "metadata not found")
    GrpcErrAuthorizationHeaderNotFound      = status.Error(codes.Unauthenticated, "authorization header not found")
    GrpcErrInvalidAuthorizationHeaderFormat = status.Error(codes.Unauthenticated, "invalid authorization header format")
    GrpcErrTokenNotFound                    = status.Error(codes.Unauthenticated, "token not found")
    GrpcErrTokenExpired                     = status.Error(codes.Unauthenticated, "token expired")
    GrpcErrTokenInvalid                     = status.Error(codes.Unauthenticated, "invalid token")
    GrpcErrUnauthenticated                  = status.Error(codes.Unauthenticated, "unauthenticated")
)

func AuthInterceptor

func AuthInterceptor(cfg *Config, logger logger.Logger, excludePaths []string) grpc.UnaryServerInterceptor

func CheckTokenInterceptor

func CheckTokenInterceptor(logger logger.Logger, checker TokenChecker, excludePaths []string) grpc.UnaryServerInterceptor

func ExtractTokenFromContext

func ExtractTokenFromContext(ctx context.Context) (string, error)

func GetRoleFromContext

func GetRoleFromContext(ctx context.Context) (string, bool)

func GetUserIDFromContext

func GetUserIDFromContext(ctx context.Context) (string, bool)

type AccessTokenClaims

type AccessTokenClaims struct {
    jwt.RegisteredClaims
    TokenData
}

type Config

type Config struct {
    AccessSecretKey      string
    RefreshSecretKey     string
    Issuer               string
    AccessTokenDuration  time.Duration
    RefreshTokenDuration time.Duration
}

type Manager

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

func New

func New(logger logger.Logger, cfg *Config) *Manager

func (*Manager) GenerateTokenPair

func (m *Manager) GenerateTokenPair(tokenData TokenData) (accessTokenStr, refreshTokenStr string, err error)

func (*Manager) RefreshTokenPair

func (m *Manager) RefreshTokenPair(ctx context.Context, accessTokenStr string, refreshTokenStr string) (accessToken, refreshToken string, err error)

func (*Manager) ValidateToken

func (m *Manager) ValidateToken(tokenString, secretKey string, claims jwt.Claims) (token *jwt.Token, err error)

func (*Manager) ValidateTokenWithoutValidation

func (m *Manager) ValidateTokenWithoutValidation(tokenString string, secretKey string, claims jwt.Claims) (token *jwt.Token, err error)

type ManagerInterface

type ManagerInterface interface {
    GenerateTokenPair(tokenData TokenData) (string, string, error)
    ValidateToken(tokenString string, secretKey string, claims jwt.Claims) (*jwt.Token, error)
    RefreshTokenPair(ctx context.Context, accessTokenStr string, refreshTokenStr string) (string, string, error)
}

type RefreshTokenClaims

type RefreshTokenClaims struct {
    jwt.RegisteredClaims
}

type RefreshTokenData

type RefreshTokenData struct {
    AccountID int64 `json:"account_id"`
}

type TokenChecker

type TokenChecker interface {
    CheckToken(ctx context.Context, token string) (TokenData, error)
}

type TokenData

type TokenData struct {
    Email      string `json:"email"`
    Role       string `json:"role"`
    AccountID  int64  `json:"account_id"`
    MerchantID int64  `json:"merchant_id"`
}

Generated by gomarkdoc