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