Repository navigation
Add support to PKCE in OIDC connector - #3777
Conversation
|
I've tested the PR, and it works as expected, using the PKCE flow with OAuth0 and PingFederate |
|
Hi Giovanni, |
|
Could you also please sign your commits? DCO will not pass otherwise |
4c63988 to
6eee8c1
Compare
|
@nabokihms I've done the requested changes and signed my commits. |
Signed-off-by: johnvan7 <[email protected]> Signed-off-by: Giovanni Vella <[email protected]>
Signed-off-by: johnvan7 <[email protected]> Signed-off-by: Giovanni Vella <[email protected]>
Signed-off-by: johnvan7 <[email protected]> Signed-off-by: Giovanni Vella <[email protected]>
Signed-off-by: johnvan7 <[email protected]> Signed-off-by: Giovanni Vella <[email protected]>
09dea1c to
39299d7
Compare
|
@johnvan7 could you please investigate the issue with tests and the lint? |
Signed-off-by: Giovanni Vella <[email protected]>
b5d3b67 to
432c432
Compare
|
@nabokihms could you please restart workflows? |
|
@sagikazarmark could you please take a look? There is an important change to the rock solid interface. Are you OK with this? |
Signed-off-by: Giovanni Vella <[email protected]>
Signed-off-by: Giovanni Vella <[email protected]>
…-support Signed-off-by: Giovanni Vella <[email protected]>
Signed-off-by: Giovanni Vella <[email protected]>
|
Would be great to have this |
|
please approve, this would solve problems! |
|
@nabokihms @sagikazarmark this PR would greatly help my team. Could it please be approved and merged? |
|
I've created a purely rebased version of this PR at https://github.com/cardoe/dex/tree/feat/pkce-challenge-support but then I noticed that:
So I've made a branch https://github.com/cardoe/dex/tree/feat/pkce-challenge-support-squashed which cleans up history. @johnvan7 you're welcome to use either of these. I'll work with the maintainers to get this merged by testing it locally here. |
This is using my rebased squashed version.
|
To further this along, I've pulled the squashed branch into a build that I made at https://github.com/cardoe/dex/releases/tag/v2.44.90 You can grab the container to test with at ghcr.io/cardoe/dex:v2.44.90 and report back on this PR. |
|
Hi @cardoe, I've tested ghcr.io/cardoe/dex:v2.44.90 with a connector that requires the PKCE flow and it works as expected! |
Thanks for confirming. Took me a bit to test as well. I can confirm it works. |
|
Would really appreciate it if this PR can get reviewed soon 🙏 |
|
Same here for what I have tested it works great! (Thanks for it 🙏) |
|
@sagikazarmark this feature still seems important, and the interface change looks like the right one. Could you please come back to the review one more time? About the interface change, I remember discussing it here #2040 |
|
@sagikazarmark, not the best options, but with this, we can preserve the interface. The only problem is that we will lose all compiler help (and probably IDE support as well): diff --git a/connector/connector.go b/connector/connector.go
index d812390f..ec8c3a8a 100644
--- a/connector/connector.go
+++ b/connector/connector.go
@@ -47,6 +47,13 @@ type PasswordConnector interface {
Login(ctx context.Context, s Scopes, username, password string) (identity Identity, validPassword bool, err error)
}
+type LoginResult struct {
+ // CallbackURL is the URL to redirect the user to after authentication.
+ CallbackURL string
+ // Connector data is stored in the auth request to handle the state before receiving the callback.
+ ConnectorData []byte
+}
+
// CallbackConnector is an interface implemented by connectors which use an OAuth
// style redirect flow to determine user information.
type CallbackConnector interface {
@@ -63,7 +70,7 @@ type CallbackConnector interface {
// requested if one has already been issues. There's no good general answer
// for these kind of restrictions, and may require this package to become more
// aware of the global set of user/connector interactions.
- LoginURL(s Scopes, callbackURL, state string) (string, error)
+ LoginURL(s Scopes, callbackURL, state string) (any, error)
// Handle the callback to the server and return an identity.
HandleCallback(s Scopes, r *http.Request) (identity Identity, err error)
diff --git a/server/handlers.go b/server/handlers.go
index 1292b8ee..ea78766d 100644
--- a/server/handlers.go
+++ b/server/handlers.go
@@ -271,12 +271,35 @@ func (s *Server) handleConnectorLogin(w http.ResponseWriter, r *http.Request) {
// Use the auth request ID as the "state" token.
//
// TODO(ericchiang): Is this appropriate or should we also be using a nonce?
- callbackURL, err := conn.LoginURL(scopes, s.absURL("/callback"), authReq.ID)
+ callbackURL, connectorData := "", []byte("")
+ res, err := conn.LoginURL(scopes, s.absURL("/callback"), authReq.ID)
if err != nil {
s.logger.ErrorContext(r.Context(), "connector returned error when creating callback", "connector_id", connID, "err", err)
s.renderError(r, w, http.StatusInternalServerError, "Login error.")
return
}
+ switch res.(type) {
+ case string:
+ callbackURL = res.(string)
+ case *connector.LoginResult:
+ val := res.(*connector.LoginResult)
+ callbackURL, connectorData = val.CallbackURL, val.ConnectorData
+ default:
+ s.logger.ErrorContext(r.Context(), fmt.Sprintf("connector returned invalid type: %T", res), "connector_id", connID)
+ s.renderError(r, w, http.StatusInternalServerError, "Login error.")
+ }
+ if len(connectorData) > 0 {
+ updater := func(a storage.AuthRequest) (storage.AuthRequest, error) {
+ a.ConnectorData = connectorData
+ return a, nil
+ }
+ err := s.storage.UpdateAuthRequest(ctx, authReq.ID, updater)
+ if err != nil {
+ s.logger.ErrorContext(r.Context(), "Failed to set connector data on auth request", "connector_id", connID, "err", err)
+ s.renderError(r, w, http.StatusInternalServerError, "Database error.")
+ return
+ }
+ }
http.Redirect(w, r, callbackURL, http.StatusFound)
case connector.PasswordConnector:
loginURL := url.URL{
|
|
Thinking out of the box. Another way to preserve the interface. @sagikazarmark could you please pick the one you like the most? diff --git a/connector/connector.go b/connector/connector.go
index d812390f..11d6f046 100644
--- a/connector/connector.go
+++ b/connector/connector.go
@@ -4,6 +4,7 @@ package connector
import (
"context"
"net/http"
+ "strings"
)
// Connector is a mechanism for federating login to a remote identity service.
@@ -47,6 +48,27 @@ type PasswordConnector interface {
Login(ctx context.Context, s Scopes, username, password string) (identity Identity, validPassword bool, err error)
}
+const delimiter = "\x00"
+
+type LoginResult struct {
+ // CallbackURL is the URL to redirect the user to after authentication.
+ CallbackURL string
+ // Connector data is stored in the auth request to handle the state before receiving the callback.
+ ConnectorData []byte
+}
+
+func (r LoginResult) String() string {
+ return r.CallbackURL + delimiter + string(r.ConnectorData)
+}
+
+func LoginResultFromString(s string) LoginResult {
+ parts := strings.SplitN(s, delimiter, 2)
+ if len(parts) != 2 {
+ return LoginResult{CallbackURL: s}
+ }
+ return LoginResult{CallbackURL: parts[0], ConnectorData: []byte(parts[1])}
+}
+
// CallbackConnector is an interface implemented by connectors which use an OAuth
// style redirect flow to determine user information.
type CallbackConnector interface {
diff --git a/server/handlers.go b/server/handlers.go
index 1292b8ee..36f97a57 100644
--- a/server/handlers.go
+++ b/server/handlers.go
@@ -271,13 +271,27 @@ func (s *Server) handleConnectorLogin(w http.ResponseWriter, r *http.Request) {
// Use the auth request ID as the "state" token.
//
// TODO(ericchiang): Is this appropriate or should we also be using a nonce?
- callbackURL, err := conn.LoginURL(scopes, s.absURL("/callback"), authReq.ID)
+ res, err := conn.LoginURL(scopes, s.absURL("/callback"), authReq.ID)
if err != nil {
s.logger.ErrorContext(r.Context(), "connector returned error when creating callback", "connector_id", connID, "err", err)
s.renderError(r, w, http.StatusInternalServerError, "Login error.")
return
}
- http.Redirect(w, r, callbackURL, http.StatusFound)
+ loginResult := connector.LoginResultFromString(res)
+ if len(loginResult.ConnectorData) > 0 {
+ updater := func(a storage.AuthRequest) (storage.AuthRequest, error) {
+ a.ConnectorData = loginResult.ConnectorData
+ return a, nil
+ }
+ err := s.storage.UpdateAuthRequest(ctx, authReq.ID, updater)
+ if err != nil {
+ s.logger.ErrorContext(r.Context(), "Failed to set connector data on auth request", "connector_id", connID, "err", err)
+ s.renderError(r, w, http.StatusInternalServerError, "Database error.")
+ return
+ }
+ }
+
+ http.Redirect(w, r, loginResult.CallbackURL, http.StatusFound)
case connector.PasswordConnector:
loginURL := url.URL{
Path: s.absPath("/auth", connID, "login"), |
|
I don't mind an interface change if it's justified. I'd rather change the interface than rely on hacks. :) In this case, changing it is probably the right thing to do. |
nabokihms
left a comment
There was a problem hiding this comment.
With this being said, thank you @johnvan7 for the PR and for all the patience.
Congratulations to all people subscribed to this thread! Today, we are celebrating a new long-awaited feature and the first connector interface change in literally 10 years.
Cherry-pick of upstream dexidp#4845 (e8aaa98), which is merged to upstream master but not yet in any release (latest release v2.45.1 is from before the merge). Adapts the new test's single HandleCallback call site from the 3-arg signature (introduced upstream by PKCE PR dexidp#3777, not present in this fork) back to the 2-arg signature on this branch. Production code applies unchanged. Fixes forced re-login after ~8h for GitHub App-backed connectors. Signed-off-by: Matheus Pimenta <[email protected]> (cherry picked from commit e8aaa98) Closes: giantswarm/giantswarm#37164 Co-authored-by: Matheus Pimenta <[email protected]>
Overview
This PR adds support for PKCE in the OIDC connector.
PKCE (Proof Key for Code Exchange) - RFC 7636 - is an extension to the Authorization Code flow to prevent CSRF (Cross-Site Request Forgery) and authorization code injection attacks.
PKCE Flow
The PKCE flow requires a code challenge to be passed to the authorization endpoint and a code verifier to be sent to the token endpoint.
What this PR does / why we need it
Dex currently does not support PKCE flow for upstream OIDC providers.
However, some identity providers (IdPs) require PKCE flow support to complete the authentication process.
Key features
connectorDataof the auth request in the database, allowing the management of multiple PKCE connections simultaneously across multiple running instances of dex.Does this PR introduce a user-facing change?
PKCEChallengeconfiguration option to the OIDC connector to specify an algorithm to use in the PKCE flow.Special notes for your reviewer
Related PRs:
Related discussions: