github.com/Aoi-hosizora/ahlib-more@v1.5.1-0.20230404072844-256112befaf6/xjwt/xjwt.go (about)

     1  package xjwt
     2  
     3  import (
     4  	"github.com/golang-jwt/jwt/v4"
     5  )
     6  
     7  // GenerateToken generates jwt token using given jwt.Claims, secret and jwt.SigningMethod.
     8  func GenerateToken(method jwt.SigningMethod, claims jwt.Claims, key interface{}) (string, error) {
     9  	tokenObj := jwt.NewWithClaims(method, claims)
    10  	token, err := tokenObj.SignedString(key)
    11  	if err != nil {
    12  		return "", err
    13  	}
    14  	return token, nil
    15  }
    16  
    17  // GenerateTokenWithHS256 generates token using given jwt.Claims, secret and HS256 (HMAC SHA256, jwt.SigningMethodHS256) signing method.
    18  func GenerateTokenWithHS256(claims jwt.Claims, secret []byte) (string, error) {
    19  	return GenerateToken(jwt.SigningMethodHS256, claims, secret)
    20  }
    21  
    22  // GenerateTokenWithHS384 generates token using given jwt.Claims, secret and HS384 (HMAC SHA384, jwt.SigningMethodHS384) signing method.
    23  func GenerateTokenWithHS384(claims jwt.Claims, secret []byte) (string, error) {
    24  	return GenerateToken(jwt.SigningMethodHS384, claims, secret)
    25  }
    26  
    27  // GenerateTokenWithHS512 generates token using given jwt.Claims, secret and HS512 (HMAC SHA512, jwt.SigningMethodHS512) signing method.
    28  func GenerateTokenWithHS512(claims jwt.Claims, secret []byte) (string, error) {
    29  	return GenerateToken(jwt.SigningMethodHS512, claims, secret)
    30  }
    31  
    32  // ParseToken parses jwt token string to jwt.Token using given jwt.Claims and secret.
    33  func ParseToken(signedToken string, secret []byte, claims jwt.Claims, options ...jwt.ParserOption) (*jwt.Token, error) {
    34  	keyFunc := func(token *jwt.Token) (interface{}, error) {
    35  		return secret, nil
    36  	}
    37  	tokenObj, err := jwt.ParseWithClaims(signedToken, claims, keyFunc, options...)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return tokenObj, nil
    42  }
    43  
    44  // ParseTokenClaims parses jwt token string to jwt.Claims using given jwt.Claims and secret.
    45  func ParseTokenClaims(signedToken string, secret []byte, claims jwt.Claims, options ...jwt.ParserOption) (jwt.Claims, error) {
    46  	tokenObj, err := ParseToken(signedToken, secret, claims, options...)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return tokenObj.Claims, nil
    51  }
    52  
    53  // CheckValidationError returns true if given error is jwt.ValidationError with given flag.
    54  func CheckValidationError(err error, flag uint32) bool {
    55  	// Here DO NOT use jwt.ValidationError.Is to check error
    56  	ve, ok := err.(*jwt.ValidationError)
    57  	return ok && (ve.Errors&flag != 0)
    58  }
    59  
    60  // IsAudienceError checks error is an AUD (Audience) validation error.
    61  func IsAudienceError(err error) bool {
    62  	return CheckValidationError(err, jwt.ValidationErrorAudience) // AUD
    63  }
    64  
    65  // IsExpiredError checks error is an EXP (Expires at) validation error.
    66  func IsExpiredError(err error) bool {
    67  	return CheckValidationError(err, jwt.ValidationErrorExpired) // EXP
    68  }
    69  
    70  // IsIdError checks error is a JTI (Id) validation error.
    71  func IsIdError(err error) bool {
    72  	return CheckValidationError(err, jwt.ValidationErrorId) // JTI
    73  }
    74  
    75  // IsIssuedAtError checks error is an IAT (Issued at) validation error.
    76  func IsIssuedAtError(err error) bool {
    77  	return CheckValidationError(err, jwt.ValidationErrorIssuedAt) // IAT
    78  }
    79  
    80  // IsIssuerError checks error is an ISS (Issuer) validation error.
    81  func IsIssuerError(err error) bool {
    82  	return CheckValidationError(err, jwt.ValidationErrorIssuer) // ISS
    83  }
    84  
    85  // IsNotValidYetError checks error is a NBF (Not before) validation error.
    86  func IsNotValidYetError(err error) bool {
    87  	return CheckValidationError(err, jwt.ValidationErrorNotValidYet) // NBF
    88  }
    89  
    90  // // IsSubjectError checks error is a SUB (Subject) validation error.
    91  // func IsSubjectError(err error) bool {
    92  // 	return CheckValidationError(err, jwt.ValidationErrorSubject) // SUB, no need to check subject error
    93  // }
    94  
    95  // IsTokenInvalidError checks error is an invalid token (could not be parsed) error.
    96  func IsTokenInvalidError(err error) bool {
    97  	return CheckValidationError(err, jwt.ValidationErrorMalformed|jwt.ValidationErrorUnverifiable|jwt.ValidationErrorSignatureInvalid)
    98  }
    99  
   100  // IsClaimsInvalidError checks error is a generic claims validation error.
   101  func IsClaimsInvalidError(err error) bool {
   102  	return CheckValidationError(err, jwt.ValidationErrorClaimsInvalid)
   103  }