github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/token.go (about) 1 package v7action 2 3 import ( 4 "strings" 5 "time" 6 7 "github.com/SermoDigital/jose/jws" 8 "github.com/SermoDigital/jose/jwt" 9 ) 10 11 func (actor Actor) RefreshAccessToken() (string, error) { 12 var expiresIn time.Duration 13 14 refreshToken := actor.Config.RefreshToken() 15 16 accessTokenString := strings.TrimPrefix(actor.Config.AccessToken(), "bearer ") 17 token, err := jws.ParseJWT([]byte(accessTokenString)) 18 19 if err == nil { 20 expiration, ok := token.Claims().Expiration() 21 if ok { 22 expiresIn = time.Until(expiration) 23 } 24 } 25 26 if err != nil || expiresIn < time.Minute { 27 tokens, err := actor.UAAClient.RefreshAccessToken(refreshToken) 28 if err != nil { 29 return "", err 30 } 31 32 actor.Config.SetAccessToken(tokens.AuthorizationToken()) 33 actor.Config.SetRefreshToken(tokens.RefreshToken) 34 35 return tokens.AuthorizationToken(), nil 36 } 37 return actor.Config.AccessToken(), nil 38 } 39 40 func (actor Actor) ParseAccessToken(accessToken string) (jwt.JWT, error) { 41 tokenStr := strings.TrimPrefix(accessToken, "bearer ") 42 return jws.ParseJWT([]byte(tokenStr)) 43 }