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.claims;
019
020
021import net.minidev.json.JSONAware;
022import net.minidev.json.JSONObject;
023
024import com.nimbusds.oauth2.sdk.ParseException;
025import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
026import com.nimbusds.openid.connect.sdk.assurance.IdentityVerification;
027import com.nimbusds.openid.connect.sdk.claims.ClaimsSet;
028import com.nimbusds.openid.connect.sdk.claims.PersonClaims;
029
030
031/**
032 * Verified claims set.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OpenID Connect for Identity Assurance 1.0, section 5.
038 * </ul>
039 */
040public class VerifiedClaimsSet implements JSONAware {
041        
042        
043        /**
044         * The verification element.
045         */
046        public static final String VERIFICATION_ELEMENT = "verification";
047        
048        
049        /**
050         * The claims element.
051         */
052        public static final String CLAIMS_ELEMENT = "claims";
053        
054        
055        /**
056         * The identity verification.
057         */
058        private final IdentityVerification identityVerification;
059        
060        
061        /**
062         * The verified claims.
063         */
064        private final ClaimsSet claimsSet;
065        
066        
067        /**
068         * Creates a new verified claims set.
069         *
070         * @param verification The identity verification. Must not be
071         *                     {@code null}.
072         * @param claims       The verified claims. Must not be {@code null}.
073         */
074        public VerifiedClaimsSet(final IdentityVerification verification,
075                                 final ClaimsSet claims) {
076                
077                if (verification == null) {
078                        throw new IllegalArgumentException("The verification must not be null");
079                }
080                identityVerification = verification;
081                
082                if (claims == null) {
083                        throw new IllegalArgumentException("The claims must not be null");
084                }
085                claimsSet = claims;
086        }
087        
088        
089        /**
090         * Returns the identity verification.
091         *
092         * @return The identity verification.
093         */
094        public IdentityVerification getVerification() {
095        
096                return identityVerification;
097        }
098        
099        
100        /**
101         * Returns the verified claims.
102         *
103         * @return The verified claims wrapped in a person claims object for
104         *         convenience.
105         */
106        public PersonClaims getClaimsSet() {
107        
108                return new PersonClaims(claimsSet.toJSONObject());
109        }
110        
111        
112        /**
113         * Returns a JSON object representation of this verified claims set.
114         *
115         * @return The JSON object.
116         */
117        public JSONObject toJSONObject() {
118                
119                JSONObject o = new JSONObject();
120                o.put(VERIFICATION_ELEMENT, identityVerification.toJSONObject());
121                o.put(CLAIMS_ELEMENT, claimsSet.toJSONObject());
122                return o;
123        }
124        
125        
126        @Override
127        public String toJSONString() {
128                
129                return toJSONObject().toJSONString();
130        }
131        
132        
133        /**
134         * Parses a verified claims set from the specified JSON object.
135         *
136         * @param jsonObject The JSON object to parse.
137         *
138         * @return The verifier claims set.
139         *
140         * @throws ParseException If parsing failed.
141         */
142        public static VerifiedClaimsSet parse(final JSONObject jsonObject)
143                throws ParseException {
144                
145                return new VerifiedClaimsSet(
146                        IdentityVerification.parse(JSONObjectUtils.getJSONObject(jsonObject, VERIFICATION_ELEMENT)),
147                        new PersonClaims(JSONObjectUtils.getJSONObject(jsonObject, CLAIMS_ELEMENT)));
148        }
149}