001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, 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.operations; 019 020 021import java.util.*; 022 023import com.nimbusds.openid.connect.sdk.federation.policy.language.OperationName; 024import com.nimbusds.openid.connect.sdk.federation.policy.language.PolicyOperation; 025import com.nimbusds.openid.connect.sdk.federation.policy.language.PolicyViolationException; 026import com.nimbusds.openid.connect.sdk.federation.policy.language.StringOperation; 027 028 029/** 030 * One-of (one_of) operation. 031 * 032 * <p>Example policy: 033 * 034 * <pre> 035 * "request_object_signing_alg" : { "one_of" : [ "ES256", "ES384", "ES512" ] } 036 * </pre> 037 * 038 * <p>Input: 039 * 040 * <pre> 041 * "request_object_signing_alg" : "ES384" 042 * </pre> 043 * 044 * <p>Result: 045 * 046 * <pre> 047 * "request_object_signing_alg" : "ES384" 048 * </pre> 049 * 050 * <p>Related specifications: 051 * 052 * <ul> 053 * <li>OpenID Connect Federation 1.0, section 4.1.2. 054 * </ul> 055 */ 056public class OneOfOperation extends AbstractSetBasedOperation implements StringOperation { 057 058 059 public static final OperationName NAME = new OperationName("one_of"); 060 061 062 @Override 063 public OperationName getOperationName() { 064 return NAME; 065 } 066 067 068 @Override 069 public Map.Entry<String, Object> toJSONObjectEntry() { 070 if (configType == null) { 071 throw new IllegalStateException("The policy is not initialized"); 072 } 073 return new AbstractMap.SimpleImmutableEntry<>(getOperationName().getValue(), (Object) getStringListConfiguration()); 074 } 075 076 077 @Override 078 public PolicyOperation merge(final PolicyOperation other) throws PolicyViolationException { 079 080 OneOfOperation otherTyped = Utils.castForMerge(other, OneOfOperation.class); 081 082 // intersect 083 Set<String> combinedConfig = new LinkedHashSet<>(setConfig); 084 combinedConfig.retainAll(otherTyped.getStringListConfiguration()); 085 086 OneOfOperation mergedPolicy = new OneOfOperation(); 087 mergedPolicy.configure(new LinkedList<>(combinedConfig)); 088 return mergedPolicy; 089 } 090 091 092 @Override 093 public String apply(final String value) 094 throws PolicyViolationException { 095 096 if (setConfig == null) { 097 throw new IllegalStateException("The policy is not initialized"); 098 } 099 100 if (value == null) { 101 throw new PolicyViolationException("Value not set"); 102 } 103 104 if (! setConfig.contains(value)) { 105 throw new PolicyViolationException("Value " + value + " not in policy list: " + setConfig); 106 } 107 108 return value; 109 } 110}