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