-
Notifications
You must be signed in to change notification settings - Fork 929
/
oauth_token_command.go
78 lines (62 loc) · 1.99 KB
/
oauth_token_command.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package v6
import (
"errors"
"time"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/api/uaa/constant"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/v6/shared"
"github.com/SermoDigital/jose/jwt"
)
//go:generate counterfeiter . OauthTokenActor
type OauthTokenActor interface {
RefreshAccessToken(refreshToken string) (string, error)
ParseAccessToken(accessToken string) (jwt.JWT, error)
}
type OauthTokenCommand struct {
usage interface{} `usage:"CF_NAME oauth-token"`
relatedCommands interface{} `related_commands:"curl"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor OauthTokenActor
}
func (cmd *OauthTokenCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
return nil
}
func (cmd OauthTokenCommand) Execute(_ []string) error {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
if cmd.Config.UAAGrantType() == string(constant.GrantTypeClientCredentials) && cmd.Config.UAAOAuthClientSecret() == "" {
token, err := cmd.Actor.ParseAccessToken(cmd.Config.AccessToken())
if err != nil {
return errors.New(cmd.UI.TranslateText("Access token is invalid."))
}
expiration, success := token.Claims().Expiration()
if !success {
return errors.New(cmd.UI.TranslateText("Access token is missing expiration claim."))
}
if expiration.Before(time.Now()) {
return errors.New(cmd.UI.TranslateText("Access token has expired."))
}
cmd.UI.DisplayText(cmd.Config.AccessToken())
return nil
}
accessToken, err := cmd.Actor.RefreshAccessToken(cmd.Config.RefreshToken())
if err != nil {
return err
}
cmd.UI.DisplayText(accessToken)
return nil
}