@@ -3,6 +3,8 @@ package login
33import (
44 "context"
55 "fmt"
6+ "os"
7+ "os/user"
68 "time"
79
810 "github.com/cli/browser"
@@ -33,9 +35,9 @@ func (opts *LoginOptions) startLoginFlow(cfg *config.CLIConfig) error {
3335 return err
3436 }
3537
36- // Fetch the JWT using the device registration resonse
37- var jwtData * auth.JWT
38- jwtData , opts .AuthTimedOut , err = fetchJWT (ctx , deviceRegistrationResponse )
38+ // Fetch the PAT using the device registration resonse
39+ var tokenData * auth.PAT
40+ tokenData , opts .AuthTimedOut , err = fetchPAT (ctx , deviceRegistrationResponse )
3941 if err != nil {
4042 return err
4143 }
@@ -47,11 +49,9 @@ func (opts *LoginOptions) startLoginFlow(cfg *config.CLIConfig) error {
4749
4850 // Storing the useful data for future reference and usage
4951 // in a global config object (Cfg)
50- cfg .User = jwtData .Payload .Email
51- cfg .Token = jwtData .Token
52- cfg .RefreshToken = jwtData .Refreshtoken
53- cfg .RefreshTokenExpiresIn = time .Unix (jwtData .RefreshExpiresIn , 0 )
54- cfg .SetTokenExpiry (jwtData .Payload .Exp )
52+ cfg .User = tokenData .User .Email
53+ cfg .Token = tokenData .Token
54+ cfg .SetTokenExpiry (tokenData .Expiry )
5555
5656 // Having stored the data in the global Cfg object, write it into the config file present in the local filesystem
5757 err = cfg .WriteFile ()
@@ -79,11 +79,30 @@ func registerDevice(ctx context.Context) (*auth.Device, error) {
7979 return res , nil
8080}
8181
82- func fetchJWT (ctx context.Context , deviceRegistrationData * auth.Device ) (* auth.JWT , bool , error ) {
83- var jwtData * auth.JWT
82+ func fetchPAT (ctx context.Context , deviceRegistrationData * auth.Device ) (* auth.PAT , bool , error ) {
83+ var tokenData * auth.PAT
8484 var err error
85+ defaultUserName := "user"
86+ defaultHostName := "host"
87+ userName := ""
8588 authTimedOut := true
8689
90+ /* ======================================================================= */
91+ // The username and hostname to add in the description for the PAT request
92+ /* ======================================================================= */
93+ userData , err := user .Current ()
94+ if err != nil {
95+ userName = defaultUserName
96+ } else {
97+ userName = userData .Username
98+ }
99+
100+ hostName , err := os .Hostname ()
101+ if err != nil {
102+ hostName = defaultHostName
103+ }
104+ userDescription := fmt .Sprintf ("CLI PAT for %s@%s" , userName , hostName )
105+
87106 // Fetching DeepSource client in order to interact with SDK
88107 deepsource , err := deepsource .New (deepsource.ClientOpts {
89108 Token : config .Cfg .Token ,
@@ -97,10 +116,10 @@ func fetchJWT(ctx context.Context, deviceRegistrationData *auth.Device) (*auth.J
97116 ticker := time .NewTicker (time .Duration (deviceRegistrationData .Interval ) * time .Second )
98117 pollStartTime := time .Now ()
99118
100- // Polling for fetching JWT
119+ // Polling for fetching PAT
101120 func () {
102121 for range ticker .C {
103- jwtData , err = deepsource .Login (ctx , deviceRegistrationData .Code )
122+ tokenData , err = deepsource .Login (ctx , deviceRegistrationData .Code , userDescription )
104123 if err == nil {
105124 authTimedOut = false
106125 return
@@ -113,5 +132,5 @@ func fetchJWT(ctx context.Context, deviceRegistrationData *auth.Device) (*auth.J
113132 }
114133 }()
115134
116- return jwtData , authTimedOut , nil
135+ return tokenData , authTimedOut , nil
117136}
0 commit comments