Skip to content

Commit 147cf7f

Browse files
author
Justin Dahmubed
committed
This is compileable
1 parent 1360a66 commit 147cf7f

10 files changed

Lines changed: 120 additions & 45 deletions

src/java/org/oauth2/ProviderConfigurationResponseDiscovery.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
import com.auth0.msg.KeyJar;
77
import com.auth0.msg.Message;
88
import com.auth0.msg.ProviderConfigurationResponse;
9+
import com.auth0.msg.ProviderConfigurationResponseDiscoveryRequestMessage;
910
import com.google.common.base.Strings;
11+
import java.util.Arrays;
12+
import java.util.List;
1013
import java.util.Map;
14+
import javax.naming.ConfigurationException;
15+
import org.oidc.common.EndpointName;
1116
import org.oidc.common.HttpMethod;
1217
import org.oidc.common.MissingRequiredAttributeException;
1318
import org.oidc.common.OidcServiceException;
@@ -31,14 +36,24 @@ public ProviderConfigurationResponseDiscovery(ServiceContext serviceContext,
3136
super(serviceContext, state);
3237
this.config = config;
3338
this.serviceName = ServiceName.PROVIDER_INFO_DISCOVERY;
34-
//this.requestMessage = ;
39+
this.requestMessage = new ProviderConfigurationResponseDiscoveryRequestMessage();
3540
this.responseMessage = new ASConfigurationResponse();
3641
}
3742

3843
public ProviderConfigurationResponseDiscovery(ServiceContext serviceContext) {
3944
this(serviceContext, null, null);
4045
}
4146

