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 config option to enable PKCE #191

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/main/java/org/jenkinsci/plugins/oic/OicSecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public static enum TokenAuthMethod { client_secret_basic, client_secret_post };
*/
private boolean sendScopesInTokenRequest = false;

/** Flag to enable PKCE challenge
*/
private boolean pkceEnabled = false;

/** old field that had an '/' implicitly added at the end,
* transient because we no longer want to have this value stored
* but it's still needed for backwards compatibility */
Expand Down Expand Up @@ -394,6 +398,10 @@ public boolean isSendScopesInTokenRequest() {
return sendScopesInTokenRequest;
}

public boolean isPkceEnabled() {
return pkceEnabled;
}

public boolean isAutoConfigure() {
return "auto".equals(this.automanualconfigure);
}
Expand Down Expand Up @@ -536,6 +544,11 @@ public void setSendScopesInTokenRequest(boolean sendScopesInTokenRequest) {
this.sendScopesInTokenRequest = sendScopesInTokenRequest;
}

@DataBoundSetter
public void setPkceEnabled(boolean pkceEnabled) {
this.pkceEnabled = pkceEnabled;
}

@Override
public String getLoginUrl() {
//Login begins with our doCommenceLogin(String,String) method
Expand Down Expand Up @@ -620,7 +633,7 @@ protected AuthorizationCodeFlow buildAuthorizationCodeFlow() {
tokenAccessMethod = BearerToken.authorizationHeaderAccessMethod();
authInterceptor = new BasicAuthentication(clientId, Secret.toString(clientSecret));
}
return new AuthorizationCodeFlow.Builder(
AuthorizationCodeFlow.Builder builder = new AuthorizationCodeFlow.Builder(
tokenAccessMethod,
httpTransport,
JSON_FACTORY,
Expand All @@ -629,8 +642,13 @@ protected AuthorizationCodeFlow buildAuthorizationCodeFlow() {
clientId,
authorizationServerUrl
)
.setScopes(Arrays.asList(this.getScopes()))
.build();
.setScopes(Arrays.asList(this.getScopes()));

if(pkceEnabled) {
builder.enablePKCE();
}

return builder.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<f:entry title="${%Post logout redirect URL}" field="postLogoutRedirectUrl">
<f:textbox/>
</f:entry>
<f:entry title="${%Enable Proof Key for Code Exchang (PKCE)}" field="pkceEnabled">
<f:checkbox/>
</f:entry>

<f:block>
<table>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Enable Proof Key for Code Exchange (PKCE)..
</div>
44 changes: 42 additions & 2 deletions src/test/java/org/jenkinsci/plugins/oic/PluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.notMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void setUp() {
assertTrue("User should be part of group " + TEST_USER_GROUPS[1], user.getAuthorities().contains(TEST_USER_GROUPS[1]));

verify(getRequestedFor(urlPathEqualTo("/authorization"))
. withQueryParam("scope", equalTo("openid email")));
.withQueryParam("scope", equalTo("openid email")));
verify(postRequestedFor(urlPathEqualTo("/token"))
.withRequestBody(notMatching(".*&scope=.*")));
}
Expand Down Expand Up @@ -157,11 +158,50 @@ public void setUp() {
webClient.goTo(jenkins.getSecurityRealm().getLoginUrl());

verify(getRequestedFor(urlPathEqualTo("/authorization"))
. withQueryParam("scope", equalTo("openid email")));
.withQueryParam("scope", equalTo("openid email")));
verify(postRequestedFor(urlPathEqualTo("/token"))
.withRequestBody(containing("&scope=openid+email&")));
}

@Test public void testLoginWithPkceEnabled() throws Exception {
KeyPair keyPair = createKeyPair();

wireMockRule.stubFor(get(urlPathEqualTo("/authorization")).willReturn(
aResponse()
.withStatus(302)
.withHeader("Content-Type", "text/html; charset=utf-8")
.withHeader("Location", jenkins.getRootUrl()+"securityRealm/finishLogin?state=state&code=code")
.withBody("")
));
Map<String, Object> keyValues = new HashMap<>();
keyValues.put(EMAIL_FIELD, TEST_USER_EMAIL_ADDRESS);
keyValues.put(FULL_NAME_FIELD, TEST_USER_FULL_NAME);
keyValues.put(GROUPS_FIELD, TEST_USER_GROUPS);

wireMockRule.stubFor(post(urlPathEqualTo("/token")).willReturn(
aResponse()
.withHeader("Content-Type", "text/html; charset=utf-8")
.withBody("{" +
"\"id_token\": \""+createIdToken(keyPair.getPrivate(), keyValues)+"\"," +
"\"access_token\":\"AcCeSs_ToKeN\"," +
"\"token_type\":\"example\"," +
"\"expires_in\":3600," +
"\"refresh_token\":\"ReFrEsH_ToKeN\"," +
"\"example_parameter\":\"example_value\"" +
"}")
));


TestRealm oidcSecurityRealm = new TestRealm(wireMockRule);
oidcSecurityRealm.setPkceEnabled(true);
jenkins.setSecurityRealm(oidcSecurityRealm);
webClient.goTo(jenkins.getSecurityRealm().getLoginUrl());

verify(getRequestedFor(urlPathEqualTo("/authorization"))
.withQueryParam("code_challenge_method", equalTo("S256"))
.withQueryParam("code_challenge", matching(".+")));
}

@Test public void testLoginWithMinimalConfiguration() throws Exception {
KeyPair keyPair = createKeyPair();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jenkins:
userNameField: userNameField
rootURLFromRequest: true
sendScopesInTokenRequest: true
pkceEnabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ escapeHatchGroup: "escapeHatchGroup"
escapeHatchUsername: "escapeHatchUsername"
fullNameFieldName: "fullNameFieldName"
groupsFieldName: "groupsFieldName"
pkceEnabled: true
rootURLFromRequest: true
scopes: "scopes"
sendScopesInTokenRequest: true
Expand Down