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 net.jcip.annotations.Immutable; 022import net.minidev.json.JSONObject; 023 024import com.nimbusds.oauth2.sdk.ParseException; 025import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 026import com.nimbusds.oauth2.sdk.util.date.SimpleDate; 027import com.nimbusds.openid.connect.sdk.claims.Address; 028 029 030/** 031 * Utility bill used as identity evidence. 032 * 033 * <p>Related specifications: 034 * 035 * <ul> 036 * <li>OpenID Connect for Identity Assurance 1.0, section 4.1.1. 037 * </ul> 038 */ 039@Immutable 040public final class UtilityBillEvidence extends IdentityEvidence { 041 042 043 /** 044 * The utility provider name. 045 */ 046 private final String providerName; 047 048 049 /** 050 * The utility provider address details. 051 */ 052 private final Address providerAddress; 053 054 055 /** 056 * The utility bill date. 057 */ 058 private final SimpleDate date; 059 060 061 /** 062 * Creates a new utility bill used as identity evidence. 063 * 064 * @param providerName The utility provider name, {@code null} if 065 * not specified. 066 * @param providerAddress The utility provider address details, 067 * {@code null} if not specified. 068 * @param date The utility bill date, {@code null} if not 069 * specified. 070 */ 071 public UtilityBillEvidence(final String providerName, final Address providerAddress, final SimpleDate date) { 072 073 super(IdentityEvidenceType.UTILITY_BILL); 074 this.providerName = providerName; 075 this.providerAddress = providerAddress; 076 this.date = date; 077 } 078 079 080 /** 081 * The utility provider name. 082 * 083 * @return The utility provider name, {@code null} if not specified. 084 */ 085 public String getUtilityProviderName() { 086 return providerName; 087 } 088 089 090 /** 091 * Returns the utility provider address details. 092 * 093 * @return The utility provider address details, {@code null} if not 094 * specified. 095 */ 096 public Address getUtilityProviderAddress() { 097 return providerAddress; 098 } 099 100 101 /** 102 * Returns the utility bill date. 103 * 104 * @return The utility bill date, {@code null} if not specified. 105 */ 106 public SimpleDate getUtilityBillDate() { 107 return date; 108 } 109 110 111 @Override 112 public JSONObject toJSONObject() { 113 114 JSONObject o = super.toJSONObject(); 115 116 JSONObject providerDetails = new JSONObject(); 117 if (getUtilityProviderName() != null) { 118 providerDetails.put("name", getUtilityProviderName()); 119 } 120 if (getUtilityProviderAddress() != null) { 121 providerDetails.putAll(getUtilityProviderAddress().toJSONObject()); 122 } 123 if (! providerDetails.isEmpty()) { 124 o.put("provider", providerDetails); 125 } 126 127 if (getUtilityBillDate() != null) { 128 o.put("date", getUtilityBillDate().toISO8601String()); 129 } 130 131 return o; 132 } 133 134 135 /** 136 * Parses a utility bill evidence from the specified JSON object. 137 * 138 * @param jsonObject The JSON object. Must not be {@code null}. 139 * 140 * @return The utility bill evidence. 141 * 142 * @throws ParseException If parsing failed. 143 */ 144 public static UtilityBillEvidence parse(final JSONObject jsonObject) 145 throws ParseException { 146 147 ensureType(IdentityEvidenceType.UTILITY_BILL, jsonObject); 148 149 JSONObject providerDetails = JSONObjectUtils.getJSONObject(jsonObject, "provider", null); 150 151 String providerName = null; 152 Address providerAddress = null; 153 if (providerDetails != null) { 154 providerName = JSONObjectUtils.getString(providerDetails, "name", null); 155 156 JSONObject providerDetailsCopy = new JSONObject(providerDetails); 157 providerDetailsCopy.remove("name"); 158 159 if (! providerDetailsCopy.isEmpty()) { 160 providerAddress = new Address(providerDetailsCopy); 161 } 162 } 163 164 SimpleDate date = null; 165 if (jsonObject.get("date") != null) { 166 date = SimpleDate.parseISO8601String(JSONObjectUtils.getString(jsonObject, "date")); 167 } 168 169 return new UtilityBillEvidence(providerName, providerAddress, date); 170 } 171}