001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2021, 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.CollectionUtils; 027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 028import com.nimbusds.openid.connect.sdk.claims.Address; 029 030 031/** 032 * Voucher. 033 * 034 * <p>Related specifications: 035 * 036 * <ul> 037 * <li>OpenID Connect for Identity Assurance 1.0 038 * </ul> 039 */ 040public class Voucher { 041 042 043 /** 044 * The name. 045 */ 046 private final Name name; 047 048 049 /** 050 * The birthdate. 051 */ 052 private final String birthdateString; 053 054 055 /** 056 * The address. 057 */ 058 private final Address address; 059 060 061 /** 062 * The occupation. 063 */ 064 private final Occupation occupation; 065 066 067 /** 068 * The organisation. 069 */ 070 private final Organization organization; 071 072 073 /** 074 * Creates a new voucher. 075 * 076 * @param name The name, {@code null} if not specified. 077 * @param birthdateString The birthday string, {@code null} if not 078 * specified. 079 * @param address The address elements, {@code null} if not 080 * specified. 081 * @param occupation The occupation, {@code null} if not 082 * specified. 083 * @param organization The organisation, {@code null} if not 084 * specified. 085 */ 086 public Voucher(final Name name, 087 final String birthdateString, 088 final Address address, 089 final Occupation occupation, 090 final Organization organization) { 091 092 this.name = name; 093 this.birthdateString = birthdateString; 094 this.address = address; 095 this.occupation = occupation; 096 this.organization = organization; 097 } 098 099 100 /** 101 * Returns the name. 102 * 103 * @return The name, {@code null} if not specified. 104 */ 105 public Name getName() { 106 return name; 107 } 108 109 110 /** 111 * Returns the birthdate string. 112 * 113 * @return The birthdate string, {@code null} if not specified. 114 */ 115 public String getBirthdateString() { 116 return birthdateString; 117 } 118 119 120 /** 121 * Returns the address elements. 122 * 123 * @return The address elements, {@code null} if not specified. 124 */ 125 public Address getAddress() { 126 return address; 127 } 128 129 130 /** 131 * Returns the occupation. 132 * 133 * @return The occupation, {@code null} if not specified. 134 */ 135 public Occupation getOccupation() { 136 return occupation; 137 } 138 139 140 /** 141 * Returns the organisation. 142 * 143 * @return The organisation, {@code null} if not specified. 144 */ 145 public Organization getOrganization() { 146 return organization; 147 } 148 149 150 /** 151 * Returns a JSON object representation of this voucher. 152 * 153 * @return The JSON object. 154 */ 155 public JSONObject toJSONObject() { 156 157 JSONObject o = new JSONObject(); 158 if (getName() != null) { 159 o.put("name", getName().getValue()); 160 } 161 if (getBirthdateString() != null) { 162 o.put("birthdate", getBirthdateString()); 163 } 164 if (getAddress() != null) { 165 o.putAll(getAddress().toJSONObject()); 166 } 167 if (getOccupation() != null) { 168 o.put("occupation", getOccupation().getValue()); 169 } 170 if (getOrganization() != null) { 171 o.put("organization", getOrganization().getValue()); 172 } 173 return o; 174 } 175 176 177 @Override 178 public boolean equals(Object o) { 179 if (this == o) return true; 180 if (!(o instanceof Voucher)) return false; 181 Voucher voucher = (Voucher) o; 182 return Objects.equals(getName(), voucher.getName()) && 183 Objects.equals(getBirthdateString(), voucher.getBirthdateString()) && 184 Objects.equals(getAddress(), voucher.getAddress()) && 185 Objects.equals(getOccupation(), voucher.getOccupation()) && 186 Objects.equals(getOrganization(), voucher.getOrganization()); 187 } 188 189 190 @Override 191 public int hashCode() { 192 return Objects.hash( 193 getName(), 194 getBirthdateString(), 195 getAddress(), 196 getOccupation(), 197 getOrganization() 198 ); 199 } 200 201 202 /** 203 * Parses a voucher from the specified JSON objecassertEquals("Equality", voucher, Voucher.parse(jsonObject)); 204 assertEquals("Hash code", voucher.hashCode(), Voucher.parse(jsonObject).hashCode());t. 205 * 206 * @param jsonObject The JSON object. Must not be {@code null}. 207 * 208 * @return The voucher. 209 * 210 * @throws ParseException If parsing failed. 211 */ 212 public static Voucher parse(final JSONObject jsonObject) 213 throws ParseException { 214 215 try { 216 Name name = null; 217 if (jsonObject.get("name") != null) { 218 name = new Name(JSONObjectUtils.getNonBlankString(jsonObject, "name")); 219 } 220 221 String birthdateString = JSONObjectUtils.getString(jsonObject, "birthdate", null); 222 223 Occupation occupation = null; 224 if (jsonObject.get("occupation") != null) { 225 occupation = new Occupation(JSONObjectUtils.getNonBlankString(jsonObject, "occupation")); 226 } 227 228 Organization organization = null; 229 if (jsonObject.get("organization") != null) { 230 organization = new Organization(JSONObjectUtils.getNonBlankString(jsonObject, "organization")); 231 } 232 233 Address address = null; 234 if (CollectionUtils.intersect(Address.getStandardClaimNames(), jsonObject.keySet())) { 235 236 JSONObject addressSpecific = new JSONObject(jsonObject); 237 addressSpecific.remove("name"); 238 addressSpecific.remove("birthdate"); 239 addressSpecific.remove("occupation"); 240 addressSpecific.remove("organization"); 241 address = new Address(addressSpecific); 242 } 243 244 return new Voucher(name, birthdateString, address, occupation, organization); 245 246 } catch (Exception e) { 247 throw new ParseException(e.getMessage(), e); 248 } 249 } 250}