Skip to content

Commit 17735ff

Browse files
committed
Fix broken code exchange on Google Chrome
It seems that Chrome will close the callback connection to localhost:XXXXX before naisdevice-agent can send any data back. This resulted in a cancellation of the oauth2 code exchange, due to naisdevice-agent using the context from the request. This change switches to using context.Background instead.
1 parent 52f7e0b commit 17735ff

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

internal/device-agent/auth/azure.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"time"
88

99
"github.com/lestrrat-go/jwx/jwt"
10-
"github.com/nais/device/internal/auth"
1110
codeverifier "github.com/nirasan/go-oauth-pkce-code-verifier"
1211
"golang.org/x/oauth2"
12+
13+
"github.com/nais/device/internal/auth"
1314
)
1415

1516
func handleRedirectAzure(state string, conf oauth2.Config, codeVerifier *codeverifier.CodeVerifier, authFlowChan chan *authFlowResponse) http.HandlerFunc {
@@ -27,7 +28,10 @@ func handleRedirectAzure(state string, conf oauth2.Config, codeVerifier *codever
2728
return
2829
}
2930

30-
ctx, cancel := context.WithDeadline(r.Context(), time.Now().Add(30*time.Second))
31+
// We used to use r.Context() here, but a Google Chrome update broke that.
32+
// It seems that Chrome closes the HTTP connection prematurely, because the context
33+
// is at this point already canceled.
34+
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
3135
defer cancel()
3236

3337
codeVerifierParam := oauth2.SetAuthURLParam("code_verifier", codeVerifier.String())

internal/device-agent/auth/google.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func handleRedirectGoogle(state, redirectURI string, codeVerifier *codeverifier.
3838
return
3939
}
4040

41-
ctx, cancel := context.WithDeadline(r.Context(), time.Now().Add(10*time.Second))
41+
// We used to use r.Context() here, but a Google Chrome update broke that.
42+
// It seems that Chrome closes the HTTP connection prematurely, because the context
43+
// is at this point already canceled.
44+
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
4245
defer cancel()
4346

4447
exchangeRequest := ExchangeRequest{

0 commit comments

Comments
 (0)