Skip to content

Commit 08f52e9

Browse files
committed
feat: oauth2 client to use pkce in auth/exchange flow
Used in coder primary auth
1 parent 05b02cf commit 08f52e9

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

coderd/httpmw/oauth2.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,14 @@ func ExtractOAuth2(config promoauth.OAuth2Config, client *http.Client, cookieCfg
133133
HttpOnly: true,
134134
}))
135135

136-
http.Redirect(rw, r, config.AuthCodeURL(state, opts...), http.StatusTemporaryRedirect)
136+
var verifier = oauth2.GenerateVerifier()
137+
http.SetCookie(rw, cookieCfg.Apply(&http.Cookie{
138+
Name: codersdk.OAuth2PKCEChallenge,
139+
Value: verifier,
140+
Path: "/",
141+
HttpOnly: true,
142+
}))
143+
http.Redirect(rw, r, config.AuthCodeURL(state, append(opts, oauth2.S256ChallengeOption(verifier))...), http.StatusTemporaryRedirect)
137144
return
138145
}
139146

@@ -163,7 +170,13 @@ func ExtractOAuth2(config promoauth.OAuth2Config, client *http.Client, cookieCfg
163170
redirect = stateRedirect.Value
164171
}
165172

166-
oauthToken, err := config.Exchange(ctx, code)
173+
exchangeOpts := []oauth2.AuthCodeOption{}
174+
pkceChallenge, err := r.Cookie(codersdk.OAuth2PKCEChallenge)
175+
if err == nil {
176+
exchangeOpts = append(exchangeOpts, oauth2.VerifierOption(pkceChallenge.Value))
177+
}
178+
179+
oauthToken, err := config.Exchange(ctx, code, exchangeOpts...)
167180
if err != nil {
168181
errorCode := http.StatusInternalServerError
169182
detail := err.Error()

codersdk/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
SessionTokenHeader = "Coder-Session-Token"
3939
// OAuth2StateCookie is the name of the cookie that stores the oauth2 state.
4040
OAuth2StateCookie = "oauth_state"
41+
42+
OAuth2PKCEChallenge = "oauth_pkce_challenge"
4143
// OAuth2RedirectCookie is the name of the cookie that stores the oauth2 redirect.
4244
OAuth2RedirectCookie = "oauth_redirect"
4345

0 commit comments

Comments
 (0)