Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PKCE #265

Merged
merged 5 commits into from
Oct 10, 2024
Merged

Add support for PKCE #265

merged 5 commits into from
Oct 10, 2024

Conversation

gdasson
Copy link
Contributor

@gdasson gdasson commented Oct 9, 2024

This PR adds support for PKCE (Proof Key for Code Exchange) to enhance the security of the OAuth 2.0 Authorization Code flow. It includes generating a code verifier and code challenge, and ensures these parameters are used during the authorization and token exchange processes. This update aligns with OAuth 2.0 best practices and improves compatibility with identity providers that require PKCE.

More details about PKCE here:

Signed-off-by: Gaurav Dasson <[email protected]>
Copy link
Collaborator

@nacx nacx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks great. A couple things:

  • Can you run make format? This should format the code and have CI pass the check step.
  • One of the e2e test suites uses Keycloak. Is there a way to configure it to force that a client uses PKCE? It would be great to have it enforced to make sure we're properly testing that path. Or we could create an additional suite, keycloak-pkce with this setup. WDYT?

@@ -133,7 +133,7 @@ func TestSessionGenerator(t *testing.T) {
require.NotEqual(t, sg.GenerateNonce(), sg.GenerateNonce())
})
t.Run("static", func(t *testing.T) {
sg := NewStaticGenerator("sessionid", "nonce", "state")
sg := NewStaticGenerator("sessionid", "nonce", "state", "codeverifier")
require.Equal(t, sg.GenerateSessionID(), sg.GenerateSessionID())
require.Equal(t, sg.GenerateState(), sg.GenerateState())
require.Equal(t, sg.GenerateNonce(), sg.GenerateNonce())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Update this test and the random one to also include a check for the code verifier.

Copy link
Contributor Author

@gdasson gdasson Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nacx : Thanks for the review. Yes, there is a way to configure keycloak to enforce PKCE use by client. Infact, I also used keycloak for my local testing. In the UI, It's available under Client > Advanced > Proof Key for Code Exchange Code Challenge Method > Set it to S256. (However, I am not sure if the same setting is available through the Keycloak admin API)

Here is a screenshot from the UI:
image

Copy link
Collaborator

@nacx nacx Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client is created and configured here: https://github.com/istio-ecosystem/authservice/blob/main/e2e/keycloak/setup-keycloak.sh#L50-L58
Let's see if PKCE can be configured in that script.

Actually, there is probably no need to create another suite. The istio suite also uses Keycloak, so we can just change the keycloak suite and we should have all cases covered.

Testing ti locally should be easy:

make docker   # the e2e tests need the update Docker image
make e2e/keycloak

# Or, if you want to see all requests/responses for the e2e tests logged:
E2E_TEST_OPTS="-v -count=1" make e2e/keycloak

Or you can start the infra and run the tests from your IDE: https://github.com/istio-ecosystem/authservice/blob/main/DEVELOPMENT.md#running-tests-from-your-ide
Keycloak will be accessible on https://localhost:9443 (admin / admin)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this to the keycloak setup script sould do the trick, to enforce PKCE server-side:

diff --git a/e2e/keycloak/setup-keycloak.sh b/e2e/keycloak/setup-keycloak.sh
index 7fc46af..ee3d340 100755
--- a/e2e/keycloak/setup-keycloak.sh
+++ b/e2e/keycloak/setup-keycloak.sh
@@ -51,6 +51,7 @@ set -ex
     -s clientId="${CLIENT_ID}" \
     -s secret="${CLIENT_SECRET}" \
     -s "redirectUris=[\"${REDIRECT_URL}\"]" \
+    -s "attributes={\"pkce.code.challenge.method\":\"S256\"}" \
     -s consentRequired=false \
     --server "${KEYCLOAK_SERVER}" \
     --realm "${REALM}" \

Copy link
Contributor Author

@gdasson gdasson Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added and ran e2e tests, they run fine. Also physically checked the setting in the UI before destroying infra.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I run the e2e with this setting but without the changes in the PR and they were failing, complaining about the missing query params, so all good!

@nacx
Copy link
Collaborator

nacx commented Oct 10, 2024

CI complains about project not being go mod tidied.

Since we have code generation, the check job makes sure there are no changes after regenerating/formatting everything, and fails early if that's not the case.

If you run locally make check locally, it should update what's missing and you can commit that to have that check pass.

Copy link
Collaborator

@nacx nacx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Changes LGTM. Let's cleanup the build to have all tests pass.

@codecov-commenter
Copy link

codecov-commenter commented Oct 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@e99c3eb). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #265   +/-   ##
=======================================
  Coverage        ?   90.71%           
=======================================
  Files           ?       25           
  Lines           ?     1615           
  Branches        ?        0           
=======================================
  Hits            ?     1465           
  Misses          ?      107           
  Partials        ?       43           
Files with missing lines Coverage Δ
internal/authz/oidc.go 94.10% <100.00%> (ø)
internal/oidc/redis.go 75.35% <100.00%> (ø)
internal/oidc/session.go 100.00% <100.00%> (ø)

@istio-testing istio-testing merged commit adea4ec into istio-ecosystem:main Oct 10, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants