@@ -23,37 +23,40 @@ var (
23
23
)
24
24
25
25
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 )
45
51
ansi .MoveCursorUp (6 )
46
52
ansi .ClearDown ()
53
+ if err != nil {
54
+ return err
55
+ }
56
+ } else if err := session .RefreshToken (); err != nil {
47
57
return err
48
58
}
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 {
57
60
return err
58
61
}
59
62
return nil
0 commit comments