Skip to content

Commit

Permalink
make jwt token customizable with JwtTokenOption (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
krrrr38 authored Jul 7, 2022
1 parent fdb4f61 commit 933acab
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions util/crypto/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ var (
MaxAge = time.Hour * 24 * 75 // 2.5 months
)

// JwtTokenOption used as a generate token option
type JwtTokenOption struct {
Expires time.Duration
}

// SetExpires set value
func SetExpires(expires time.Duration) {
jwtExpires = expires
Expand All @@ -49,14 +54,18 @@ func SetMaxAge(age time.Duration) {
}

// JwtToken returns jwt token with expires
func JwtToken(data interface{}) (string, error) {
func JwtToken(data interface{}, option JwtTokenOption) (string, error) {
if jwtSecret == jwtSecretDefault {
return "", xerrors.New("must be changed default secret")
}

expires := jwtExpires
if option.Expires != 0 {
expires = option.Expires
}
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"data": data,
"exp": time.Now().Add(jwtExpires).Unix(),
"exp": time.Now().Add(expires).Unix(),
"iat": time.Now().Unix(),
})
// Sign and get the complete encoded token as a string using the secret
Expand Down

0 comments on commit 933acab

Please sign in to comment.