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.DateWithTimeZoneOffset; 028import com.nimbusds.oauth2.sdk.util.date.SimpleDate; 029 030 031/** 032 * Electronic record details. 033 * 034 * <p>Related specifications: 035 * 036 * <ul> 037 * <li>OpenID Connect for Identity Assurance 1.0, section 5.1.1.2. 038 * </ul> 039 */ 040public class ElectronicRecordDetails { 041 042 043 /** 044 * The electronic record type. 045 */ 046 private final ElectronicRecordType type; 047 048 049 /** 050 * The personal number. 051 */ 052 private final PersonalNumber personalNumber; 053 054 055 /** 056 * The time of creation. 057 */ 058 private final DateWithTimeZoneOffset createdAt; 059 060 061 /** 062 * The date of expiry. 063 */ 064 private final SimpleDate dateOfExpiry; 065 066 067 /** 068 * The electronic record source. 069 */ 070 private final ElectronicRecordSource source; 071 072 073 /** 074 * Creates a new electronic record details instance. 075 * 076 * @param type The electronic record type. Must not be 077 * {@code null}. 078 * @param personalNumber The personal number, {@code null} if not 079 * specified. 080 * @param createdAt The time of creation, {@code null} if not 081 * specified. 082 * @param dateOfExpiry The date of expiry, {@code null} if not 083 * specified. 084 * @param source The electronic record source, {@code null} if 085 * not specified. 086 */ 087 public ElectronicRecordDetails(final ElectronicRecordType type, 088 final PersonalNumber personalNumber, 089 final DateWithTimeZoneOffset createdAt, 090 final SimpleDate dateOfExpiry, 091 final ElectronicRecordSource source) { 092 Objects.requireNonNull(type); 093 this.type = type; 094 this.personalNumber = personalNumber; 095 this.createdAt = createdAt; 096 this.dateOfExpiry = dateOfExpiry; 097 this.source = source; 098 } 099 100 101 /** 102 * Returns the electronic record type. 103 * 104 * @return The electronic record type. 105 */ 106 public ElectronicRecordType getType() { 107 return type; 108 } 109 110 111 /** 112 * Returns the personal number. 113 * 114 * @return The personal number, {@code null} if not specified. 115 */ 116 public PersonalNumber getPersonalNumber() { 117 return personalNumber; 118 } 119 120 121 /** 122 * Returns the time of creation. 123 * 124 * @return The time of creation, {@code null} if not specified. 125 */ 126 public DateWithTimeZoneOffset getCreatedAt() { 127 return createdAt; 128 } 129 130 131 /** 132 * Returns the date of expiry. 133 * 134 * @return The date of expiry, {@code null} if not specified. 135 */ 136 public SimpleDate getDateOfExpiry() { 137 return dateOfExpiry; 138 } 139 140 141 /** 142 * Returns the electronic record source. 143 * 144 * @return The electronic record source, {@code null} if not specified. 145 */ 146 public ElectronicRecordSource getSource() { 147 return source; 148 } 149 150 151 /** 152 * Returns a JSON object representation of this electronic record 153 * details instance. 154 * 155 * @return The JSON object. 156 */ 157 public JSONObject toJSONObject() { 158 JSONObject o = new JSONObject(); 159 o.put("type", getType().getValue()); 160 if (getPersonalNumber() != null) { 161 o.put("personal_number", getPersonalNumber().getValue()); 162 } 163 if (getCreatedAt() != null) { 164 o.put("created_at", getCreatedAt().toISO8601String()); 165 } 166 if (getDateOfExpiry() != null) { 167 o.put("date_of_expiry", getDateOfExpiry().toISO8601String()); 168 } 169 if (getSource() != null) { 170 JSONObject sourceObject = getSource().toJSONObject(); 171 if (! sourceObject.isEmpty()) { 172 o.put("source", sourceObject); 173 } 174 } 175 return o; 176 } 177 178 179 @Override 180 public boolean equals(Object o) { 181 if (this == o) return true; 182 if (!(o instanceof ElectronicRecordDetails)) return false; 183 ElectronicRecordDetails that = (ElectronicRecordDetails) o; 184 return getType().equals(that.getType()) && 185 Objects.equals(getPersonalNumber(), that.getPersonalNumber()) && 186 Objects.equals(getCreatedAt(), that.getCreatedAt()) && 187 Objects.equals(getDateOfExpiry(), that.getDateOfExpiry()) && 188 Objects.equals(getSource(), that.getSource()); 189 } 190 191 192 @Override 193 public int hashCode() { 194 return Objects.hash(getType(), getPersonalNumber(), getCreatedAt(), getDateOfExpiry(), getSource()); 195 } 196 197 198 /** 199 * Parses an electronic record details instance from the specified JSON 200 * object. 201 * 202 * @param jsonObject The JSON object. Must not be {@code null}. 203 * 204 * @return The electronic record details instance. 205 * 206 * @throws ParseException If parsing failed. 207 */ 208 public static ElectronicRecordDetails parse(final JSONObject jsonObject) 209 throws ParseException { 210 211 try { 212 ElectronicRecordType type = new ElectronicRecordType(JSONObjectUtils.getString(jsonObject, "type")); 213 214 PersonalNumber personalNumber = null; 215 if (jsonObject.get("personal_number") != null) { 216 personalNumber = new PersonalNumber(JSONObjectUtils.getString(jsonObject, "personal_number")); 217 } 218 219 DateWithTimeZoneOffset createdAt = null; 220 if (jsonObject.get("created_at") != null) { 221 createdAt = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "created_at")); 222 } 223 224 SimpleDate dateOfExpiry = null; 225 if (jsonObject.get("date_of_expiry") != null) { 226 dateOfExpiry = SimpleDate.parseISO8601String(JSONObjectUtils.getString(jsonObject, "date_of_expiry")); 227 } 228 229 ElectronicRecordSource source = null; 230 if (jsonObject.get("source") != null) { 231 source = ElectronicRecordSource.parse(JSONObjectUtils.getJSONObject(jsonObject, "source")); 232 } 233 234 return new ElectronicRecordDetails(type, personalNumber, createdAt, dateOfExpiry, source); 235 236 } catch (Exception e) { 237 throw new ParseException(e.getMessage(), e); 238 } 239 } 240}