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.List; 022import java.util.concurrent.atomic.AtomicBoolean; 023 024import com.nimbusds.oauth2.sdk.ParseException; 025import com.nimbusds.oauth2.sdk.util.JSONUtils; 026import com.nimbusds.openid.connect.sdk.federation.policy.language.*; 027 028 029/** 030 * Default (default) value operation. 031 * 032 * <p>Example policy: 033 * 034 * <pre> 035 * "require_auth_time": { "default" : true } 036 * </pre> 037 * 038 * <p>Related specifications: 039 * 040 * <ul> 041 * <li>OpenID Connect Federation 1.0, section 4.1.6. 042 * </ul> 043 */ 044public class DefaultOperation implements PolicyOperation, 045 BooleanConfiguration, StringConfiguration, StringListConfiguration, 046 UntypedOperation { 047 048 049 public static final OperationName NAME = new OperationName("default"); 050 051 052 private AtomicBoolean isInit = new AtomicBoolean(false); 053 054 055 private boolean booleanValue; 056 057 058 private String stringValue; 059 060 061 private List<String> stringListValue; 062 063 064 @Override 065 public OperationName getOperationName() { 066 return NAME; 067 } 068 069 070 @Override 071 public void configure(final boolean parameter) { 072 isInit.set(true); 073 this.booleanValue = parameter; 074 } 075 076 077 @Override 078 public void configure(final String parameter) { 079 isInit.set(true); 080 this.stringValue = parameter; 081 } 082 083 084 @Override 085 public void configure(final List<String> parameter) { 086 isInit.set(true); 087 this.stringListValue = parameter; 088 } 089 090 091 @Override 092 public void parseConfiguration(final Object jsonEntity) throws ParseException { 093 if (jsonEntity instanceof Boolean) { 094 configure(JSONUtils.toBoolean(jsonEntity)); 095 } else if (jsonEntity instanceof String) { 096 configure(JSONUtils.toString(jsonEntity)); 097 } else { 098 configure(JSONUtils.toStringList(jsonEntity)); 099 } 100 } 101 102 103 @Override 104 public boolean getBooleanConfiguration() { 105 return booleanValue; 106 } 107 108 109 @Override 110 public String getStringConfiguration() { 111 return stringValue; 112 } 113 114 115 @Override 116 public List<String> getStringListConfiguration() { 117 return stringListValue; 118 } 119 120 121 @Override 122 public PolicyOperation merge(final PolicyOperation other) 123 throws PolicyViolationException { 124 125 DefaultOperation otherTyped = Utils.castForMerge(other, DefaultOperation.class); 126 127 if (! isInit.get() || ! otherTyped.isInit.get()) { 128 throw new PolicyViolationException("The default operation is not initialized"); 129 } 130 131 if (getStringListConfiguration() != null) { 132 133 if (getStringListConfiguration().equals(otherTyped.getStringListConfiguration())) { 134 135 DefaultOperation copy = new DefaultOperation(); 136 copy.configure(getStringListConfiguration()); 137 return copy; 138 } 139 140 throw new PolicyViolationException("Default value mismatch"); 141 142 } else if (getStringConfiguration() != null) { 143 144 if (getStringConfiguration().equals(otherTyped.getStringConfiguration())) { 145 146 DefaultOperation copy = new DefaultOperation(); 147 copy.configure(getStringConfiguration()); 148 return copy; 149 } 150 151 throw new PolicyViolationException("Default value mismatch"); 152 153 } else if (getBooleanConfiguration() == otherTyped.getBooleanConfiguration()) { 154 155 DefaultOperation copy = new DefaultOperation(); 156 copy.configure(getBooleanConfiguration()); 157 return copy; 158 } else { 159 throw new PolicyViolationException("Default value mismatch"); 160 } 161 } 162 163 164 @Override 165 public Object apply(final Object value) { 166 167 if (! isInit.get()) { 168 throw new IllegalStateException("The policy is not initialized"); 169 } 170 171 if (value != null) { 172 return value; 173 } 174 175 // Return default value 176 177 if (stringListValue != null) { 178 return stringListValue; 179 } 180 if (stringValue != null) { 181 return stringValue; 182 } 183 return booleanValue; 184 } 185}