@@ -50,7 +50,10 @@ type apiClient interface {
5050}
5151
5252// AuthorizeUser implements the PKCE OAuth2 flow.
53- func AuthorizeUser (p * print.Printer , isReauthentication bool ) error {
53+ func AuthorizeUser (p * print.Printer , context StorageContext , isReauthentication bool ) error {
54+ // Set the storage printer so debug messages use the correct verbosity
55+ SetStoragePrinter (p )
56+
5457 idpWellKnownConfigURL , err := getIDPWellKnownConfigURL ()
5558 if err != nil {
5659 return fmt .Errorf ("get IDP well-known configuration: %w" , err )
@@ -65,7 +68,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
6568
6669 p .Debug (print .DebugLevel , "get IDP well-known configuration from %s" , idpWellKnownConfigURL )
6770 httpClient := & http.Client {}
68- idpWellKnownConfig , err := parseWellKnownConfiguration (httpClient , idpWellKnownConfigURL )
71+ idpWellKnownConfig , err := parseWellKnownConfiguration (p , httpClient , idpWellKnownConfigURL , context )
6972 if err != nil {
7073 return fmt .Errorf ("parse IDP well-known configuration: %w" , err )
7174 }
@@ -159,29 +162,30 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
159162 p .Debug (print .DebugLevel , "trading authorization code for access and refresh tokens" )
160163
161164 // Trade the authorization code and the code verifier for access and refresh tokens
162- accessToken , refreshToken , err := getUserAccessAndRefreshTokens (idpWellKnownConfig , idpClientID , codeVerifier , code , redirectURL )
165+ accessToken , refreshToken , err := getUserAccessAndRefreshTokens (p , idpWellKnownConfig , idpClientID , codeVerifier , code , redirectURL )
163166 if err != nil {
164167 errServer = fmt .Errorf ("retrieve tokens: %w" , err )
165168 return
166169 }
167170
168171 p .Debug (print .DebugLevel , "received response from the authentication server" )
169172
170- sessionExpiresAtUnix , err := getStartingSessionExpiresAtUnix ()
173+ // Get access token expiration from the token itself (not session time limit)
174+ sessionExpiresAtUnix , err := getAccessTokenExpiresAtUnix (accessToken )
171175 if err != nil {
172- errServer = fmt .Errorf ("compute session expiration timestamp : %w" , err )
176+ errServer = fmt .Errorf ("get access token expiration : %w" , err )
173177 return
174178 }
175179
176180 sessionExpiresAtUnixInt , err := strconv .Atoi (sessionExpiresAtUnix )
177181 if err != nil {
178- p .Debug (print .ErrorLevel , "parse session expiration value \" %s\" : %s" , sessionExpiresAtUnix , err )
182+ p .Debug (print .ErrorLevel , "parse access token expiration value \" %s\" : %s" , sessionExpiresAtUnix , err )
179183 } else {
180184 sessionExpiresAt := time .Unix (int64 (sessionExpiresAtUnixInt ), 0 )
181- p .Debug (print .DebugLevel , "session expires at %s" , sessionExpiresAt )
185+ p .Debug (print .DebugLevel , "access token expires at %s" , sessionExpiresAt )
182186 }
183187
184- err = SetAuthFlow ( AUTH_FLOW_USER_TOKEN )
188+ err = SetAuthFlowWithContext ( context , AUTH_FLOW_USER_TOKEN )
185189 if err != nil {
186190 errServer = fmt .Errorf ("set auth flow type: %w" , err )
187191 return
@@ -195,7 +199,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
195199
196200 p .Debug (print .DebugLevel , "user %s logged in successfully" , email )
197201
198- err = LoginUser ( email , accessToken , refreshToken , sessionExpiresAtUnix )
202+ err = LoginUserWithContext ( context , email , accessToken , refreshToken , sessionExpiresAtUnix )
199203 if err != nil {
200204 errServer = fmt .Errorf ("set in auth storage: %w" , err )
201205 return
@@ -211,7 +215,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
211215 mux .HandleFunc (loginSuccessPath , func (w http.ResponseWriter , _ * http.Request ) {
212216 defer cleanup (server )
213217
214- email , err := GetAuthField ( USER_EMAIL )
218+ email , err := GetAuthFieldWithContext ( context , USER_EMAIL )
215219 if err != nil {
216220 errServer = fmt .Errorf ("read user email: %w" , err )
217221 }
@@ -265,7 +269,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
265269}
266270
267271// getUserAccessAndRefreshTokens trades the authorization code retrieved from the first OAuth2 leg for an access token and a refresh token
268- func getUserAccessAndRefreshTokens (idpWellKnownConfig * wellKnownConfig , clientID , codeVerifier , authorizationCode , callbackURL string ) (accessToken , refreshToken string , err error ) {
272+ func getUserAccessAndRefreshTokens (p * print. Printer , idpWellKnownConfig * wellKnownConfig , clientID , codeVerifier , authorizationCode , callbackURL string ) (accessToken , refreshToken string , err error ) {
269273 // Set form-encoded data for the POST to the access token endpoint
270274 data := fmt .Sprintf (
271275 "grant_type=authorization_code&client_id=%s" +
@@ -278,6 +282,10 @@ func getUserAccessAndRefreshTokens(idpWellKnownConfig *wellKnownConfig, clientID
278282 // Create the request and execute it
279283 req , _ := http .NewRequest ("POST" , idpWellKnownConfig .TokenEndpoint , payload )
280284 req .Header .Add ("content-type" , "application/x-www-form-urlencoded" )
285+
286+ // Debug log the request
287+ debugHTTPRequest (p , req )
288+
281289 httpClient := & http.Client {}
282290 res , err := httpClient .Do (req )
283291 if err != nil {
@@ -291,6 +299,10 @@ func getUserAccessAndRefreshTokens(idpWellKnownConfig *wellKnownConfig, clientID
291299 err = fmt .Errorf ("close response body: %w" , closeErr )
292300 }
293301 }()
302+
303+ // Debug log the response
304+ debugHTTPResponse (p , res )
305+
294306 body , err := io .ReadAll (res .Body )
295307 if err != nil {
296308 return "" , "" , fmt .Errorf ("read response body: %w" , err )
@@ -350,8 +362,12 @@ func openBrowser(pageUrl string) error {
350362
351363// parseWellKnownConfiguration gets the well-known OpenID configuration from the provided URL and returns it as a JSON
352364// the method also stores the IDP token endpoint in the authentication storage
353- func parseWellKnownConfiguration (httpClient apiClient , wellKnownConfigURL string ) (wellKnownConfig * wellKnownConfig , err error ) {
365+ func parseWellKnownConfiguration (p * print. Printer , httpClient apiClient , wellKnownConfigURL string , context StorageContext ) (wellKnownConfig * wellKnownConfig , err error ) {
354366 req , _ := http .NewRequest ("GET" , wellKnownConfigURL , http .NoBody )
367+
368+ // Debug log the request
369+ debugHTTPRequest (p , req )
370+
355371 res , err := httpClient .Do (req )
356372 if err != nil {
357373 return nil , fmt .Errorf ("make the request: %w" , err )
@@ -364,6 +380,10 @@ func parseWellKnownConfiguration(httpClient apiClient, wellKnownConfigURL string
364380 err = fmt .Errorf ("close response body: %w" , closeErr )
365381 }
366382 }()
383+
384+ // Debug log the response
385+ debugHTTPResponse (p , res )
386+
367387 body , err := io .ReadAll (res .Body )
368388 if err != nil {
369389 return nil , fmt .Errorf ("read response body: %w" , err )
@@ -386,7 +406,7 @@ func parseWellKnownConfiguration(httpClient apiClient, wellKnownConfigURL string
386406 return nil , fmt .Errorf ("found no token endpoint" )
387407 }
388408
389- err = SetAuthField ( IDP_TOKEN_ENDPOINT , wellKnownConfig .TokenEndpoint )
409+ err = SetAuthFieldWithContext ( context , IDP_TOKEN_ENDPOINT , wellKnownConfig .TokenEndpoint )
390410 if err != nil {
391411 return nil , fmt .Errorf ("set token endpoint in the authentication storage: %w" , err )
392412 }
0 commit comments