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.Objects;
022
023import net.minidev.json.JSONObject;
024
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
027import com.nimbusds.oauth2.sdk.util.date.DateWithTimeZoneOffset;
028
029
030/**
031 * Identity document used as identity evidence.
032 *
033 * <p>Related specifications:
034 *
035 * <ul>
036 *     <li>OpenID Connect for Identity Assurance 1.0, section 5.1.1.1.
037 * </ul>
038 *
039 * @deprecated Use {@link DocumentEvidence} instead.
040 */
041@Deprecated
042public class IDDocumentEvidence extends IdentityEvidence {
043        
044        
045        /**
046         * The document verification method.
047         */
048        private final IdentityVerificationMethod method;
049        
050        
051        /**
052         * The document verification timestamp.
053         */
054        private final DateWithTimeZoneOffset time;
055        
056        
057        /**
058         * Optional verifier if not the OpenID provider itself.
059         */
060        private final IdentityVerifier verifier;
061        
062        
063        /**
064         * The identity document description.
065         */
066        private final IDDocumentDescription idDocument;
067        
068        
069        /**
070         * Creates a new identity document evidence.
071         *
072         * @param method     The document verification method, {@code null} if
073         *                   not specified.
074         * @param verifier   Optional verifier if not the OpenID provider
075         *                   itself, {@code null} if none.
076         * @param time        The document verification timestamp, {@code null}
077         *                   if not specified.
078         * @param idDocument The identity document description, {@code null} if
079         *                   not specified.
080         */
081        public IDDocumentEvidence(final IdentityVerificationMethod method,
082                                  final IdentityVerifier verifier,
083                                  final DateWithTimeZoneOffset time,
084                                  final IDDocumentDescription idDocument) {
085                
086                super(IdentityEvidenceType.ID_DOCUMENT, null);
087                
088                this.method = method;
089                this.time = time;
090                this.verifier = verifier;
091                this.idDocument = idDocument;
092        }
093        
094        
095        /**
096         * Returns the document verification method.
097         *
098         * @return The document verification method, {@code null} if not
099         *         specified.
100         */
101        public IdentityVerificationMethod getVerificationMethod() {
102                return method;
103        }
104        
105        
106        /**
107         * Returns the document verification timestamp.
108         *
109         * @return The document verification timestamp, {@code null} if not
110         *         specified.
111         */
112        public DateWithTimeZoneOffset getVerificationTime() {
113                return time;
114        }
115        
116        
117        /**
118         * Returns the optional verifier if not the OpenID provider itself.
119         *
120         * @return The optional verifier if not the OpenID provider itself,
121         *         {@code null} if none.
122         */
123        public IdentityVerifier getVerifier() {
124                return verifier;
125        }
126        
127        
128        /**
129         * Returns the identity document description.
130         *
131         * @return The identity document description, {@code null} if not
132         *         specified.
133         */
134        public IDDocumentDescription getIdentityDocument() {
135                return idDocument;
136        }
137        
138        
139        @Override
140        public JSONObject toJSONObject() {
141                JSONObject o = super.toJSONObject();
142                if (getVerificationMethod() != null) {
143                        o.put("method", getVerificationMethod().getValue());
144                }
145                if (getVerificationTime() != null) {
146                        o.put("time", getVerificationTime().toISO8601String());
147                }
148                if (getVerifier() != null) {
149                        o.put("verifier", getVerifier().toJSONObject());
150                }
151                if (getIdentityDocument() != null) {
152                        o.put("document", getIdentityDocument().toJSONObject());
153                }
154                return o;
155        }
156        
157        
158        @Override
159        public boolean equals(Object o) {
160                if (this == o) return true;
161                if (!(o instanceof IDDocumentEvidence)) return false;
162                IDDocumentEvidence that = (IDDocumentEvidence) o;
163                return Objects.equals(getVerificationMethod(), that.getVerificationMethod()) &&
164                        Objects.equals(getVerificationTime(), that.getVerificationTime()) &&
165                        Objects.equals(getVerifier(), that.getVerifier()) &&
166                        Objects.equals(getIdentityDocument(), that.getIdentityDocument());
167        }
168        
169        
170        @Override
171        public int hashCode() {
172                return Objects.hash(method, time, getVerifier(), idDocument);
173        }
174        
175        
176        /**
177         * Parses an identity document used as identity evidence from the
178         * specified JSON object.
179         *
180         * @param jsonObject The JSON object. Must not be {@code null}.
181         *
182         * @return The identity document used as identity evidence.
183         *
184         * @throws ParseException If parsing failed.
185         */
186        public static IDDocumentEvidence parse(final JSONObject jsonObject)
187                throws ParseException {
188                
189                ensureType(IdentityEvidenceType.ID_DOCUMENT, jsonObject);
190                
191                IdentityVerificationMethod method = null;
192                if (jsonObject.get("method") != null) {
193                        method = new IdentityVerificationMethod(JSONObjectUtils.getString(jsonObject, "method"));
194                }
195                
196                DateWithTimeZoneOffset dtz = null;
197                if (jsonObject.get("time") != null) {
198                        dtz = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "time"));
199                }
200                
201                IdentityVerifier verifier = null;
202                if (jsonObject.get("verifier") != null) {
203                        verifier = IdentityVerifier.parse(JSONObjectUtils.getJSONObject(jsonObject, "verifier"));
204                }
205                
206                IDDocumentDescription idDocument = null;
207                if (jsonObject.get("document") != null) {
208                        idDocument = IDDocumentDescription.parse(JSONObjectUtils.getJSONObject(jsonObject, "document"));
209                }
210                
211                return new IDDocumentEvidence(method, verifier, dtz, idDocument);
212        }
213}