001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2021, 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.assurance.evidences;
019
020
021import java.util.Objects;
022
023import net.jcip.annotations.Immutable;
024import net.minidev.json.JSONObject;
025
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028import com.nimbusds.openid.connect.sdk.assurance.Policy;
029import com.nimbusds.openid.connect.sdk.assurance.Procedure;
030import com.nimbusds.openid.connect.sdk.assurance.Status;
031
032
033/**
034 * Validation method establishing the authenticity of an evidence, such as a
035 * document, electronic record, vouch, utility bill or electronic signature.
036 *
037 * <p>Related specifications:
038 *
039 * <ul>
040 *     <li>OpenID Connect for Identity Assurance 1.0, section 5.1.1.
041 * </ul>
042 */
043@Immutable
044public final class ValidationMethod extends CommonMethodAttributes {
045        
046        
047        /**
048         * The type.
049         */
050        private final ValidationMethodType type;
051        
052        
053        /**
054         * Creates a new validation method.
055         *
056         * @param type      The type. Must not be {@code null}.
057         * @param policy    The policy, {@code null} if not specified.
058         * @param procedure The procedure, {@code null} if not specified.
059         * @param status    The status, {@code null} if not specified.
060         */
061        public ValidationMethod(final ValidationMethodType type,
062                                final Policy policy,
063                                final Procedure procedure,
064                                final Status status) {
065                
066                super(policy, procedure, status);
067                
068                Objects.requireNonNull(type);
069                this.type = type;
070        }
071        
072        
073        /**
074         * Returns the type of this validation method.
075         *
076         * @return The type.
077         */
078        public ValidationMethodType getType() {
079                return type;
080        }
081        
082        
083        @Override
084        public boolean equals(Object o) {
085                if (this == o) return true;
086                if (!(o instanceof ValidationMethod)) return false;
087                ValidationMethod that = (ValidationMethod) o;
088                return getType().equals(that.getType())
089                        && Objects.equals(getPolicy(), that.getPolicy())
090                        && Objects.equals(getProcedure(), that.getProcedure())
091                        && Objects.equals(getStatus(), that.getStatus());
092        }
093        
094        
095        @Override
096        public int hashCode() {
097                return Objects.hash(getType(), getPolicy(), getProcedure(), getStatus());
098        }
099        
100        
101        /**
102         * Returns a JSON object representation of this validation method.
103         *
104         * @return The JSON object.
105         */
106        @Override
107        public JSONObject toJSONObject() {
108                JSONObject o = super.toJSONObject();
109                o.put("type", getType().getValue());
110                return o;
111        }
112        
113        
114        /**
115         * Parses a validation method from the specified JSON object.
116         *
117         * @param jsonObject The JSON object. Must not be {@code null}.
118         *
119         * @return The validation method.
120         *
121         * @throws ParseException If parsing failed.
122         */
123        public static ValidationMethod parse(final JSONObject jsonObject)
124                throws ParseException {
125                
126                try {
127                        ValidationMethodType type = new ValidationMethodType(JSONObjectUtils.getString(jsonObject, "type"));
128                        Policy policy = null;
129                        if (jsonObject.get("policy") != null) {
130                                policy = new Policy(JSONObjectUtils.getString(jsonObject, "policy"));
131                        }
132                        Procedure procedure = null;
133                        if (jsonObject.get("procedure") != null) {
134                                procedure = new Procedure(JSONObjectUtils.getString(jsonObject, "procedure"));
135                        }
136                        Status status = null;
137                        if (jsonObject.get("status") != null) {
138                                status = new Status(JSONObjectUtils.getString(jsonObject, "status"));
139                        }
140                        return new ValidationMethod(type, policy, procedure, status);
141                } catch (IllegalArgumentException e) {
142                        throw new ParseException(e.getMessage(), e);
143                }
144        }
145}