github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/jwt/token.go (about)

     1  package jwt
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/dgrijalva/jwt-go"
     7  )
     8  
     9  // AccessToken represents a token
    10  type AccessToken struct {
    11  	Type    string `json:"token_type"`
    12  	Token   string `json:"access_token"`
    13  	Expires int64  `json:"expires_in"`
    14  }
    15  
    16  // IssueAdminToken issues admin JWT for API access
    17  func IssueAdminToken(signingMethod SigningMethod, claims jwt.MapClaims, expireIn time.Duration) (*AccessToken, error) {
    18  	token := jwt.New(jwt.GetSigningMethod(signingMethod.Alg))
    19  	exp := time.Now().Add(expireIn).Unix()
    20  
    21  	token.Claims = claims
    22  	claims["exp"] = exp
    23  	claims["iat"] = time.Now().Unix()
    24  
    25  	accessToken, err := token.SignedString([]byte(signingMethod.Key))
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	// currently only HSXXX algorithms are supported for issuing admin token, so we cast key to bytes array
    31  	return &AccessToken{
    32  		Type:    "Bearer",
    33  		Token:   accessToken,
    34  		Expires: exp,
    35  	}, nil
    36  }