@@ -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 }
@@ -164,29 +167,30 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
164167 p .Debug (print .DebugLevel , "trading authorization code for access and refresh tokens" )
165168
166169 // Trade the authorization code and the code verifier for access and refresh tokens
167- accessToken , refreshToken , err := getUserAccessAndRefreshTokens (idpWellKnownConfig , idpClientID , codeVerifier , code , redirectURL )
170+ accessToken , refreshToken , err := getUserAccessAndRefreshTokens (p , idpWellKnownConfig , idpClientID , codeVerifier , code , redirectURL )
168171 if err != nil {
169172 errServer = fmt .Errorf ("retrieve tokens: %w" , err )
170173 return
171174 }
172175
173176 p .Debug (print .DebugLevel , "received response from the authentication server" )
174177
175- sessionExpiresAtUnix , err := getStartingSessionExpiresAtUnix ()
178+ // Get access token expiration from the token itself (not session time limit)
179+ sessionExpiresAtUnix , err := getAccessTokenExpiresAtUnix (accessToken )
176180 if err != nil {
177- errServer = fmt .Errorf ("compute session expiration timestamp : %w" , err )
181+ errServer = fmt .Errorf ("get access token expiration : %w" , err )
178182 return
179183 }
180184
181185 sessionExpiresAtUnixInt , err := strconv .Atoi (sessionExpiresAtUnix )
182186 if err != nil {
183- p .Debug (print .ErrorLevel , "parse session expiration value \" %s\" : %s" , sessionExpiresAtUnix , err )
187+ p .Debug (print .ErrorLevel , "parse access token expiration value \" %s\" : %s" , sessionExpiresAtUnix , err )
184188 } else {
185189 sessionExpiresAt := time .Unix (int64 (sessionExpiresAtUnixInt ), 0 )
186- p .Debug (print .DebugLevel , "session expires at %s" , sessionExpiresAt )
190+ p .Debug (print .DebugLevel , "access token expires at %s" , sessionExpiresAt )
187191 }
188192
189- err = SetAuthFlow ( AUTH_FLOW_USER_TOKEN )
193+ err = SetAuthFlowWithContext ( context , AUTH_FLOW_USER_TOKEN )
190194 if err != nil {
191195 errServer = fmt .Errorf ("set auth flow type: %w" , err )
192196 return
@@ -200,7 +204,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
200204
201205 p .Debug (print .DebugLevel , "user %s logged in successfully" , email )
202206
203- err = LoginUser ( email , accessToken , refreshToken , sessionExpiresAtUnix )
207+ err = LoginUserWithContext ( context , email , accessToken , refreshToken , sessionExpiresAtUnix )
204208 if err != nil {
205209 errServer = fmt .Errorf ("set in auth storage: %w" , err )
206210 return
@@ -216,7 +220,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
216220 mux .HandleFunc (loginSuccessPath , func (w http.ResponseWriter , _ * http.Request ) {
217221 defer cleanup (server )
218222
219- email , err := GetAuthField ( USER_EMAIL )
223+ email , err := GetAuthFieldWithContext ( context , USER_EMAIL )
220224 if err != nil {
221225 errServer = fmt .Errorf ("read user email: %w" , err )
222226 }
@@ -270,7 +274,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
270274}
271275
272276// getUserAccessAndRefreshTokens trades the authorization code retrieved from the first OAuth2 leg for an access token and a refresh token
273- func getUserAccessAndRefreshTokens (idpWellKnownConfig * wellKnownConfig , clientID , codeVerifier , authorizationCode , callbackURL string ) (accessToken , refreshToken string , err error ) {
277+ func getUserAccessAndRefreshTokens (p * print. Printer , idpWellKnownConfig * wellKnownConfig , clientID , codeVerifier , authorizationCode , callbackURL string ) (accessToken , refreshToken string , err error ) {
274278 // Set form-encoded data for the POST to the access token endpoint
275279 data := fmt .Sprintf (
276280 "grant_type=authorization_code&client_id=%s" +
@@ -283,6 +287,10 @@ func getUserAccessAndRefreshTokens(idpWellKnownConfig *wellKnownConfig, clientID
283287 // Create the request and execute it
284288 req , _ := http .NewRequest ("POST" , idpWellKnownConfig .TokenEndpoint , payload )
285289 req .Header .Add ("content-type" , "application/x-www-form-urlencoded" )
290+
291+ // Debug log the request
292+ debugHTTPRequest (p , req )
293+
286294 httpClient := & http.Client {}
287295 res , err := httpClient .Do (req )
288296 if err != nil {
@@ -296,6 +304,10 @@ func getUserAccessAndRefreshTokens(idpWellKnownConfig *wellKnownConfig, clientID
296304 err = fmt .Errorf ("close response body: %w" , closeErr )
297305 }
298306 }()
307+
308+ // Debug log the response
309+ debugHTTPResponse (p , res )
310+
299311 body , err := io .ReadAll (res .Body )
300312 if err != nil {
301313 return "" , "" , fmt .Errorf ("read response body: %w" , err )
@@ -355,8 +367,12 @@ func openBrowser(pageUrl string) error {
355367
356368// parseWellKnownConfiguration gets the well-known OpenID configuration from the provided URL and returns it as a JSON
357369// the method also stores the IDP token endpoint in the authentication storage
358- func parseWellKnownConfiguration (httpClient apiClient , wellKnownConfigURL string ) (wellKnownConfig * wellKnownConfig , err error ) {
370+ func parseWellKnownConfiguration (p * print. Printer , httpClient apiClient , wellKnownConfigURL string , context StorageContext ) (wellKnownConfig * wellKnownConfig , err error ) {
359371 req , _ := http .NewRequest ("GET" , wellKnownConfigURL , http .NoBody )
372+
373+ // Debug log the request
374+ debugHTTPRequest (p , req )
375+
360376 res , err := httpClient .Do (req )
361377 if err != nil {
362378 return nil , fmt .Errorf ("make the request: %w" , err )
@@ -369,6 +385,10 @@ func parseWellKnownConfiguration(httpClient apiClient, wellKnownConfigURL string
369385 err = fmt .Errorf ("close response body: %w" , closeErr )
370386 }
371387 }()
388+
389+ // Debug log the response
390+ debugHTTPResponse (p , res )
391+
372392 body , err := io .ReadAll (res .Body )
373393 if err != nil {
374394 return nil , fmt .Errorf ("read response body: %w" , err )
@@ -391,7 +411,7 @@ func parseWellKnownConfiguration(httpClient apiClient, wellKnownConfigURL string
391411 return nil , fmt .Errorf ("found no token endpoint" )
392412 }
393413
394- err = SetAuthField ( IDP_TOKEN_ENDPOINT , wellKnownConfig .TokenEndpoint )
414+ err = SetAuthFieldWithContext ( context , IDP_TOKEN_ENDPOINT , wellKnownConfig .TokenEndpoint )
395415 if err != nil {
396416 return nil , fmt .Errorf ("set token endpoint in the authentication storage: %w" , err )
397417 }
0 commit comments