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. Must not be
065         *                        {@code null}.
066         * @param providerAddress The utility provider address details. Must
067         *                        not be {@code null}.
068         * @param date            The utility bill date. Must not be
069         *                        {@code null}.
070         */
071        public UtilityBillEvidence(final String providerName, final Address providerAddress, final SimpleDate date) {
072                
073                super(IdentityEvidenceType.UTILITY_BILL);
074                
075                if (providerName == null) {
076                        throw new IllegalArgumentException("The utility provider name must not be null");
077                }
078                this.providerName = providerName;
079                
080                if (providerAddress == null) {
081                        throw new IllegalArgumentException("The utility provider address must not be null");
082                }
083                this.providerAddress = providerAddress;
084                
085                if (date == null) {
086                        throw new IllegalArgumentException("The utility bill date must not be null");
087                }
088                this.date = date;
089        }
090        
091        
092        /**
093         * The utility provider name.
094         *
095         * @return The utility provider name.
096         */
097        public String getUtilityProviderName() {
098                return providerName;
099        }
100        
101        
102        /**
103         * Returns the utility provider address details.
104         *
105         * @return The utility provider address details.
106         */
107        public Address getUtilityProviderAddress() {
108                return providerAddress;
109        }
110        
111        
112        /**
113         * Returns the utility bill date.
114         *
115         * @return The utility bill date.
116         */
117        public SimpleDate getUtilityBillDate() {
118                return date;
119        }
120        
121        
122        @Override
123        public JSONObject toJSONObject() {
124                
125                JSONObject o = super.toJSONObject();
126                
127                JSONObject providerDetails = new JSONObject();
128                providerDetails.put("name", getUtilityProviderName());
129                providerDetails.putAll(getUtilityProviderAddress().toJSONObject());
130                o.put("provider", providerDetails);
131                
132                o.put("date", getUtilityBillDate().toISO8601String());
133                
134                return o;
135        }
136        
137        
138        /**
139         * Parses a utility bill evidence from the specified JSON object.
140         *
141         * @param jsonObject The JSON object. Must not be {@code null}.
142         *
143         * @return The utility bill evidence.
144         *
145         * @throws ParseException If parsing failed.
146         */
147        public static UtilityBillEvidence parse(final JSONObject jsonObject)
148                throws ParseException {
149                
150                ensureType(IdentityEvidenceType.UTILITY_BILL, jsonObject);
151                
152                JSONObject providerDetails = JSONObjectUtils.getJSONObject(jsonObject, "provider");
153                String providerName = JSONObjectUtils.getString(providerDetails, "name");
154                providerDetails.remove("name");
155                Address providerAddress = Address.parse(providerDetails.toJSONString());
156                
157                SimpleDate date = SimpleDate.parseISO8601String(JSONObjectUtils.getString(jsonObject, "date"));
158                
159                return new UtilityBillEvidence(providerName, providerAddress, date);
160        }
161}