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 com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.util.JSONUtils;
023import com.nimbusds.openid.connect.sdk.federation.policy.language.*;
024
025
026/**
027 * Default (default) value operation.
028 *
029 * <p>Example policy:
030 *
031 * <pre>
032 * "tos_uri" : { "essential" : true }
033 * </pre>
034 *
035 * <p>Related specifications:
036 *
037 * <ul>
038 *     <li>OpenID Connect Federation 1.0, section 4.1.7.
039 * </ul>
040 */
041public class EssentialOperation implements PolicyOperation, BooleanConfiguration, UntypedOperation {
042        
043        
044        public static final OperationName NAME = new OperationName("essential");
045        
046        
047        private boolean enable = false;
048        
049        
050        @Override
051        public OperationName getOperationName() {
052                return NAME;
053        }
054        
055        
056        @Override
057        public void configure(final boolean enable) {
058                this.enable = enable;
059        }
060        
061        
062        @Override
063        public void parseConfiguration(final Object jsonEntity) throws ParseException {
064                
065                configure(JSONUtils.toBoolean(jsonEntity));
066        }
067        
068        
069        @Override
070        public boolean getBooleanConfiguration() {
071                return enable;
072        }
073        
074        
075        @Override
076        public PolicyOperation merge(final PolicyOperation other)
077                throws PolicyViolationException {
078                
079                EssentialOperation otherTyped = Utils.castForMerge(other, EssentialOperation.class);
080                
081                if (getBooleanConfiguration() == otherTyped.getBooleanConfiguration()) {
082                        EssentialOperation copy = new EssentialOperation();
083                        copy.configure(getBooleanConfiguration());
084                        return copy;
085                }
086                
087                throw new PolicyViolationException("Essential value mismatch");
088        }
089        
090        
091        @Override
092        public Object apply(final Object value) throws PolicyViolationException {
093        
094                if (enable && value == null) {
095                        throw new PolicyViolationException("Essential parameter not present");
096                }
097                
098                return value;
099        }
100}