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