Skip to content

Commit ee96657

Browse files
Merge pull request #2243 from ChinthakaJ98/authenticator-display-name
Adding authentication methods display name for the client authenticators
2 parents aaa7c7b + 3bb71c9 commit ee96657

File tree

9 files changed

+133
-24
lines changed

9 files changed

+133
-24
lines changed

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/client/authentication/BasicAuthClientAuthenticator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException;
3131
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
3232
import org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext;
33+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
3334
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
3435

3536
import java.util.ArrayList;
@@ -52,6 +53,8 @@ public class BasicAuthClientAuthenticator extends AbstractOAuthClientAuthenticat
5253
private static final int CREDENTIAL_LENGTH = 2;
5354
private static final String CLIENT_SECRET_BASIC = "client_secret_basic";
5455
private static final String CLIENT_SECRET_POST = "client_secret_post";
56+
private static final String CLIENT_SECRET_BASIC_DISPLAY_NAME = "Client Secret Basic";
57+
private static final String CLIENT_SECRET_POST_DISPLAY_NAME = "Client Secret Post";
5558

5659
/**
5760
* Returns the execution order of this authenticator
@@ -278,11 +281,13 @@ protected void setClientCredentialsFromParam(Map<String, List> bodyParams, OAuth
278281
* @return Authentication methods supported by the authenticator.
279282
*/
280283
@Override
281-
public List<String> getSupportedClientAuthenticationMethods() {
284+
public List<ClientAuthenticationMethodModel> getSupportedClientAuthenticationMethods() {
282285

283-
List<String> supportedAuthMethods = new ArrayList<>();
284-
supportedAuthMethods.add(CLIENT_SECRET_BASIC);
285-
supportedAuthMethods.add(CLIENT_SECRET_POST);
286+
List<ClientAuthenticationMethodModel> supportedAuthMethods = new ArrayList<>();
287+
supportedAuthMethods.add(new ClientAuthenticationMethodModel(CLIENT_SECRET_BASIC,
288+
CLIENT_SECRET_BASIC_DISPLAY_NAME));
289+
supportedAuthMethods.add(new ClientAuthenticationMethodModel(CLIENT_SECRET_POST,
290+
CLIENT_SECRET_POST_DISPLAY_NAME));
286291
return supportedAuthMethods;
287292
}
288293

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/client/authentication/OAuthClientAuthenticator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.wso2.carbon.identity.core.handler.IdentityHandler;
2222
import org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext;
23+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
2324

2425
import java.util.Collections;
2526
import java.util.List;
@@ -72,7 +73,7 @@ String getClientId(HttpServletRequest request, Map<String, List> bodyParams, OAu
7273
*
7374
* @return Authentication methods supported by the authenticator.
7475
*/
75-
default List<String> getSupportedClientAuthenticationMethods() {
76+
default List<ClientAuthenticationMethodModel> getSupportedClientAuthenticationMethods() {
7677

7778
return Collections.emptyList();
7879
}

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/client/authentication/OAuthClientAuthnService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
3030
import org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext;
3131
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
32+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
3233
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
3334

3435
import java.util.ArrayList;
@@ -382,8 +383,11 @@ private List<OAuthClientAuthenticator> filterClientAuthenticatorsForFapi(
382383

383384
List<OAuthClientAuthenticator> filteredAuthenticators = new ArrayList<>();
384385
for (OAuthClientAuthenticator authenticator : configuredAuthenticators) {
385-
if (fapiAllowedAuthMethods.stream().anyMatch(authenticator
386-
.getSupportedClientAuthenticationMethods()::contains)) {
386+
List<String> supportedClientAuthMethods = new ArrayList<>();
387+
for (ClientAuthenticationMethodModel authMethod : authenticator.getSupportedClientAuthenticationMethods()) {
388+
supportedClientAuthMethods.add(authMethod.getName());
389+
}
390+
if (fapiAllowedAuthMethods.stream().anyMatch(supportedClientAuthMethods::contains)) {
387391
filteredAuthenticators.add(authenticator);
388392
}
389393
}
@@ -401,8 +405,11 @@ private List<OAuthClientAuthenticator> getApplicableClientAuthenticators(List<St
401405

402406
List<OAuthClientAuthenticator> applicableClientAuthenticators = new ArrayList<>();
403407
for (OAuthClientAuthenticator authenticator : this.getClientAuthenticators()) {
404-
if (configuredAuthenticators.stream().anyMatch(
405-
authenticator.getSupportedClientAuthenticationMethods()::contains)) {
408+
List<String> supportedClientAuthMethods = new ArrayList<>();
409+
for (ClientAuthenticationMethodModel authMethod : authenticator.getSupportedClientAuthenticationMethods()) {
410+
supportedClientAuthMethods.add(authMethod.getName());
411+
}
412+
if (configuredAuthenticators.stream().anyMatch(supportedClientAuthMethods::contains)) {
406413
applicableClientAuthenticators.add(authenticator);
407414
}
408415
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.identity.oauth2.model;
20+
21+
/**
22+
* Client authentication method model.
23+
*/
24+
public class ClientAuthenticationMethodModel {
25+
26+
private String name;
27+
private String displayName;
28+
29+
public ClientAuthenticationMethodModel(String name, String displayName) {
30+
31+
this.name = name;
32+
this.displayName = displayName;
33+
}
34+
35+
public String getName() {
36+
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
42+
this.name = name;
43+
}
44+
45+
public String getDisplayName() {
46+
47+
return displayName;
48+
}
49+
50+
public void setDisplayName(String displayName) {
51+
52+
this.displayName = displayName;
53+
}
54+
}

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/util/OAuth2Util.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
import org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO;
130130
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
131131
import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
132+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
132133
import org.wso2.carbon.identity.oauth2.model.ClientCredentialDO;
133134
import org.wso2.carbon.identity.oauth2.token.JWTTokenIssuer;
134135
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
@@ -5013,15 +5014,31 @@ public static String[] extractCredentialsFromAuthzHeader(HttpServletRequest requ
50135014
*/
50145015
public static String[] getSupportedClientAuthMethods() {
50155016

5016-
List<OAuthClientAuthenticator> clientAuthenticators = OAuth2ServiceComponentHolder.getAuthenticationHandlers();
5017+
HashSet<ClientAuthenticationMethodModel> clientAuthenticators = OAuth2Util.getSupportedAuthenticationMethods();
50175018
HashSet<String> supportedClientAuthMethods = new HashSet<>();
5019+
for (ClientAuthenticationMethodModel authMethod : clientAuthenticators) {
5020+
supportedClientAuthMethods.add(authMethod.getName());
5021+
}
5022+
return supportedClientAuthMethods.toArray(new String[0]);
5023+
}
5024+
5025+
/**
5026+
* Retrieve the list of client authentication methods supported by the server with the authenticator display name.
5027+
*
5028+
* @return Client authentication methods supported by the server.
5029+
*/
5030+
public static HashSet<ClientAuthenticationMethodModel> getSupportedAuthenticationMethods() {
5031+
5032+
List<OAuthClientAuthenticator> clientAuthenticators = OAuth2ServiceComponentHolder.getAuthenticationHandlers();
5033+
HashSet<ClientAuthenticationMethodModel> supportedClientAuthMethods = new HashSet<>();
50185034
for (OAuthClientAuthenticator clientAuthenticator : clientAuthenticators) {
5019-
List<String> supportedAuthMethods = clientAuthenticator.getSupportedClientAuthenticationMethods();
5035+
List<ClientAuthenticationMethodModel> supportedAuthMethods = clientAuthenticator
5036+
.getSupportedClientAuthenticationMethods();
50205037
if (!supportedAuthMethods.isEmpty()) {
50215038
supportedClientAuthMethods.addAll(supportedAuthMethods);
50225039
}
50235040
}
5024-
return supportedClientAuthMethods.toArray(new String[0]);
5041+
return supportedClientAuthMethods;
50255042
}
50265043

50275044
/**

components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/client/authentication/BasicAuthClientAuthenticatorTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
import org.wso2.carbon.identity.oauth.IdentityOAuthAdminException;
3434
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
3535
import org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext;
36+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
3637
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
3738
import org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest;
3839

3940
import java.io.File;
4041
import java.nio.file.Paths;
42+
import java.util.ArrayList;
4143
import java.util.HashMap;
4244
import java.util.List;
4345

@@ -289,7 +291,11 @@ public void testGetClientIdErrorScenario(String headerName, String headerValue,
289291
@Test
290292
public void testGetSupportedClientAuthenticationMethods() {
291293

292-
List<String> supportedAuthMethods = basicAuthClientAuthenticator.getSupportedClientAuthenticationMethods();
294+
List<String> supportedAuthMethods = new ArrayList<>();
295+
for (ClientAuthenticationMethodModel clientAuthenticationMethodModel : basicAuthClientAuthenticator
296+
.getSupportedClientAuthenticationMethods()) {
297+
supportedAuthMethods.add(clientAuthenticationMethodModel.getName());
298+
}
293299
Assert.assertTrue(supportedAuthMethods.contains("client_secret_basic"));
294300
Assert.assertTrue(supportedAuthMethods.contains("client_secret_post"));
295301
assertEquals(supportedAuthMethods.size(), 2);

components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/client/authentication/OAuthClientAuthnServiceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.wso2.carbon.identity.oauth.dao.OAuthAppDO;
3535
import org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext;
3636
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
37+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
3738
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
3839
import org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest;
3940

@@ -246,7 +247,7 @@ public void testAuthenticateWhenAuthMethodConfiguredInApp(boolean isFapiApp) thr
246247
PowerMockito.when(oAuthClientAuthenticator.getClientId(Mockito.any(), Mockito.any(), Mockito.any()))
247248
.thenReturn(CLIENT_ID);
248249
PowerMockito.when(oAuthClientAuthenticator.getSupportedClientAuthenticationMethods())
249-
.thenReturn(Arrays.asList("private_key_jwt"));
250+
.thenReturn(Arrays.asList(new ClientAuthenticationMethodModel("private_key_jwt", "Private Key JWT")));
250251
OAuthClientAuthnService oAuthClientAuthnService = Mockito.spy(OAuthClientAuthnService.class);
251252
PowerMockito.when(oAuthClientAuthnService.getClientAuthenticators()).thenReturn
252253
(Arrays.asList(oAuthClientAuthenticator));

components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/client/authentication/SampleClientAuthenticator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.wso2.carbon.identity.oauth2.client.authentication;
1818

1919
import org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext;
20+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
2021

2122
import java.util.Arrays;
2223
import java.util.List;
@@ -87,8 +88,9 @@ public boolean isEnabled() {
8788
}
8889

8990
@Override
90-
public List<String> getSupportedClientAuthenticationMethods() {
91+
public List<ClientAuthenticationMethodModel> getSupportedClientAuthenticationMethods() {
9192

92-
return Arrays.asList(SAMPLE_CLIENT_AUTHENTICATOR_AUTH_METHOD);
93+
return Arrays.asList(new ClientAuthenticationMethodModel(SAMPLE_CLIENT_AUTHENTICATOR_AUTH_METHOD,
94+
SAMPLE_CLIENT_AUTHENTICATOR));
9395
}
9496
}

components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/util/OAuth2UtilTest.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory;
8181
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
8282
import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
83+
import org.wso2.carbon.identity.oauth2.model.ClientAuthenticationMethodModel;
8384
import org.wso2.carbon.identity.oauth2.model.ClientCredentialDO;
8485
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
8586
import org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer;
@@ -2615,26 +2616,41 @@ public Object[][] clientAuthenticatorsDataProvider() {
26152616
@Test
26162617
public void testGetSupportedClientAuthMethods() {
26172618

2619+
ClientAuthenticationMethodModel secretBasic = new ClientAuthenticationMethodModel("client_secret_basic",
2620+
"Client Secret Basic");
2621+
ClientAuthenticationMethodModel secretPost = new ClientAuthenticationMethodModel("client_secret_post",
2622+
"Client Secret Post");
2623+
ClientAuthenticationMethodModel mtls = new ClientAuthenticationMethodModel("tls_client_auth",
2624+
"Mutual TLS");
2625+
ClientAuthenticationMethodModel pkJwt = new ClientAuthenticationMethodModel("private_key_jwt",
2626+
"Private Key JWT");
26182627
List<OAuthClientAuthenticator> clientAuthenticators = new ArrayList<>();
26192628
OAuthClientAuthenticator basicClientAuthenticator = PowerMockito.mock(OAuthClientAuthenticator.class);
26202629
PowerMockito.when(basicClientAuthenticator.getSupportedClientAuthenticationMethods())
2621-
.thenReturn(Arrays.asList("client_secret_basic", "client_secret_post"));
2630+
.thenReturn(Arrays.asList(secretBasic, secretPost));
26222631
clientAuthenticators.add(basicClientAuthenticator);
26232632
OAuthClientAuthenticator mtlsClientAuthenticator = PowerMockito.mock(OAuthClientAuthenticator.class);
26242633
PowerMockito.when(mtlsClientAuthenticator.getSupportedClientAuthenticationMethods())
2625-
.thenReturn(Arrays.asList("tls_client_auth"));
2634+
.thenReturn(Arrays.asList(mtls));
26262635
clientAuthenticators.add(mtlsClientAuthenticator);
26272636
OAuthClientAuthenticator pkjwtClientAuthenticator = PowerMockito.mock(OAuthClientAuthenticator.class);
26282637
PowerMockito.when(pkjwtClientAuthenticator.getSupportedClientAuthenticationMethods())
2629-
.thenReturn(Arrays.asList("private_key_jwt"));
2638+
.thenReturn(Arrays.asList(pkJwt));
26302639
clientAuthenticators.add(pkjwtClientAuthenticator);
26312640
mockStatic(OAuth2ServiceComponentHolder.class);
26322641
when(OAuth2ServiceComponentHolder.getAuthenticationHandlers()).thenReturn(clientAuthenticators);
2633-
List<String> supportedClientAuthMethods = Arrays.asList(OAuth2Util.getSupportedClientAuthMethods());
2634-
assertTrue(supportedClientAuthMethods.contains("client_secret_basic"));
2635-
assertTrue(supportedClientAuthMethods.contains("client_secret_post"));
2636-
assertTrue(supportedClientAuthMethods.contains("tls_client_auth"));
2637-
assertTrue(supportedClientAuthMethods.contains("private_key_jwt"));
2642+
HashSet<ClientAuthenticationMethodModel> supportedClientAuthMethods = OAuth2Util
2643+
.getSupportedAuthenticationMethods();
2644+
assertTrue(supportedClientAuthMethods.contains(secretBasic));
2645+
assertTrue(supportedClientAuthMethods.contains(secretPost));
2646+
assertTrue(supportedClientAuthMethods.contains(mtls));
2647+
assertTrue(supportedClientAuthMethods.contains(pkJwt));
26382648
assertEquals(supportedClientAuthMethods.size(), 4);
2649+
List<String> supportedAuthMethods = Arrays.asList(OAuth2Util.getSupportedClientAuthMethods());
2650+
assertTrue(supportedAuthMethods.contains("client_secret_basic"));
2651+
assertTrue(supportedAuthMethods.contains("client_secret_post"));
2652+
assertTrue(supportedAuthMethods.contains("tls_client_auth"));
2653+
assertTrue(supportedAuthMethods.contains("private_key_jwt"));
2654+
assertEquals(supportedAuthMethods.size(), 4);
26392655
}
26402656
}

0 commit comments

Comments
 (0)