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 * Document 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.1.
039 * </ul>
040 */
041public class DocumentEvidence extends IdentityEvidence {
042        
043        
044        /**
045         * The document 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 verification method.
058         */
059        @Deprecated
060        private final IdentityVerificationMethod method;
061        
062        
063        /**
064         * The identity verifier if not the OpenID provider itself.
065         */
066        private final IdentityVerifier verifier;
067        
068        
069        /**
070         * The document verification timestamp.
071         */
072        private final DateWithTimeZoneOffset time;
073        
074        
075        /**
076         * The document details.
077         */
078        private final DocumentDetails documentDetails;
079        
080        
081        /**
082         * Creates a new document evidence.
083         *
084         * @param validationMethod   The document validation method,
085         *                           {@code null} if not specified.
086         * @param verificationMethod The person verification method,
087         *                           {@code null} if not specified.
088         * @param method             The alternative coarse identity
089         *                           verification method, {@code null} if not
090         *                           specified.
091         * @param verifier           Optional verifier if not the OpenID
092         *                           provider itself, {@code null} if none.
093         * @param time                The document verification timestamp,
094         *                           {@code null} if not specified.
095         * @param documentDetails    The document details, {@code null} if not
096         *                           specified.
097         * @param attachments        The optional attachments, {@code null} if
098         *                           not specified.
099         */
100        public DocumentEvidence(final ValidationMethod validationMethod,
101                                final VerificationMethod verificationMethod,
102                                final IdentityVerificationMethod method,
103                                final IdentityVerifier verifier,
104                                final DateWithTimeZoneOffset time,
105                                final DocumentDetails documentDetails,
106                                final List<Attachment> attachments) {
107                super(IdentityEvidenceType.DOCUMENT, attachments);
108                this.validationMethod = validationMethod;
109                this.verificationMethod = verificationMethod;
110                this.method = method;
111                this.time = time;
112                this.verifier = verifier;
113                this.documentDetails = documentDetails;
114        }
115        
116        
117        /**
118         * Returns the document validation method.
119         *
120         * @return The document validation method, {@code null} if not
121         *         specified.
122         */
123        public ValidationMethod getValidationMethod() {
124                return validationMethod;
125        }
126        
127        
128        /**
129         * Returns the person verification method.
130         *
131         * @return The person verification method, {@code null} if not
132         *         specified.
133         */
134        public VerificationMethod getVerificationMethod() {
135                return verificationMethod;
136        }
137        
138        
139        /**
140         * Returns the alternative coarse identity verification method.
141         *
142         * @return The identity verification method, {@code null} if not
143         *         specified.
144         */
145        @Deprecated
146        public IdentityVerificationMethod getMethod() {
147                return method;
148        }
149        
150        
151        /**
152         * Returns the optional verifier if not the OpenID provider itself.
153         *
154         * @return The optional verifier if not the OpenID provider itself,
155         *         {@code null} if none.
156         */
157        public IdentityVerifier getVerifier() {
158                return verifier;
159        }
160        
161        
162        /**
163         * Returns the document verification timestamp.
164         *
165         * @return The document verification timestamp, {@code null} if not
166         *         specified.
167         */
168        public DateWithTimeZoneOffset getVerificationTime() {
169                return time;
170        }
171        
172        
173        /**
174         * Returns the document details.
175         *
176         * @return The document details, {@code null} if not specified.
177         */
178        public DocumentDetails getDocumentDetails() {
179                return documentDetails;
180        }
181        
182        
183        @Override
184        public JSONObject toJSONObject() {
185                JSONObject o = super.toJSONObject();
186                if (getValidationMethod() != null) {
187                        o.put("validation_method", getValidationMethod().toJSONObject());
188                }
189                if (getVerificationMethod() != null) {
190                        o.put("verification_method", getVerificationMethod().toJSONObject());
191                }
192                if (getMethod() != null) {
193                        o.put("method", getMethod().getValue());
194                }
195                if (getVerifier() != null) {
196                        o.put("verifier", getVerifier().toJSONObject());
197                }
198                if (getVerificationTime() != null) {
199                        o.put("time", getVerificationTime().toISO8601String());
200                }
201                if (getDocumentDetails() != null) {
202                        o.put("document_details", getDocumentDetails().toJSONObject());
203                }
204                return o;
205        }
206        
207        
208        @Override
209        public boolean equals(Object o) {
210                if (this == o) return true;
211                if (!(o instanceof DocumentEvidence)) return false;
212                DocumentEvidence that = (DocumentEvidence) o;
213                return Objects.equals(getValidationMethod(), that.getValidationMethod()) &&
214                        Objects.equals(getVerificationMethod(), that.getVerificationMethod()) &&
215                        Objects.equals(getMethod(), that.getMethod()) &&
216                        Objects.equals(getVerifier(), that.getVerifier()) &&
217                        Objects.equals(getVerificationTime(), that.getVerificationTime()) &&
218                        Objects.equals(getDocumentDetails(), that.getDocumentDetails());
219        }
220        
221        
222        @Override
223        public int hashCode() {
224                return Objects.hash(getValidationMethod(), getVerificationMethod(), getMethod(), getVerifier(), getVerificationTime(), getDocumentDetails());
225        }
226        
227        
228        /**
229         * Parses a document evidence from the specified JSON object.
230         *
231         * @param jsonObject The JSON object. Must not be {@code null}.
232         *
233         * @return The document evidence.
234         *
235         * @throws ParseException If parsing failed.
236         */
237        public static DocumentEvidence parse(final JSONObject jsonObject)
238                throws ParseException {
239                
240                ensureType(IdentityEvidenceType.DOCUMENT, jsonObject);
241                
242                ValidationMethod validationMethod = null;
243                if (jsonObject.get("validation_method") != null) {
244                        validationMethod = ValidationMethod.parse(JSONObjectUtils.getJSONObject(jsonObject, "validation_method"));
245                }
246                
247                VerificationMethod verificationMethod = null;
248                if (jsonObject.get("verification_method") != null) {
249                        verificationMethod = VerificationMethod.parse(JSONObjectUtils.getJSONObject(jsonObject, "verification_method"));
250                }
251                IdentityVerificationMethod method = null;
252                if (jsonObject.get("method") != null) {
253                        method = new IdentityVerificationMethod(JSONObjectUtils.getString(jsonObject, "method"));
254                }
255                IdentityVerifier verifier = null;
256                if (jsonObject.get("verifier") != null) {
257                        verifier = IdentityVerifier.parse(JSONObjectUtils.getJSONObject(jsonObject, "verifier"));
258                }
259                DateWithTimeZoneOffset dtz = null;
260                if (jsonObject.get("time") != null) {
261                        dtz = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "time"));
262                }
263                
264                DocumentDetails documentDetails = null;
265                if (jsonObject.get("document_details") != null) {
266                        documentDetails = DocumentDetails.parse(JSONObjectUtils.getJSONObject(jsonObject, "document_details"));
267                }
268                
269                List<Attachment> attachments = null;
270                if (jsonObject.get("attachments") != null) {
271                        attachments = Attachment.parseList(JSONObjectUtils.getJSONArray(jsonObject, "attachments"));
272                }
273                
274                return new DocumentEvidence(validationMethod, verificationMethod, method, verifier, dtz, documentDetails, attachments);
275        }
276}