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 * Electronic record 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.2.
039 * </ul>
040 */
041public class ElectronicRecordEvidence extends IdentityEvidence {
042        
043        
044        /**
045         * The electronic record 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 electronic record verification timestamp.
064         */
065        private final DateWithTimeZoneOffset time;
066        
067        
068        /**
069         * The electronic record details.
070         */
071        private final ElectronicRecordDetails recordDetails;
072        
073        
074        /**
075         * Creates a new electronic record evidence.
076         *
077         * @param validationMethod   The eletronic record validation method,
078         *                           {@code null} 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 electronic record verification
084         *                           timestamp, {@code null} if not specified.
085         * @param recordDetails      The electronic record details,
086         *                           {@code null} if not specified.
087         * @param attachments        The optional attachments, {@code null} if
088         *                           not specified.
089         */
090        public ElectronicRecordEvidence(final ValidationMethod validationMethod,
091                                        final VerificationMethod verificationMethod,
092                                        final IdentityVerifier verifier,
093                                        final DateWithTimeZoneOffset time,
094                                        final ElectronicRecordDetails recordDetails,
095                                        final List<Attachment> attachments) {
096                super(IdentityEvidenceType.ELECTRONIC_RECORD, attachments);
097                this.validationMethod = validationMethod;
098                this.verificationMethod = verificationMethod;
099                this.time = time;
100                this.verifier = verifier;
101                this.recordDetails = recordDetails;
102        }
103        
104        
105        /**
106         * Returns the electronic record validation method.
107         *
108         * @return The electronic record 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 electronic record verification timestamp.
140         *
141         * @return The electronic record verification timestamp, {@code null}
142         *         if not specified.
143         */
144        public DateWithTimeZoneOffset getVerificationTime() {
145                return time;
146        }
147        
148        
149        /**
150         * Returns the electronic record details.
151         *
152         * @return The electronic record details, {@code null} if not
153         *          specified.
154         */
155        public ElectronicRecordDetails getRecordDetails() {
156                return recordDetails;
157        }
158        
159        
160        @Override
161        public JSONObject toJSONObject() {
162                JSONObject o = super.toJSONObject();
163                if (getValidationMethod() != null) {
164                        o.put("validation_method", getValidationMethod().toJSONObject());
165                }
166                if (getVerificationMethod() != null) {
167                        o.put("verification_method", getVerificationMethod().toJSONObject());
168                }
169                if (getVerifier() != null) {
170                        o.put("verifier", getVerifier().toJSONObject());
171                }
172                if (getVerificationTime() != null) {
173                        o.put("time", getVerificationTime().toISO8601String());
174                }
175                if (getRecordDetails() != null) {
176                        o.put("record", getRecordDetails().toJSONObject());
177                }
178                return o;
179        }
180        
181        
182        @Override
183        public boolean equals(Object o) {
184                if (this == o) return true;
185                if (!(o instanceof ElectronicRecordEvidence)) return false;
186                ElectronicRecordEvidence that = (ElectronicRecordEvidence) o;
187                return Objects.equals(getValidationMethod(), that.getValidationMethod()) &&
188                        Objects.equals(getVerificationMethod(), that.getVerificationMethod()) &&
189                        Objects.equals(getVerifier(), that.getVerifier()) &&
190                        Objects.equals(getVerificationTime(), that.getVerificationTime()) &&
191                        Objects.equals(getRecordDetails(), that.getRecordDetails());
192        }
193        
194        
195        @Override
196        public int hashCode() {
197                return Objects.hash(getValidationMethod(), getVerificationMethod(), getVerifier(), getVerificationTime(), getRecordDetails());
198        }
199        
200        
201        /**
202         * Parses an electronic record evidence from the specified JSON object.
203         *
204         * @param jsonObject The JSON object. Must not be {@code null}.
205         *
206         * @return The electronic record evidence.
207         *
208         * @throws ParseException If parsing failed.
209         */
210        public static ElectronicRecordEvidence parse(final JSONObject jsonObject)
211                throws ParseException {
212                
213                ensureType(IdentityEvidenceType.ELECTRONIC_RECORD, jsonObject);
214                
215                ValidationMethod validationMethod = null;
216                if (jsonObject.get("validation_method") != null) {
217                        validationMethod = ValidationMethod.parse(JSONObjectUtils.getJSONObject(jsonObject, "validation_method"));
218                }
219                
220                VerificationMethod verificationMethod = null;
221                if (jsonObject.get("verification_method") != null) {
222                        verificationMethod = VerificationMethod.parse(JSONObjectUtils.getJSONObject(jsonObject, "verification_method"));
223                }
224                IdentityVerifier verifier = null;
225                if (jsonObject.get("verifier") != null) {
226                        verifier = IdentityVerifier.parse(JSONObjectUtils.getJSONObject(jsonObject, "verifier"));
227                }
228                DateWithTimeZoneOffset dtz = null;
229                if (jsonObject.get("time") != null) {
230                        dtz = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "time"));
231                }
232                
233                ElectronicRecordDetails recordDetails = null;
234                if (jsonObject.get("record") != null) {
235                        recordDetails = ElectronicRecordDetails.parse(JSONObjectUtils.getJSONObject(jsonObject, "record"));
236                }
237                
238                List<Attachment> attachments = null;
239                if (jsonObject.get("attachments") != null) {
240                        attachments = Attachment.parseList(JSONObjectUtils.getJSONArray(jsonObject, "attachments"));
241                }
242                
243                return new ElectronicRecordEvidence(validationMethod, verificationMethod, verifier, dtz, recordDetails, attachments);
244        }
245}