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