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 com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
023import com.nimbusds.oauth2.sdk.util.date.SimpleDate;
024import net.minidev.json.JSONObject;
025
026import java.util.Objects;
027
028
029/**
030 * Document details.
031 *
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OpenID Connect for Identity Assurance 1.0
036 * </ul>
037 */
038public class DocumentDetails {
039        
040        
041        /**
042         * The document type.
043         */
044        private final DocumentType type;
045        
046        
047        /**
048         * The document number.
049         */
050        private final DocumentNumber documentNumber;
051        
052        
053        /**
054         * The personal number.
055         */
056        private final PersonalNumber personalNumber;
057        
058        
059        /**
060         * The serial number.
061         */
062        private final SerialNumber serialNumber;
063        
064        
065        /**
066         * The date of issuance.
067         */
068        private final SimpleDate dateOfIssuance;
069        
070        
071        /**
072         * The date of expiry.
073         */
074        private final SimpleDate dateOfExpiry;
075        
076        
077        /**
078         * The document issuer information.
079         */
080        private final DocumentIssuer issuer;
081        
082        
083        /**
084         * Creates a new document details instance.
085         *
086         * @param type           The document type. Must not be {@code null}.
087         * @param documentNumber The document number, {@code null} if not
088         *                       specified.
089         * @param personalNumber The personal number, {@code null} if not
090         *                       specified.
091         * @param serialNumber   The serial number, {@code null} if not
092         *                       specified.
093         * @param dateOfIssuance The date of issuance, {@code null} if not
094         *                       specified.
095         * @param dateOfExpiry   The date of expiry, {@code null} if not
096         *                       specified.
097         * @param issuer         The document issuer information, {@code null}
098         *                       if not specified.
099         */
100        public DocumentDetails(final DocumentType type,
101                               final DocumentNumber documentNumber,
102                               final PersonalNumber personalNumber,
103                               final SerialNumber serialNumber,
104                               final SimpleDate dateOfIssuance,
105                               final SimpleDate dateOfExpiry,
106                               final DocumentIssuer issuer) {
107                Objects.requireNonNull(type);
108                this.type = type;
109                this.documentNumber = documentNumber;
110                this.personalNumber = personalNumber;
111                this.serialNumber = serialNumber;
112                this.dateOfIssuance = dateOfIssuance;
113                this.dateOfExpiry = dateOfExpiry;
114                this.issuer = issuer;
115        }
116        
117        
118        /**
119         * Returns the document type.
120         *
121         * @return The document type.
122         */
123        public DocumentType getType() {
124                return type;
125        }
126        
127        
128        /**
129         * Returns the document number.
130         *
131         * @return The document number, {@code null} if not specified.
132         */
133        public DocumentNumber getDocumentNumber() {
134                return documentNumber;
135        }
136        
137        
138        /**
139         * Returns the personal number.
140         *
141         * @return The personal number, {@code null} if not specified.
142         */
143        public PersonalNumber getPersonalNumber() {
144                return personalNumber;
145        }
146        
147        
148        /**
149         * Returns the serial number.
150         *
151         * @return The serial number, {@code null} if not specified.
152         */
153        public SerialNumber getSerialNumber() {
154                return serialNumber;
155        }
156        
157        
158        /**
159         * Returns the date of issuance.
160         *
161         * @return The date of issuance, {@code null} if not specified.
162         */
163        public SimpleDate getDateOfIssuance() {
164                return dateOfIssuance;
165        }
166        
167        
168        /**
169         * Returns the date of expiry.
170         *
171         * @return The date of expiry, {@code null} if not specified.
172         */
173        public SimpleDate getDateOfExpiry() {
174                return dateOfExpiry;
175        }
176        
177        
178        /**
179         * Returns the document issuer information.
180         *
181         * @return The document issuer information, {@code null} if not
182         *         specified.
183         */
184        public DocumentIssuer getIssuer() {
185                return issuer;
186        }
187        
188        
189        /**
190         * Returns a JSON object representation of this document details
191         * instance.
192         *
193         * @return The JSON object.
194         */
195        public JSONObject toJSONObject() {
196                JSONObject o = new JSONObject();
197                o.put("type", getType().getValue());
198                if (getDocumentNumber() != null) {
199                        o.put("document_number", getDocumentNumber().getValue());
200                }
201                if (getPersonalNumber() != null) {
202                        o.put("personal_number", getPersonalNumber().getValue());
203                }
204                if (getSerialNumber() != null) {
205                        o.put("serial_number", getSerialNumber().getValue());
206                }
207                if (getDateOfIssuance() != null) {
208                        o.put("date_of_issuance", getDateOfIssuance().toISO8601String());
209                }
210                if (getDateOfExpiry() != null) {
211                        o.put("date_of_expiry", getDateOfExpiry().toISO8601String());
212                }
213                if (getIssuer() != null) {
214                        JSONObject issuerObject = getIssuer().toJSONObject();
215                        if (! issuerObject.isEmpty()) {
216                                o.put("issuer", issuerObject);
217                        }
218                }
219                return o;
220        }
221        
222        
223        @Override
224        public boolean equals(Object o) {
225                if (this == o) return true;
226                if (!(o instanceof DocumentDetails)) return false;
227                DocumentDetails that = (DocumentDetails) o;
228                return getType().equals(that.getType()) &&
229                        Objects.equals(getDocumentNumber(), that.getDocumentNumber()) &&
230                        Objects.equals(getPersonalNumber(), that.getPersonalNumber()) &&
231                        Objects.equals(getSerialNumber(), that.getSerialNumber()) &&
232                        Objects.equals(getDateOfIssuance(), that.getDateOfIssuance()) &&
233                        Objects.equals(getDateOfExpiry(), that.getDateOfExpiry()) &&
234                        Objects.equals(getIssuer(), that.getIssuer());
235        }
236        
237        
238        @Override
239        public int hashCode() {
240                return Objects.hash(
241                        getType(),
242                        getDocumentNumber(),
243                        getPersonalNumber(),
244                        getSerialNumber(),
245                        getDateOfIssuance(),
246                        getDateOfExpiry(),
247                        getIssuer()
248                );
249        }
250        
251        
252        /**
253         * Parses a document details instance from the specified JSON object.
254         *
255         * @param jsonObject The JSON object. Must not be {@code null}.
256         *
257         * @return The document details instance.
258         *
259         * @throws ParseException If parsing failed.
260         */
261        public static DocumentDetails parse(final JSONObject jsonObject)
262                throws ParseException {
263
264                DocumentType type = new DocumentType(JSONObjectUtils.getNonBlankString(jsonObject, "type"));
265
266                DocumentNumber documentNumber = null;
267                if (jsonObject.get("document_number") != null) {
268                        documentNumber = new DocumentNumber(JSONObjectUtils.getNonBlankString(jsonObject, "document_number"));
269                }
270
271                PersonalNumber personalNumber = null;
272                if (jsonObject.get("personal_number") != null) {
273                        personalNumber = new PersonalNumber(JSONObjectUtils.getNonBlankString(jsonObject, "personal_number"));
274                }
275
276                SerialNumber serialNumber = null;
277                if (jsonObject.get("serial_number") != null) {
278                        serialNumber = new SerialNumber(JSONObjectUtils.getNonBlankString(jsonObject, "serial_number"));
279                }
280
281                SimpleDate dateOfIssuance = null;
282                if (jsonObject.get("date_of_issuance") != null) {
283                        dateOfIssuance = SimpleDate.parseISO8601String(JSONObjectUtils.getNonBlankString(jsonObject, "date_of_issuance"));
284                }
285
286                SimpleDate dateOfExpiry = null;
287                if (jsonObject.get("date_of_expiry") != null) {
288                        dateOfExpiry = SimpleDate.parseISO8601String(JSONObjectUtils.getNonBlankString(jsonObject, "date_of_expiry"));
289                }
290
291                DocumentIssuer issuer = null;
292                if (jsonObject.get("issuer") != null) {
293                        issuer = DocumentIssuer.parse(JSONObjectUtils.getJSONObject(jsonObject, "issuer"));
294                }
295
296                return new DocumentDetails(type, documentNumber, personalNumber, serialNumber, dateOfIssuance, dateOfExpiry, issuer);
297        }
298}