forked from motiv-labs/janus
/
token.go
36 lines (29 loc) · 893 Bytes
/
token.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
package jwt
import (
"time"
"github.com/dgrijalva/jwt-go"
)
// AccessToken represents a token
type AccessToken struct {
Type string `json:"token_type"`
Token string `json:"access_token"`
Expires int64 `json:"expires_in"`
}
// IssueAdminToken issues admin JWT for API access
func IssueAdminToken(signingMethod SigningMethod, claims jwt.MapClaims, expireIn time.Duration) (*AccessToken, error) {
token := jwt.New(jwt.GetSigningMethod(signingMethod.Alg))
exp := time.Now().Add(expireIn).Unix()
token.Claims = claims
claims["exp"] = exp
claims["iat"] = time.Now().Unix()
accessToken, err := token.SignedString([]byte(signingMethod.Key))
if err != nil {
return nil, err
}
// currently only HSXXX algorithms are supported for issuing admin token, so we cast key to bytes array
return &AccessToken{
Type: "Bearer",
Token: accessToken,
Expires: exp,
}, nil
}