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. Must not be {@code null}. 086 * @param issuerName The issuer name. Must not be {@code null}. 087 * @param issuerCountry The issuer country. Must not be {@code null}. 088 * @param dateOfIssuance The date of issuance. Must not be 089 * {@code null}. 090 * @param dateOfExpiry The date of expiry. Must not be {@code null}. 091 */ 092 public IDDocumentDescription(final IDDocumentType type, 093 final String number, 094 final String issuerName, 095 final CountryCode issuerCountry, 096 final SimpleDate dateOfIssuance, 097 final SimpleDate dateOfExpiry) { 098 099 if (type == null) { 100 throw new IllegalArgumentException("The type must not be null"); 101 } 102 this.type = type; 103 104 if (number == null) { 105 throw new IllegalArgumentException("The number must not be null"); 106 } 107 this.number = number; 108 109 if (issuerName == null) { 110 throw new IllegalArgumentException("The issuer name must not be null"); 111 } 112 this.issuerName = issuerName; 113 114 if (issuerCountry == null) { 115 throw new IllegalArgumentException("The issuer country must not be null"); 116 } 117 this.issuerCountry = issuerCountry; 118 119 if (dateOfIssuance == null) { 120 throw new IllegalArgumentException("The date of issuance must not be null"); 121 } 122 this.dateOfIssuance = dateOfIssuance; 123 124 if (dateOfExpiry == null) { 125 throw new IllegalArgumentException("The date of expiry must not be null"); 126 } 127 this.dateOfExpiry = dateOfExpiry; 128 } 129 130 131 /** 132 * Returns the identity document type. 133 * 134 * @return The identity document type. 135 */ 136 public IDDocumentType getType() { 137 return type; 138 } 139 140 141 /** 142 * Returns the identity document number. 143 * 144 * @return The identity document number. 145 */ 146 public String getNumber() { 147 return number; 148 } 149 150 151 /** 152 * Returns the issuer name. 153 * 154 * @return The issuer name. 155 */ 156 public String getIssuerName() { 157 return issuerName; 158 } 159 160 161 /** 162 * Returns the issuer country. 163 * 164 * @return The issuer country code. 165 */ 166 public CountryCode getIssuerCountry() { 167 return issuerCountry; 168 } 169 170 171 /** 172 * Returns the date of issuance. 173 * 174 * @return The date of issuance. 175 */ 176 public SimpleDate getDateOfIssuance() { 177 return dateOfIssuance; 178 } 179 180 181 /** 182 * Returns the date of expiry. 183 * 184 * @return The date of expiry. 185 */ 186 public SimpleDate getDateOfExpiry() { 187 return dateOfExpiry; 188 } 189 190 191 /** 192 * Returns a JSON object representation of this identity document 193 * description. 194 * 195 * @return The JSON object. 196 */ 197 public JSONObject toJSONObject() { 198 JSONObject o = new JSONObject(); 199 o.put("type", getType().getValue()); 200 o.put("number", getNumber()); // TODO https://bitbucket.org/openid/connect/issues/1123/assurance-4111-id_document-document-number 201 JSONObject issuerObject = new JSONObject(); 202 issuerObject.put("name", getIssuerName()); 203 issuerObject.put("country", getIssuerCountry().getValue()); 204 o.put("issuer", issuerObject); 205 o.put("date_of_issuance", getDateOfIssuance().toISO8601String()); 206 o.put("date_of_expiry", getDateOfExpiry().toISO8601String()); 207 return o; 208 } 209 210 211 @Override 212 public String toJSONString() { 213 return toJSONObject().toJSONString(); 214 } 215 216 217 @Override 218 public String toString() { 219 return toJSONString(); 220 } 221 222 223 @Override 224 public boolean equals(Object o) { 225 if (this == o) return true; 226 if (!(o instanceof IDDocumentDescription)) return false; 227 IDDocumentDescription that = (IDDocumentDescription) o; 228 return getType().equals(that.getType()) && 229 getNumber().equals(that.getNumber()) && 230 getIssuerName().equals(that.getIssuerName()) && 231 getIssuerCountry().equals(that.getIssuerCountry()) && 232 getDateOfIssuance().equals(that.getDateOfIssuance()) && 233 getDateOfExpiry().equals(that.getDateOfExpiry()); 234 } 235 236 237 @Override 238 public int hashCode() { 239 return Objects.hash(getType(), getNumber(), getIssuerName(), getIssuerCountry(), getDateOfIssuance(), getDateOfExpiry()); 240 } 241 242 243 /** 244 * Parses an identity document description from the specified JSON 245 * object. 246 * 247 * @param jsonObject The JSON object. Must not be {@code null}. 248 * 249 * @return The identity document description. 250 * 251 * @throws ParseException If parsing failed. 252 */ 253 public static IDDocumentDescription parse(final JSONObject jsonObject) 254 throws ParseException { 255 256 IDDocumentType type = new IDDocumentType(JSONObjectUtils.getString(jsonObject, "type")); 257 String number = JSONObjectUtils.getString(jsonObject, "number"); 258 259 JSONObject issuerObject = JSONObjectUtils.getJSONObject(jsonObject, "issuer"); 260 String issuerName = JSONObjectUtils.getString(issuerObject, "name"); 261 CountryCode issuerCountry = ISO3166_1Alpha2CountryCode.parse(JSONObjectUtils.getString(issuerObject, "country")); 262 263 SimpleDate dateOfIssuance = SimpleDate.parseISO8601String(JSONObjectUtils.getString(jsonObject, "date_of_issuance")); 264 SimpleDate dateOfExpiry = SimpleDate.parseISO8601String(JSONObjectUtils.getString(jsonObject, "date_of_expiry")); 265 266 return new IDDocumentDescription(type, number, issuerName, issuerCountry, dateOfIssuance, dateOfExpiry); 267 } 268}