Skip to content

Commit ecfce4b

Browse files
committed
Refreshing client token instead of reissuing another one
1 parent bf934ec commit ecfce4b

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

sdk/credentials/session.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,31 @@ func (s *Session) WaitForToken(deviceCode string) error {
326326
return nil
327327
}
328328

329+
func (s *Session) RefreshToken() error {
330+
options := ssooidc.Options{Region: s.Region}
331+
client := ssooidc.New(options)
332+
token, err := client.CreateToken(context.TODO(), &ssooidc.CreateTokenInput{
333+
ClientId: aws.String(s.ClientCredentials.ClientId),
334+
ClientSecret: aws.String(s.ClientCredentials.ClientSecret),
335+
RefreshToken: aws.String(s.ClientToken.RefreshToken),
336+
GrantType: aws.String("refresh_token"),
337+
})
338+
if err != nil {
339+
return err
340+
}
341+
s.ClientToken = &ClientToken{
342+
AccessToken: aws.ToString(token.AccessToken),
343+
ClientId: s.ClientCredentials.ClientId,
344+
ClientSecret: s.ClientCredentials.ClientSecret,
345+
ExpiresAt: time.Now().Add(time.Duration(token.ExpiresIn) * time.Second).UTC(),
346+
RefreshToken: aws.ToString(token.RefreshToken),
347+
Region: s.Region,
348+
RegistrationExpiresAt: s.ClientCredentials.ExpiresAt,
349+
StartUrl: s.StartUrl,
350+
}
351+
return nil
352+
}
353+
329354
func (s *Session) GetAccounts() (Accounts, error) {
330355
accounts := Accounts{}
331356
options := sso.Options{Region: s.Region}

sdk/tui/tui.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,40 @@ var (
2323
)
2424

2525
func ClientLogin(session *credentials.Session) error {
26-
if err := session.RegisterClient(); err != nil {
27-
return err
28-
}
29-
userCode, deviceCode, url, urlFull, err := session.StartDeviceAuthorization()
30-
if err != nil {
31-
return err
32-
}
33-
yellow := color.ToForeground(YellowColor).Decorator()
34-
gray := color.ToForeground(LightGrayColor).Decorator()
35-
title := TitleStyle.Decorator()
36-
DefaultStyle.Printfln("")
37-
DefaultStyle.Printfln("%s %s", title("SSO Session: "), gray(session.Name))
38-
DefaultStyle.Printfln("%s %s", title("SSO Start URL: "), gray(session.StartUrl))
39-
DefaultStyle.Printfln("%s %s", title("Authorization URL:"), gray(url))
40-
DefaultStyle.Printfln("%s %s", title("Device Code: "), yellow(userCode))
41-
DefaultStyle.Printfln("")
42-
DefaultStyle.Printf("Waiting for authorization to complete...")
43-
err = browser.OpenURL(urlFull)
44-
if err != nil {
26+
if session.ClientCredentials.IsExpired() {
27+
if err := session.RegisterClient(); err != nil {
28+
return err
29+
}
30+
userCode, deviceCode, url, urlFull, err := session.StartDeviceAuthorization()
31+
if err != nil {
32+
return err
33+
}
34+
yellow := color.ToForeground(YellowColor).Decorator()
35+
gray := color.ToForeground(LightGrayColor).Decorator()
36+
title := TitleStyle.Decorator()
37+
DefaultStyle.Printfln("")
38+
DefaultStyle.Printfln("%s %s", title("SSO Session: "), gray(session.Name))
39+
DefaultStyle.Printfln("%s %s", title("SSO Start URL: "), gray(session.StartUrl))
40+
DefaultStyle.Printfln("%s %s", title("Authorization URL:"), gray(url))
41+
DefaultStyle.Printfln("%s %s", title("Device Code: "), yellow(userCode))
42+
DefaultStyle.Printfln("")
43+
DefaultStyle.Printf("Waiting for authorization to complete...")
44+
err = browser.OpenURL(urlFull)
45+
if err != nil {
46+
ansi.MoveCursorUp(6)
47+
ansi.ClearDown()
48+
return err
49+
}
50+
err = session.WaitForToken(deviceCode)
4551
ansi.MoveCursorUp(6)
4652
ansi.ClearDown()
53+
if err != nil {
54+
return err
55+
}
56+
} else if err := session.RefreshToken(); err != nil {
4757
return err
4858
}
49-
err = session.WaitForToken(deviceCode)
50-
ansi.MoveCursorUp(6)
51-
ansi.ClearDown()
52-
if err != nil {
53-
return err
54-
}
55-
err = session.Save()
56-
if err != nil {
59+
if err := session.Save(); err != nil {
5760
return err
5861
}
5962
return nil

0 commit comments

Comments
 (0)