77import com .auth0 .msg .RegistrationResponse ;
88import com .google .common .base .Strings ;
99import java .util .HashMap ;
10+ import java .util .List ;
1011import java .util .Map ;
1112import javax .naming .ConfigurationException ;
1213import org .oidc .common .OidcServiceException ;
14+ import org .oidc .service .AbstractService ;
1315import org .oidc .service .base .ServiceContext ;
1416import org .oidc .service .data .State ;
1517import org .oidc .service .util .Constants ;
18+ import org .slf4j .Logger ;
19+ import org .slf4j .LoggerFactory ;
1620
1721public 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}
0 commit comments