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.List;
022import java.util.Objects;
023
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.DateWithTimeZoneOffset;
029import com.nimbusds.oauth2.sdk.util.date.SimpleDate;
030import com.nimbusds.openid.connect.sdk.assurance.evidences.attachment.Attachment;
031import com.nimbusds.openid.connect.sdk.claims.Address;
032
033
034/**
035 * Utility bill used as identity evidence.
036 *
037 * <p>Related specifications:
038 *
039 * <ul>
040 *     <li>OpenID Connect for Identity Assurance 1.0, section 5.1.1.4.
041 * </ul>
042 */
043@Deprecated
044public class UtilityBillEvidence extends IdentityEvidence {
045        
046        
047        /**
048         * The utility provider name.
049         */
050        private final String providerName;
051        
052        
053        /**
054         * The utility provider address details.
055         */
056        private final Address providerAddress;
057        
058        
059        /**
060         * The utility bill date.
061         */
062        private final SimpleDate date;
063        
064        
065        /**
066         * The utility bill verification timestamp.
067         */
068        private final DateWithTimeZoneOffset time;
069        
070        
071        /**
072         * The identity verification method.
073         */
074        private final IdentityVerificationMethod method;
075        
076        
077        /**
078         * Creates a new utility bill used as identity evidence.
079         *
080         * @param providerName    The utility provider name, {@code null} if
081         *                        not specified.
082         * @param providerAddress The utility provider address details,
083         *                        {@code null} if not specified.
084         * @param date            The utility bill date, {@code null} if not
085         *                        specified.
086         */
087        @Deprecated
088        public UtilityBillEvidence(final String providerName, final Address providerAddress, final SimpleDate date) {
089                
090                this(providerName, providerAddress, date, null, null, null);
091        }
092        
093        
094        /**
095         * Creates a new utility bill used as identity evidence.
096         *
097         * @param providerName    The utility provider name, {@code null} if
098         *                        not specified.
099         * @param providerAddress The utility provider address details,
100         *                        {@code null} if not specified.
101         * @param date            The utility bill date, {@code null} if not
102         *                        specified.
103         * @param time             The utility bill verification timestamp,
104         *                        {@code null} if not specified.
105         * @param method          The identity verification method,
106         *                        {@code null} if not specified.
107         * @param attachments     The optional attachments, {@code null} if not
108         *                        specified.
109         */
110        public UtilityBillEvidence(final String providerName,
111                                   final Address providerAddress,
112                                   final SimpleDate date,
113                                   final DateWithTimeZoneOffset time,
114                                   final IdentityVerificationMethod method,
115                                   final List<Attachment> attachments) {
116                
117                super(IdentityEvidenceType.UTILITY_BILL, attachments);
118                this.providerName = providerName;
119                this.providerAddress = providerAddress;
120                this.date = date;
121                this.time = time;
122                this.method = method;
123        }
124        
125        
126        /**
127         * The utility provider name.
128         *
129         * @return The utility provider name, {@code null} if not specified.
130         */
131        public String getUtilityProviderName() {
132                return providerName;
133        }
134        
135        
136        /**
137         * Returns the utility provider address details.
138         *
139         * @return The utility provider address details, {@code null} if not
140         *         specified.
141         */
142        public Address getUtilityProviderAddress() {
143                return providerAddress;
144        }
145        
146        
147        /**
148         * Returns the utility bill date.
149         *
150         * @return The utility bill date, {@code null} if not specified.
151         */
152        public SimpleDate getUtilityBillDate() {
153                return date;
154        }
155        
156        
157        /**
158         * Returns the utility bill verification timestamp.
159         *
160         * @return The utility bill verification timestamp, {@code null} if not
161         *         specified.
162         */
163        public DateWithTimeZoneOffset getVerificationTime() {
164                return time;
165        }
166        
167        
168        /**
169         * Returns the utility bill verification method.
170         *
171         * @return The utility bill verification method, {@code null} if not
172         *         specified.
173         */
174        public IdentityVerificationMethod getVerificationMethod() {
175                return method;
176        }
177        
178        
179        @Override
180        public JSONObject toJSONObject() {
181                
182                JSONObject o = super.toJSONObject();
183                
184                JSONObject providerDetails = new JSONObject();
185                if (getUtilityProviderName() != null) {
186                        providerDetails.put("name", getUtilityProviderName());
187                }
188                if (getUtilityProviderAddress() != null) {
189                        providerDetails.putAll(getUtilityProviderAddress().toJSONObject());
190                }
191                if (! providerDetails.isEmpty()) {
192                        o.put("provider", providerDetails);
193                }
194                
195                if (getUtilityBillDate() != null) {
196                        o.put("date", getUtilityBillDate().toISO8601String());
197                }
198                
199                if (getVerificationTime() != null) {
200                        o.put("time", getVerificationTime().toISO8601String());
201                }
202                
203                if (getVerificationMethod() != null) {
204                        o.put("method", getVerificationMethod().getValue());
205                }
206                
207                return o;
208        }
209        
210        
211        @Override
212        public boolean equals(Object o) {
213                if (this == o) return true;
214                if (!(o instanceof UtilityBillEvidence)) return false;
215                UtilityBillEvidence evidence = (UtilityBillEvidence) o;
216                return Objects.equals(getUtilityProviderName(), evidence.getUtilityProviderName()) &&
217                        Objects.equals(getUtilityProviderAddress(), evidence.getUtilityProviderAddress()) &&
218                        Objects.equals(getUtilityBillDate(), evidence.getUtilityBillDate()) &&
219                        Objects.equals(getVerificationTime(), evidence.getVerificationTime()) &&
220                        Objects.equals(getVerificationMethod(), evidence.getVerificationMethod());
221        }
222        
223        
224        @Override
225        public int hashCode() {
226                return Objects.hash(
227                        getUtilityProviderName(),
228                        getUtilityProviderAddress(),
229                        getUtilityBillDate(),
230                        getVerificationTime(),
231                        getVerificationMethod()
232                );
233        }
234        
235        
236        /**
237         * Parses a utility bill evidence from the specified JSON object.
238         *
239         * @param jsonObject The JSON object. Must not be {@code null}.
240         *
241         * @return The utility bill evidence.
242         *
243         * @throws ParseException If parsing failed.
244         */
245        public static UtilityBillEvidence parse(final JSONObject jsonObject)
246                throws ParseException {
247                
248                ensureType(IdentityEvidenceType.UTILITY_BILL, jsonObject);
249                
250                JSONObject providerDetails = JSONObjectUtils.getJSONObject(jsonObject, "provider", null);
251                
252                String providerName = null;
253                Address providerAddress = null;
254                if (providerDetails != null) {
255                        providerName = JSONObjectUtils.getString(providerDetails, "name", null);
256                        
257                        JSONObject providerDetailsCopy = new JSONObject(providerDetails);
258                        providerDetailsCopy.remove("name");
259                        
260                        if (! providerDetailsCopy.isEmpty()) {
261                                providerAddress = new Address(providerDetailsCopy);
262                        }
263                }
264                
265                SimpleDate date = null;
266                if (jsonObject.get("date") != null) {
267                        date = SimpleDate.parseISO8601String(JSONObjectUtils.getString(jsonObject, "date"));
268                }
269                
270                DateWithTimeZoneOffset dtz = null;
271                if (jsonObject.get("time") != null) {
272                        dtz = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "time"));
273                }
274                
275                IdentityVerificationMethod method = null;
276                if (jsonObject.get("method") != null) {
277                        method = new IdentityVerificationMethod(JSONObjectUtils.getString(jsonObject, "method"));
278                }
279                
280                List<Attachment> attachments = null;
281                if (jsonObject.get("attachments") != null) {
282                        attachments = Attachment.parseList(JSONObjectUtils.getJSONArray(jsonObject, "attachments"));
283                }
284                
285                return new UtilityBillEvidence(providerName, providerAddress, date, dtz, method, attachments);
286        }
287}