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.DateWithTimeZoneOffset;
027
028
029/**
030 * Identity document used as identity evidence.
031 *
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OpenID Connect for Identity Assurance 1.0, section 4.1.1.
036 * </ul>
037 */
038@Immutable
039public final class IDDocumentEvidence extends IdentityEvidence {
040        
041        
042        /**
043         * The document verification method.
044         */
045        private final IdentityVerificationMethod method;
046        
047        
048        /**
049         * The document verification timestamp.
050         */
051        private final DateWithTimeZoneOffset dtz;
052        
053        
054        /**
055         * Optional verifier if not the OpenID provider itself.
056         */
057        private final IdentityVerifier verifier;
058        
059        
060        /**
061         * The identity document description.
062         */
063        private final IDDocumentDescription idDocument;
064        
065        
066        /**
067         * Creates a new identity document evidence.
068         *
069         * @param method     The document verification method. Must not be
070         *                   {@code null}.
071         * @param verifier   Optional verifier if not the OpenID provider
072         *                   itself, {@code null} if none.
073         * @param dtz        The document verification timestamp, {@code null}
074         *                   if not specified.
075         * @param idDocument The identity document description. Must not be
076         *                   {@code null}.
077         */
078        public IDDocumentEvidence(final IdentityVerificationMethod method,
079                                  final IdentityVerifier verifier,
080                                  final DateWithTimeZoneOffset dtz,
081                                  final IDDocumentDescription idDocument) {
082                
083                super(IdentityEvidenceType.ID_DOCUMENT);
084                
085                if (method == null) {
086                        throw new IllegalArgumentException("The verification method must not be null");
087                }
088                this.method = method;
089                
090                this.dtz = dtz;
091                
092                this.verifier = verifier;
093                
094                if (idDocument == null) {
095                        throw new IllegalArgumentException("The identity document description must not be null");
096                }
097                this.idDocument = idDocument;
098        }
099        
100        
101        /**
102         * Returns the document verification method.
103         *
104         * @return The document verification method.
105         */
106        public IdentityVerificationMethod getVerificationMethod() {
107                return method;
108        }
109        
110        
111        /**
112         * Returns the document verification timestamp.
113         *
114         * @return The document verification timestamp, {@code null} if not
115         *         specified.
116         */
117        public DateWithTimeZoneOffset getVerificationTime() {
118                return dtz;
119        }
120        
121        
122        /**
123         * Returns the optional verifier if not the OpenID provider itself.
124         *
125         * @return The optional verifier if not the OpenID provider itself,
126         *         {@code null} if none.
127         */
128        public IdentityVerifier getVerifier() {
129                return verifier;
130        }
131        
132        
133        /**
134         * Returns the identity document description.
135         *
136         * @return The identity document description.
137         */
138        public IDDocumentDescription getIdentityDocument() {
139                return idDocument;
140        }
141        
142        
143        @Override
144        public JSONObject toJSONObject() {
145                JSONObject o = super.toJSONObject();
146                o.put("method", getVerificationMethod().getValue());
147                if (dtz != null) {
148                        o.put("time", getVerificationTime().toISO8601String());
149                }
150                if (verifier != null) {
151                        o.put("verifier", getVerifier().toJSONObject());
152                }
153                o.put("document", getIdentityDocument().toJSONObject());
154                return o;
155        }
156        
157        
158        /**
159         * Parses an identity document used as identity evidence from the
160         * specified JSON object.
161         *
162         * @param jsonObject The JSON object. Must not be {@code null}.
163         *
164         * @return The identity document used as identity evidence.
165         *
166         * @throws ParseException If parsing failed.
167         */
168        public static IDDocumentEvidence parse(final JSONObject jsonObject)
169                throws ParseException {
170                
171                ensureType(IdentityEvidenceType.ID_DOCUMENT, jsonObject);
172                
173                IdentityVerificationMethod method = new IdentityVerificationMethod(JSONObjectUtils.getString(jsonObject, "method"));
174                
175                DateWithTimeZoneOffset dtz = null;
176                if (jsonObject.get("time") != null) {
177                        dtz = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "time"));
178                }
179                
180                IdentityVerifier verifier = null;
181                if (jsonObject.get("verifier") != null) {
182                        verifier = IdentityVerifier.parse(JSONObjectUtils.getJSONObject(jsonObject, "verifier"));
183                }
184                
185                IDDocumentDescription idDocument = IDDocumentDescription.parse(JSONObjectUtils.getJSONObject(jsonObject, "document"));
186                
187                return new IDDocumentEvidence(method, verifier, dtz, idDocument);
188        }
189}