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.assurance.evidences;
019
020
021import java.util.List;
022import java.util.Objects;
023
024import net.minidev.json.JSONObject;
025
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028import com.nimbusds.oauth2.sdk.util.date.DateWithTimeZoneOffset;
029import com.nimbusds.openid.connect.sdk.assurance.evidences.attachment.Attachment;
030
031
032/**
033 * Vouch used as identity evidence.
034 *
035 * <p>Related specifications:
036 *
037 * <ul>
038 *     <li>OpenID Connect for Identity Assurance 1.0, section 5.1.1.3.
039 * </ul>
040 */
041public class VouchEvidence extends IdentityEvidence {
042        
043        
044        /**
045         * The vouch validation method.
046         */
047        private final ValidationMethod validationMethod;
048        
049        
050        /**
051         * The person verification method.
052         */
053        private final VerificationMethod verificationMethod;
054        
055        
056        /**
057         * The identity verifier if not the OpenID provider itself.
058         */
059        private final IdentityVerifier verifier;
060        
061        
062        /**
063         * The vouch verification timestamp.
064         */
065        private final DateWithTimeZoneOffset time;
066        
067        
068        /**
069         * The attestation details.
070         */
071        private final Attestation attestation;
072        
073        
074        /**
075         * Creates a new vouch evidence.
076         *
077         * @param validationMethod   The vouch validation method, {@code null} 
078         *                           if not specified.
079         * @param verificationMethod The person verification method,
080         *                           {@code null} if not specified.
081         * @param verifier           Optional verifier if not the OpenID
082         *                           provider itself, {@code null} if none.
083         * @param time               The vouch verification timestamp,
084         *                           {@code null} if not specified.
085         * @param attestation        The attestation, {@code null} if not
086         *                           specified.
087         * @param attachments        The optional attachments, {@code null} if
088         *                           not specified.
089         */
090        public VouchEvidence(final ValidationMethod validationMethod,
091                             final VerificationMethod verificationMethod,
092                             final IdentityVerifier verifier,
093                             final DateWithTimeZoneOffset time,
094                             final Attestation attestation,
095                             final List<Attachment> attachments) {
096                super(IdentityEvidenceType.VOUCH, attachments);
097                this.validationMethod = validationMethod;
098                this.verificationMethod = verificationMethod;
099                this.time = time;
100                this.verifier = verifier;
101                this.attestation = attestation;
102        }
103        
104        
105        /**
106         * Returns the vouch validation method.
107         *
108         * @return The vouch validation method, {@code null} if not
109         *         specified.
110         */
111        public ValidationMethod getValidationMethod() {
112                return validationMethod;
113        }
114        
115        
116        /**
117         * Returns the person verification method.
118         *
119         * @return The person verification method, {@code null} if not
120         *         specified.
121         */
122        public VerificationMethod getVerificationMethod() {
123                return verificationMethod;
124        }
125        
126        
127        /**
128         * Returns the optional verifier if not the OpenID provider itself.
129         *
130         * @return The optional verifier if not the OpenID provider itself,
131         *         {@code null} if none.
132         */
133        public IdentityVerifier getVerifier() {
134                return verifier;
135        }
136        
137        
138        /**
139         * Returns the vouch verification timestamp.
140         *
141         * @return The vouch verification timestamp, {@code null} if not
142         *         specified.
143         */
144        public DateWithTimeZoneOffset getVerificationTime() {
145                return time;
146        }
147        
148        
149        /**
150         * Returns the attestation.
151         *
152         * @return The attestation, {@code null} if not specified.
153         */
154        public Attestation getAttestation() {
155                return attestation;
156        }
157        
158        
159        @Override
160        public JSONObject toJSONObject() {
161                JSONObject o = super.toJSONObject();
162                if (getValidationMethod() != null) {
163                        o.put("validation_method", getValidationMethod().toJSONObject());
164                }
165                if (getVerificationMethod() != null) {
166                        o.put("verification_method", getVerificationMethod().toJSONObject());
167                }
168                if (getVerifier() != null) {
169                        o.put("verifier", getVerifier().toJSONObject());
170                }
171                if (getVerificationTime() != null) {
172                        o.put("time", getVerificationTime().toISO8601String());
173                }
174                if (getAttestation() != null) {
175                        o.put("attestation", getAttestation().toJSONObject());
176                }
177                return o;
178        }
179        
180        
181        @Override
182        public boolean equals(Object o) {
183                if (this == o) return true;
184                if (!(o instanceof VouchEvidence)) return false;
185                VouchEvidence that = (VouchEvidence) o;
186                return Objects.equals(getValidationMethod(), that.getValidationMethod()) &&
187                        Objects.equals(getVerificationMethod(), that.getVerificationMethod()) &&
188                        Objects.equals(getVerifier(), that.getVerifier()) &&
189                        Objects.equals(getVerificationTime(), that.getVerificationTime()) &&
190                        Objects.equals(getAttestation(), that.getAttestation());
191        }
192        
193        
194        @Override
195        public int hashCode() {
196                return Objects.hash(
197                        getValidationMethod(),
198                        getVerificationMethod(),
199                        getVerifier(),
200                        getVerificationTime(),
201                        getAttestation()
202                );
203        }
204        
205        
206        /**
207         * Parses a vouch evidence from the specified JSON object.
208         *
209         * @param jsonObject The JSON object. Must not be {@code null}.
210         *
211         * @return The vouch evidence.
212         *
213         * @throws ParseException If parsing failed.
214         */
215        public static VouchEvidence parse(final JSONObject jsonObject)
216                throws ParseException {
217                
218                ensureType(IdentityEvidenceType.VOUCH, jsonObject);
219                
220                ValidationMethod validationMethod = null;
221                if (jsonObject.get("validation_method") != null) {
222                        validationMethod = ValidationMethod.parse(JSONObjectUtils.getJSONObject(jsonObject, "validation_method"));
223                }
224                
225                VerificationMethod verificationMethod = null;
226                if (jsonObject.get("verification_method") != null) {
227                        verificationMethod = VerificationMethod.parse(JSONObjectUtils.getJSONObject(jsonObject, "verification_method"));
228                }
229                
230                IdentityVerifier verifier = null;
231                if (jsonObject.get("verifier") != null) {
232                        verifier = IdentityVerifier.parse(JSONObjectUtils.getJSONObject(jsonObject, "verifier"));
233                }
234                
235                DateWithTimeZoneOffset time = null;
236                if (jsonObject.get("time") != null) {
237                        time = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "time"));
238                }
239                
240                Attestation attestation = null;
241                if (jsonObject.get("attestation") != null) {
242                        attestation = Attestation.parse(JSONObjectUtils.getJSONObject(jsonObject, "attestation"));
243                }
244                
245                List<Attachment> attachments = null;
246                if (jsonObject.get("attachments") != null) {
247                        attachments = Attachment.parseList(JSONObjectUtils.getJSONArray(jsonObject, "attachments"));
248                }
249                
250                return new VouchEvidence(validationMethod, verificationMethod, verifier, time, attestation, attachments);
251        }
252}