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 * Verification method establishing a given user owns a set of provided claims.
035 *
036 * <p>Related specifications:
037 *
038 * <ul>
039 *     <li>OpenID Connect for Identity Assurance 1.0, section 5.1.1.
040 * </ul>
041 */
042@Immutable
043public final class VerificationMethod extends CommonMethodAttributes {
044        
045        
046        /**
047         * The verification method type.
048         */
049        private final VerificationMethodType type;
050        
051        
052        /**
053         * Creates a new verification method.
054         *
055         * @param type      The type. Must not be {@code null}.
056         * @param policy    The policy, {@code null} if not specified.
057         * @param procedure The procedure, {@code null} if not specified.
058         * @param status    The status, {@code null} if not specified.
059         */
060        public VerificationMethod(final VerificationMethodType type,
061                                  final Policy policy,
062                                  final Procedure procedure,
063                                  final Status status) {
064                
065                super(policy, procedure, status);
066                
067                Objects.requireNonNull(type);
068                this.type = type;
069        }
070        
071        
072        /**
073         * Returns the type of this verification method.
074         *
075         * @return The type.
076         */
077        public VerificationMethodType getType() {
078                return type;
079        }
080        
081        
082        @Override
083        public boolean equals(Object o) {
084                if (this == o) return true;
085                if (!(o instanceof VerificationMethod)) return false;
086                VerificationMethod that = (VerificationMethod) o;
087                return getType().equals(that.getType())
088                        && Objects.equals(getPolicy(), that.getPolicy())
089                        && Objects.equals(getProcedure(), that.getProcedure())
090                        && Objects.equals(getStatus(), that.getStatus());
091        }
092        
093        
094        @Override
095        public int hashCode() {
096                return Objects.hash(getType(), getPolicy(), getProcedure(), getStatus());
097        }
098        
099        
100        /**
101         * Returns a JSON object representation of this verification method.
102         *
103         * @return The JSON object.
104         */
105        @Override
106        public JSONObject toJSONObject() {
107                JSONObject o = super.toJSONObject();
108                o.put("type", getType().getValue());
109                return o;
110        }
111        
112        
113        /**
114         * Parses a verification method from the specified JSON object.
115         *
116         * @param jsonObject The JSON object. Must not be {@code null}.
117         *
118         * @return The verification method.
119         *
120         * @throws ParseException If parsing failed.
121         */
122        public static VerificationMethod parse(final JSONObject jsonObject)
123                throws ParseException {
124                
125                try {
126                        VerificationMethodType type = new VerificationMethodType(JSONObjectUtils.getString(jsonObject, "type"));
127                        Policy policy = null;
128                        if (jsonObject.get("policy") != null) {
129                                policy = new Policy(JSONObjectUtils.getString(jsonObject, "policy"));
130                        }
131                        Procedure procedure = null;
132                        if (jsonObject.get("procedure") != null) {
133                                procedure = new Procedure(JSONObjectUtils.getString(jsonObject, "procedure"));
134                        }
135                        Status status = null;
136                        if (jsonObject.get("status") != null) {
137                                status = new Status(JSONObjectUtils.getString(jsonObject, "status"));
138                        }
139                        return new VerificationMethod(type, policy, procedure, status);
140                } catch (IllegalArgumentException e) {
141                        throw new ParseException(e.getMessage(), e);
142                }
143        }
144}