47+
/**
48+
* Builds the request message and constructs the HTTP headers.
49+
* <p>
50+
* This is the starting pont for a pipeline that will:
51+
* <p>
52+
* - gather a set of HTTP headers like url and http method
53+
*
54+
* @param httpMethod
55+
* @return HttpArguments
56+
*/
4257
public HttpArguments getRequestParameters(HttpMethod httpMethod) {
4358
String issuer = this.serviceContext.getIssuer();
4459

@@ -69,7 +84,7 @@ public HttpArguments getRequestParameters() {
6984
*
7085
* @param response the response as a ProviderConfigurationResponse instance
7186
*/
72-
public void updateServiceContext(ProviderConfigurationResponse response) throws OidcServiceException, InvalidClaimException {
87+
public void updateServiceContext(ProviderConfigurationResponse response) throws OidcServiceException, InvalidClaimException, ConfigurationException {
7388

7489
String issuer = this.serviceContext.getIssuer();
7590

@@ -99,16 +114,10 @@ public void updateServiceContext(ProviderConfigurationResponse response) throws
99114

100115
ProviderConfigurationResponse pcr = this.serviceContext.getProviderConfigurationResponse();
101116
Map<String,Object> pcrClaims = pcr.getClaims();
102-
for(String key : pcrClaims.keySet()) {
103-
if(!Strings.isNullOrEmpty(key) && pcrClaims.get(key) instanceof ServiceName) {
104-
//todo: where are we getting the service from the SC from?
105-
106-
//service is ServiceName is enum from AbstractService
107-
/* todo
108-
for _srv in self.service_context.service.values():
109-
if _srv.endpoint_name == key:
110-
_srv.endpoint = val
111-
*/
117+
List<EndpointName> endpointList = Arrays.asList(EndpointName.AUTHORIZATION, EndpointName.TOKEN, EndpointName.USER_INFO, EndpointName.REGISTRATION, EndpointName.USER_INFO, EndpointName.END_SESSION);
118+
for(EndpointName endpoint : endpointList) {
119+
if(this.getEndpointName().equals(endpoint)) {
120+
this.setEndpoint((String) pcrClaims.get(endpoint));
112121
}
113122
}
114123

@@ -117,10 +126,7 @@ public void updateServiceContext(ProviderConfigurationResponse response) throws
117126
keyJar = new KeyJar();
118127
}
119128

120-
keyJar.addKeyBundle();
121-
keyJar.getKeyBundle().addKey(new Key());
122-
//todo: where are we loading keys?
123-
//kj.load_keys(resp, _pcr_issuer)
129+
keyJar.loadKeys(response, this.serviceContext.getIssuer());
124130
this.serviceContext.setKeyJar(keyJar);
125131
}
126132

src/java/org/oidc/common/EndpointName.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* The name of the endpoint on the server that the request should be sent to
55
*/
66
public enum EndpointName {
7-
AUTHORIZATION, TOKEN, REGISTRATION, USER_INFO;
7+
AUTHORIZATION, TOKEN, REGISTRATION, USER_INFO, END_SESSION;
88
}

src/java/org/oidc/service/AbstractService.java

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

33
import com.auth0.msg.InvalidClaimException;
44
import com.auth0.msg.Message;
5+
import com.auth0.msg.SerializationException;
56
import com.fasterxml.jackson.core.JsonProcessingException;
67
import com.google.common.base.Strings;
78
import java.io.UnsupportedEncodingException;
@@ -278,7 +279,7 @@ public Message parseResponse(
278279
*/
279280
public HttpArguments getRequestParameters(Map<String, String> requestArguments) throws UnsupportedSerializationTypeException,
280281
JsonProcessingException, MissingRequiredAttributeException, MalformedURLException, WebFingerException, ValueException,
281-
UnsupportedEncodingException {
282+
UnsupportedEncodingException, SerializationException {
282283
if (requestArguments == null) {
283284
throw new IllegalArgumentException("null requestArguments");
284285
}

src/java/org/oidc/service/Service.java

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

33
import com.auth0.msg.InvalidClaimException;
44
import com.auth0.msg.Message;
5+
import com.auth0.msg.SerializationException;
56
import com.fasterxml.jackson.core.JsonProcessingException;
67
import java.io.UnsupportedEncodingException;
78
import java.net.MalformedURLException;
@@ -34,7 +35,7 @@ public interface Service {
3435
* @param requestArguments
3536
* @return HttpArguments
3637
*/
37-
HttpArguments getRequestParameters(Map<String, String> requestArguments) throws UnsupportedSerializationTypeException, JsonProcessingException, MissingRequiredAttributeException, MalformedURLException, WebFingerException, ValueException, UnsupportedEncodingException;
38+
HttpArguments getRequestParameters(Map<String, String> requestArguments) throws UnsupportedSerializationTypeException, JsonProcessingException, MissingRequiredAttributeException, MalformedURLException, WebFingerException, ValueException, UnsupportedEncodingException, SerializationException;
3839

3940
/**
4041
This the start of a pipeline that will:

src/java/org/oidc/service/util/ServiceUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.oidc.service.util;
22

33
import com.auth0.msg.Message;
4+
import com.auth0.msg.SerializationException;
45
import com.fasterxml.jackson.core.JsonProcessingException;
56
import com.google.common.base.Strings;
67
import java.net.MalformedURLException;
@@ -46,7 +47,7 @@ public static String getUrlInfo(String url) throws MalformedURLException {
4647
* @return the request serialized according to the passed in serialization type
4748
* @throws UnsupportedSerializationTypeException
4849
*/
49-
public static String getHttpBody(Message request, SerializationType serializationType) throws UnsupportedSerializationTypeException, JsonProcessingException {
50+
public static String getHttpBody(Message request, SerializationType serializationType) throws UnsupportedSerializationTypeException, JsonProcessingException, SerializationException {
5051
if (SerializationType.URL_ENCODED.equals(serializationType)) {
5152
return request.toUrlEncoded();
5253
} else if (SerializationType.JSON.equals(serializationType)) {

src/java/org/oidc/services/ProviderConfigurationResponseDiscovery.java

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77
import com.auth0.msg.RegistrationResponse;
88
import com.google.common.base.Strings;
99
import java.util.HashMap;
10+
import java.util.List;
1011
import java.util.Map;
1112
import javax.naming.ConfigurationException;
1213
import org.oidc.common.OidcServiceException;
14+
import org.oidc.service.AbstractService;
1315
import org.oidc.service.base.ServiceContext;
1416
import org.oidc.service.data.State;
1517
import org.oidc.service.util.Constants;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1620

1721
public class ProviderConfigurationResponseDiscovery extends org.oauth2.ProviderConfigurationResponseDiscovery{
1822

23+
private static final Logger logger = LoggerFactory.getLogger(ProviderConfigurationResponseDiscovery.class);
24+
1925
public ProviderConfigurationResponseDiscovery(ServiceContext serviceContext,
2026
State state,
2127
ProviderConfigResponseDiscoveryServiceConfig config) {
@@ -34,15 +40,20 @@ public ProviderConfigurationResponseDiscovery(ServiceContext serviceContext) {
3440
*
3541
* @param response the response as a ProviderConfigurationResponse instance
3642
*/
37-
public void updateServiceContext(ProviderConfigurationResponse response) throws OidcServiceException, ConfigurationException, InvalidClaimException {
43+
public void updateServiceContext(ProviderConfigurationResponse response) throws OidcServiceException, InvalidClaimException, ConfigurationException {
3844
super.updateServiceContext(response);
3945
this.matchPreferences(response);
40-
Jwks jwks; //todo: whats contained in a jwks?
46+
Jwks jwks;
4147
if(config.getPreLoadKeys() != null && !config.getPreLoadKeys().isEmpty()) {
4248
jwks = this.serviceContext.getKeyJar().exportJwksAsJson((String) this.responseMessage.getClaims().get("issuer"));
49+
logger.info("Preloaded keys for %s: %s", response.getClaims().get("issuer"), jwks.toString());
4350
}
4451
}
4552

53+
/**
54+
* Gets OIDC url which includes issuer
55+
* @return OIDC url which includes issuer
56+
*/
4657
public String getEndpoint() {
4758
String issuer = this.serviceContext.getIssuer();
4859
if(Strings.isNullOrEmpty(issuer)) {
@@ -56,31 +67,36 @@ public String getEndpoint() {
5667
return String.format(Constants.OIDC_PATTERN, issuer);
5768
}
5869

70+
/**
71+
* Match the clients preferences against what the provider can do.
72+
* This is to prepare for later client registration and or what
73+
* functionality the client actually will use.
74+
* In the client configuration the client preferences are expressed.
75+
* These are then compared with the ProviderConfigurationResponse.
76+
* If the Provider has left some claims out, defaults specified in the
77+
* standard will be used.
78+
*
79+
* @param response
80+
* @throws ConfigurationException
81+
* @throws InvalidClaimException
82+
*/
5983
public void matchPreferences(ProviderConfigurationResponse response) throws ConfigurationException, InvalidClaimException {
6084
if(response == null) {
6185
response = this.serviceContext.getProviderConfigurationResponse();
6286
}
6387

64-
//todo: how are we going to add the claims with this current implementation?
65-
//RegistrationRequest has a ton of claims that need to be added
66-
RegistrationRequest registrationRequest = new RegistrationRequest();
88+
RegistrationRequest registrationRequest = serviceContext.getConfig().getRegistrationRequest();
6789
String pcrValues = null;
90+
Object value;
6891
for(String key : Constants.PREFERENCE_TO_PROVIDER.keySet()) {
69-
values = this.serviceContext.getClientPreferences().getClaims().get(key);
92+
value = this.serviceContext.getClientPreferences().getClaims().get(key);
7093

7194
if("tokenEndpointAuthMethod".equals(key)) {
7295
pcrValues = "clientSecretBasic";
7396
} else if("idTokenSignedResponseAlg".equals(key)) {
7497
pcrValues = "RS256";
7598
}
7699

77-
if(pcrValues == null) {
78-
//todo: replacement for strictOnPrefs?
79-
if(this.serviceContext) {
80-
throw new ConfigurationException("OP couldn't match preferences: " + key);
81-
}
82-
}
83-
84100
Object values = this.serviceContext.getClientPreferences().getClaims().get(key);
85101
if(values instanceof String) {
86102
if(pcrValues.contains((String) values)) {
@@ -90,16 +106,64 @@ public void matchPreferences(ProviderConfigurationResponse response) throws Conf
90106
this.serviceContext.setBehavior(registrationResponse);
91107
}
92108
} else {
93-
Object value = registrationRequest.getClaims().get(key);
109+
Object registrationRequestValue = registrationRequest.getClaims().get(key);
110+
RegistrationResponse registrationResponse;
111+
if(values instanceof List) {
112+
List<String> listOfValues = (List<String>) values;
113+
if (registrationRequestValue instanceof List) {
114+
Map<String,Object> claims = new HashMap<>();
115+
claims.put(key, null);
116+
registrationResponse = new RegistrationResponse(claims);
117+
this.serviceContext.setBehavior(registrationResponse);
118+
for (Object valueIndex : listOfValues) {
119+
if (pcrValues.contains((String) valueIndex)) {
120+
claims = new HashMap<>();
121+
List<String> listOfValuesFromClaims = (List<String>) claims.get(key);
122+
listOfValuesFromClaims.add((String) valueIndex);
123+
claims.put(key, listOfValuesFromClaims);
124+
registrationResponse = new RegistrationResponse(claims);
125+
this.serviceContext.setBehavior(registrationResponse);
126+
}
127+
}
128+
}
129+
} else {
130+
List<String> listOfValues = (List<String>) values;
131+
for (Object valueIndex : listOfValues) {
132+
if (pcrValues.contains((String) valueIndex)) {
133+
Map<String,Object> claims = new HashMap<>();
134+
claims.put(key, (String) valueIndex);
135+
registrationResponse = new RegistrationResponse(claims);
136+
this.serviceContext.setBehavior(registrationResponse);
137+
}
138+
}
139+
}
140+
141+
if(!this.serviceContext.getBehavior().getClaims().containsKey(key)) {
142+
throw new ConfigurationException("OP couldn't match preferences: " + key);
143+
}
94144
}
95145
}
96146

97147
Map<String,Object> claims = this.serviceContext.getClientPreferences().getClaims();
148+
Object valueFromClaims;
98149
for(String key : claims.keySet()) {
150+
valueFromClaims = claims.get(key);
99151
if(!this.serviceContext.getBehavior().getClaims().containsKey(key)) {
100-
registrationRequest.getClaims().get(key);
152+
Object registrationRequestValue = registrationRequest.getClaims().get(key);
153+
if(registrationRequestValue instanceof List) {
154+
List<String> valuesList = (List<String>) registrationRequestValue;
155+
valueFromClaims = valuesList.get(0);
156+
}
157+
}
158+
if(!Constants.PREFERENCE_TO_PROVIDER.containsKey(key)) {
159+
claims = new HashMap<>();
160+
claims.put(key, valueFromClaims);
161+
RegistrationResponse registrationResponse = new RegistrationResponse(claims);
162+
this.serviceContext.setBehavior(registrationResponse);
101163
}
102164
}
165+
166+
logger.debug("ServiceContext behavior: " + this.serviceContext.getBehavior());
103167
}
104168

105169
}

src/test/java/org/oauth2/services/ProviderConfigurationResponseDiscoveryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testGetRequestParametersWithIssuerEndingInForwardSlash() {
3939
serviceContext.setIssuer("issuer/");
4040
ProviderConfigurationResponseDiscovery pcrd = new ProviderConfigurationResponseDiscovery(serviceContext);
4141
HttpArguments httpArguments = pcrd.getRequestParameters();
42-
Assert.assertTrue(httpArguments.getUrl().equals());
42+
//Assert.assertTrue(httpArguments.getUrl().equals());
4343
Assert.assertTrue(httpArguments.getHttpMethod().equals(HttpMethod.GET));
4444
}
4545

src/test/java/org/oidc/service/base/ServiceContextTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.oidc.service.base;
22

3+
import com.auth0.msg.InvalidClaimException;
34
import com.auth0.msg.Key;
45
import com.auth0.msg.KeyBundle;
56
import com.auth0.msg.KeyJar;
@@ -101,7 +102,7 @@ public void testFileNameFromWebnameWhereWebNameDoesntStartsWithForwardSlash() th
101102
}
102103

103104
@Test
104-
public void testGenerateRequestUrisWithNullIssuer() throws NoSuchAlgorithmException, ValueException {
105+
public void testGenerateRequestUrisWithNullIssuer() throws NoSuchAlgorithmException, ValueException, InvalidClaimException {
105106
ServiceContext serviceContext = new ServiceContext();
106107
serviceContext.setIssuer("issuer");
107108
serviceContext.setBaseUrl("baseUrl");
@@ -115,7 +116,7 @@ public void testGenerateRequestUrisWithNullIssuer() throws NoSuchAlgorithmExcept
115116
}
116117

117118
@Test
118-
public void testGenerateRequestUrisWithForwardSlash() throws NoSuchAlgorithmException, ValueException {
119+
public void testGenerateRequestUrisWithForwardSlash() throws NoSuchAlgorithmException, ValueException, InvalidClaimException {
119120
ServiceContext serviceContext = new ServiceContext();
120121
serviceContext.setIssuer("issuer");
121122
serviceContext.setBaseUrl("baseUrl");
@@ -133,7 +134,7 @@ public void testGenerateRequestUrisWithForwardSlash() throws NoSuchAlgorithmExce
133134
* @throws NoSuchAlgorithmException
134135
*/
135136
@Test
136-
public void testGenerateRequestUrisWithMultipleClaimsForPCR() throws NoSuchAlgorithmException, ValueException {
137+
public void testGenerateRequestUrisWithMultipleClaimsForPCR() throws NoSuchAlgorithmException, ValueException, InvalidClaimException {
137138
ServiceContext serviceContext = new ServiceContext();
138139
serviceContext.setIssuer("issuer");
139140
serviceContext.setBaseUrl("baseUrl");
@@ -147,7 +148,7 @@ public void testGenerateRequestUrisWithMultipleClaimsForPCR() throws NoSuchAlgor
147148
}
148149

149150
@Test
150-
public void testGenerateRequestUrisWithoutForwardSlash() throws NoSuchAlgorithmException, ValueException {
151+
public void testGenerateRequestUrisWithoutForwardSlash() throws NoSuchAlgorithmException, ValueException, InvalidClaimException {
151152
ServiceContext serviceContext = new ServiceContext();
152153
serviceContext.setIssuer("issuer");
153154
serviceContext.setBaseUrl("baseUrl");

src/test/java/org/oidc/service/util/ServiceUtilTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.auth0.msg.Message;
66
import com.auth0.msg.ProviderConfigurationResponse;
7+
import com.auth0.msg.SerializationException;
78
import com.fasterxml.jackson.core.JsonProcessingException;
89
import java.util.HashMap;
910
import java.util.Map;
@@ -49,7 +50,7 @@ public void testGetUrlQueryReferenceWithQueryExcluded() throws Exception{
4950
}
5051

5152
@Test
52-
public void testGetHttpBodyWithSerializationTypeUrlEncoded() throws UnsupportedSerializationTypeException, JsonProcessingException {
53+
public void testGetHttpBodyWithSerializationTypeUrlEncoded() throws UnsupportedSerializationTypeException, JsonProcessingException, SerializationException {
5354
Map<String,Object> claims = new HashMap<>();
5455
claims.put(Constants.ISSUER, "issuer");
5556
Message request = new ProviderConfigurationResponse(claims);
@@ -58,7 +59,7 @@ public void testGetHttpBodyWithSerializationTypeUrlEncoded() throws UnsupportedS
5859
}
5960

6061
@Test
61-
public void testGetHttpBodyWithSerializationTypeJson() throws UnsupportedSerializationTypeException, JsonProcessingException {
62+
public void testGetHttpBodyWithSerializationTypeJson() throws UnsupportedSerializationTypeException, JsonProcessingException, SerializationException {
6263
Map<String,Object> claims = new HashMap<>();
6364
claims.put(Constants.ISSUER, "issuer");
6465
Message request = new ProviderConfigurationResponse(claims);
@@ -67,7 +68,7 @@ public void testGetHttpBodyWithSerializationTypeJson() throws UnsupportedSeriali
6768
}
6869

6970
@Test
70-
public void testGetHttpBodyWithIncorrectSerializationType() throws UnsupportedSerializationTypeException, JsonProcessingException {
71+
public void testGetHttpBodyWithIncorrectSerializationType() throws UnsupportedSerializationTypeException, JsonProcessingException, SerializationException {
7172
thrown.expect(UnsupportedSerializationTypeException.class);
7273
thrown.expectMessage(containsString("Unsupported serialization type: "));
7374
Map<String,Object> claims = new HashMap<>();

0 commit comments

Comments
 (0)