-
Notifications
You must be signed in to change notification settings - Fork 929
/
token.go
43 lines (33 loc) · 1.01 KB
/
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
37
38
39
40
41
42
43
package v7action
import (
"strings"
"time"
"github.com/SermoDigital/jose/jws"
"github.com/SermoDigital/jose/jwt"
)
func (actor Actor) RefreshAccessToken() (string, error) {
var expiresIn time.Duration
refreshToken := actor.Config.RefreshToken()
accessTokenString := strings.TrimPrefix(actor.Config.AccessToken(), "bearer ")
token, err := jws.ParseJWT([]byte(accessTokenString))
if err == nil {
expiration, ok := token.Claims().Expiration()
if ok {
expiresIn = time.Until(expiration)
}
}
if err != nil || expiresIn < time.Minute {
tokens, err := actor.UAAClient.RefreshAccessToken(refreshToken)
if err != nil {
return "", err
}
actor.Config.SetAccessToken(tokens.AuthorizationToken())
actor.Config.SetRefreshToken(tokens.RefreshToken)
return tokens.AuthorizationToken(), nil
}
return actor.Config.AccessToken(), nil
}
func (actor Actor) ParseAccessToken(accessToken string) (jwt.JWT, error) {
tokenStr := strings.TrimPrefix(accessToken, "bearer ")
return jws.ParseJWT([]byte(tokenStr))
}