-
Notifications
You must be signed in to change notification settings - Fork 2
/
credentials_jwt.go
38 lines (33 loc) · 975 Bytes
/
credentials_jwt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package goauth
import (
"strings"
"time"
jwt "github.com/golang-jwt/jwt/v5"
)
const (
SigningMethodES256 = "ES256"
SigningMethodES384 = "ES384"
SigningMethodES512 = "ES512"
SigningMethodHS256 = "HS256"
SigningMethodHS384 = "HS384"
SigningMethodHS512 = "HS512"
)
type CredentialsJWT struct {
Issuer string `json:"issuer,omitempty"`
PrivateKey string `json:"privateKey,omitempty"`
SigningMethod string `json:"signingMethod,omitempty"`
}
func (jc *CredentialsJWT) StandardToken(tokenDuration time.Duration) (*jwt.Token, string, error) {
stdClaims := jwt.RegisteredClaims{}
if len(jc.Issuer) > 0 {
stdClaims.Issuer = jc.Issuer
}
if tokenDuration > 0 {
stdClaims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(tokenDuration))
}
token := jwt.NewWithClaims(
jwt.GetSigningMethod(strings.ToUpper(strings.TrimSpace(jc.SigningMethod))),
stdClaims)
tokenString, err := token.SignedString([]byte(jc.PrivateKey))
return token, tokenString, err
}