001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2020, 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.claims;
019
020
021import java.util.Collection;
022
023import net.jcip.annotations.Immutable;
024import net.minidev.json.JSONObject;
025
026import com.nimbusds.langtag.LangTag;
027import com.nimbusds.oauth2.sdk.ParseException;
028import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
029import com.nimbusds.openid.connect.sdk.claims.ClaimsSetRequest;
030
031
032/**
033 * OpenID Connect verified claims set request, intended to represent the
034 * {@code verified_claims} sub-element within a {@code userinfo} or
035 * {@code id_token} element in a
036 * {@link com.nimbusds.openid.connect.sdk.OIDCClaimsRequest claims} request
037 * parameter.
038 *
039 * <p>Example:
040 *
041 * <pre>
042 * {
043 *   "verification": {
044 *      "trust_framework": "eidas_ial"
045 *   },
046 *   "claims":{
047 *      "given_name": null,
048 *      "family_name": null,
049 *      "birthdate": null
050 *   }
051 * }
052 * </pre>
053 *
054 * <p>Related specifications:
055 *
056 * <ul>
057 *     <li>OpenID Connect Core 1.0, section 5.5.
058 *     <li>OpenID Connect for Identity Assurance 1.0, section 6.
059 * </ul>
060 *
061 * @deprecated Use
062 * {@link com.nimbusds.openid.connect.sdk.assurance.request.VerifiedClaimsSetRequest}
063 * instead.
064 */
065@Deprecated
066@Immutable
067public class VerifiedClaimsSetRequest extends ClaimsSetRequest {
068        
069        
070        /**
071         * The verification element for the requested verified claims.
072         */
073        private final JSONObject verificationJSONObject;
074        
075        
076        /**
077         * Creates a new empty OpenID Connect verified claims set request.
078         */
079        public VerifiedClaimsSetRequest() {
080                super();
081                verificationJSONObject = null;
082        }
083        
084        
085        /**
086         * Creates a new OpenID Connect verified claims set request.
087         *
088         * @param entries                The request entries. Must not be
089         *                               {@code null}.
090         * @param verificationJSONObject The verification JSON object,
091         *                               {@code null} if not specified.
092         */
093        public VerifiedClaimsSetRequest(final Collection<ClaimsSetRequest.Entry> entries,
094                                        final JSONObject verificationJSONObject) {
095                super(entries);
096                this.verificationJSONObject = verificationJSONObject;
097        }
098        
099        
100        /**
101         * Gets the {@code verification} element.
102         *
103         * @return The {@code verification} JSON object, {@code null} if not
104         *         specified.
105         */
106        public JSONObject getVerificationJSONObject() {
107                return verificationJSONObject;
108        }
109        
110        
111        /**
112         * Sets the {@code verification} element.
113         *
114         * @param jsonObject The {@code verification} JSON object, {@code null}
115         *                   if not specified.
116         *
117         * @return The updated verified claims set request.
118         */
119        public VerifiedClaimsSetRequest withVerificationJSONObject(final JSONObject jsonObject) {
120                return new VerifiedClaimsSetRequest(getEntries(), jsonObject);
121        }
122        
123        
124        @Override
125        public VerifiedClaimsSetRequest add(final String claimName) {
126                ClaimsSetRequest csr = add(new ClaimsSetRequest.Entry(claimName));
127                return new VerifiedClaimsSetRequest(csr.getEntries(), getVerificationJSONObject());
128        }
129        
130        
131        @Override
132        public VerifiedClaimsSetRequest add(final ClaimsSetRequest.Entry entry) {
133                ClaimsSetRequest csr = super.add(entry);
134                return new VerifiedClaimsSetRequest(csr.getEntries(), getVerificationJSONObject());
135        }
136        
137        
138        @Override
139        public VerifiedClaimsSetRequest delete(final String claimName, final LangTag langTag) {
140                ClaimsSetRequest csr = super.delete(claimName, langTag);
141                return new VerifiedClaimsSetRequest(csr.getEntries(), getVerificationJSONObject());
142        }
143        
144        
145        @Override
146        public VerifiedClaimsSetRequest delete(final String claimName) {
147                ClaimsSetRequest csr = super.delete(claimName);
148                return new VerifiedClaimsSetRequest(csr.getEntries(), getVerificationJSONObject());
149        }
150        
151        
152        /**
153         * Returns the JSON object representation of this verified claims set
154         * request.
155         *
156         * <p>Example:
157         *
158         * <pre>
159         * {
160         *   "verification": {
161         *      "trust_framework": "eidas"
162         *   },
163         *   "claims":{
164         *      "given_name": null,
165         *      "family_name": null,
166         *      "birthdate": null
167         *   }
168         * }
169         * </pre>
170         *
171         * @return The JSON object, empty if no claims are specified.
172         */
173        @Override
174        public JSONObject toJSONObject() {
175                
176                JSONObject o = new JSONObject();
177                
178                if (verificationJSONObject != null && ! verificationJSONObject.isEmpty()) {
179                        o.put(VerifiedClaimsSet.VERIFICATION_ELEMENT, verificationJSONObject);
180                }
181                
182                JSONObject claims = super.toJSONObject();
183                
184                if (claims != null && ! claims.isEmpty()) {
185                        o.put(VerifiedClaimsSet.CLAIMS_ELEMENT, claims);
186                }
187                
188                return o;
189        }
190        
191        
192        /**
193         * Parses an OpenID Connect verified claims set request from the
194         * specified JSON object representation.
195         *
196         * <p>Example:
197         *
198         * <pre>
199         * {
200         *   "verification": {
201         *      "trust_framework": "eidas"
202         *   },
203         *   "claims":{
204         *      "given_name": null,
205         *      "family_name": null,
206         *      "birthdate": null
207         *   }
208         * }
209         * </pre>
210         *
211         * @param jsonObject The JSON object to parse. Must not be
212         *                   {@code null}.
213         *
214         * @return The verified claims set request.
215         *
216         * @throws ParseException If parsing failed.
217         */
218        public static VerifiedClaimsSetRequest parse(final JSONObject jsonObject)
219                throws ParseException {
220                
221                JSONObject verificationJSONObject = JSONObjectUtils.getJSONObject(jsonObject, VerifiedClaimsSet.VERIFICATION_ELEMENT, null);
222                
223                JSONObject claimsJSONObject = JSONObjectUtils.getJSONObject(jsonObject, VerifiedClaimsSet.CLAIMS_ELEMENT, new JSONObject());
224                
225                if (claimsJSONObject.isEmpty()) {
226                        throw new ParseException("Empty verified claims object");
227                }
228                
229                return new VerifiedClaimsSetRequest(
230                        ClaimsSetRequest.parse(claimsJSONObject).getEntries(),
231                        verificationJSONObject);
232        }
233        
234        
235        /**
236         * Parses an OpenID Connect verified claims set request from the
237         * specified JSON object string representation.
238         *
239         * <p>Example:
240         *
241         * <pre>
242         * {
243         *   "verification": {
244         *      "trust_framework": "eidas"
245         *   },
246         *   "claims":{
247         *      "given_name": null,
248         *      "family_name": null,
249         *      "birthdate": null
250         *   }
251         * }
252         * </pre>
253         *
254         * @param json The JSON object string to parse. Must not be
255         *             {@code null}.
256         *
257         * @return The verified claims set request.
258         *
259         * @throws ParseException If parsing failed.
260         */
261        public static VerifiedClaimsSetRequest parse(final String json)
262                throws ParseException {
263                
264                return parse(JSONObjectUtils.parse(json));
265        }
266}