001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2020, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.openid.connect.sdk.federation.policy.factories;
019
020
021import java.util.*;
022
023import net.jcip.annotations.ThreadSafe;
024import net.minidev.json.JSONObject;
025
026import com.nimbusds.openid.connect.sdk.federation.policy.MetadataPolicy;
027import com.nimbusds.openid.connect.sdk.federation.policy.MetadataPolicyEntry;
028import com.nimbusds.openid.connect.sdk.federation.policy.language.PolicyOperation;
029import com.nimbusds.openid.connect.sdk.federation.policy.operations.ValueOperation;
030import com.nimbusds.openid.connect.sdk.rp.OIDCClientInformation;
031import com.nimbusds.openid.connect.sdk.rp.OIDCClientMetadata;
032
033
034/**
035 * The default OpenID relying party metadata policy factory.
036 *
037 * @deprecated Explicit client registration in OpenID Connect Federation 1.0 no
038 * longer uses metadata policies in the explicit client registration response.
039 */
040@ThreadSafe
041@Deprecated
042public class DefaultRPMetadataPolicyFactory implements RPMetadataPolicyFactory {
043        
044        
045        @Override
046        public MetadataPolicy create(final OIDCClientMetadata initialMetadata, final OIDCClientInformation target)
047                throws PolicyFormulationException {
048                
049                MetadataPolicy policy = new MetadataPolicy();
050                
051                JSONObject initialJSONObject = initialMetadata.toJSONObject();
052                
053                for (Map.Entry<String,Object> en: target.toJSONObject().entrySet()) {
054                        
055                        if (en.equals(new AbstractMap.SimpleImmutableEntry<>(en.getKey(), initialJSONObject.get(en.getKey())))) {
056                                // No policy entry needed
057                                continue;
058                        }
059                        
060                        // Set (override) value
061                        MetadataPolicyEntry policyEntry = new MetadataPolicyEntry(
062                                en.getKey(),
063                                Collections.singletonList((PolicyOperation) createValueOperation(en))
064                        );
065                        
066                        policy.put(policyEntry);
067                }
068                
069                return policy;
070        }
071        
072        
073        private static ValueOperation createValueOperation(final Map.Entry<String,Object> objectEntry)
074                throws PolicyFormulationException {
075                
076                ValueOperation valueOperation = new ValueOperation();
077                
078                if (objectEntry.getValue() instanceof String) {
079                        valueOperation.configure((String)objectEntry.getValue());
080                        
081                } else if (objectEntry.getValue() instanceof Boolean) {
082                        valueOperation.configure((Boolean)objectEntry.getValue());
083                        
084                } else if (objectEntry.getValue() instanceof Number) {
085                        valueOperation.configure((Number)objectEntry.getValue());
086                        
087                } else if (objectEntry.getValue() instanceof List) {
088                        // assume string list
089                        List<String> stringList = new LinkedList<>();
090                        
091                        for (Object item : (List<?>) objectEntry.getValue()) {
092                                if (item instanceof String) {
093                                        stringList.add((String) item);
094                                } else {
095                                        stringList.add(null);
096                                }
097                        }
098                        valueOperation.configure(stringList);
099                } else if (objectEntry.getValue() == null) {
100                        valueOperation.configure((String)null);
101                } else {
102                        throw new PolicyFormulationException("Unsupported type for " + objectEntry.getKey() + ": " + objectEntry.getValue().getClass() + ": " + objectEntry.getValue());
103                }
104                
105                return valueOperation;
106        }
107        
108}