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, {@code null} if
070         *                   not specified.
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, {@code null} if
076         *                   not specified.
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                this.method = method;
086                this.dtz = dtz;
087                this.verifier = verifier;
088                this.idDocument = idDocument;
089        }
090        
091        
092        /**
093         * Returns the document verification method.
094         *
095         * @return The document verification method, {@code null} if not
096         *         specified.
097         */
098        public IdentityVerificationMethod getVerificationMethod() {
099                return method;
100        }
101        
102        
103        /**
104         * Returns the document verification timestamp.
105         *
106         * @return The document verification timestamp, {@code null} if not
107         *         specified.
108         */
109        public DateWithTimeZoneOffset getVerificationTime() {
110                return dtz;
111        }
112        
113        
114        /**
115         * Returns the optional verifier if not the OpenID provider itself.
116         *
117         * @return The optional verifier if not the OpenID provider itself,
118         *         {@code null} if none.
119         */
120        public IdentityVerifier getVerifier() {
121                return verifier;
122        }
123        
124        
125        /**
126         * Returns the identity document description.
127         *
128         * @return The identity document description, {@code null} if not
129         *         specified.
130         */
131        public IDDocumentDescription getIdentityDocument() {
132                return idDocument;
133        }
134        
135        
136        @Override
137        public JSONObject toJSONObject() {
138                JSONObject o = super.toJSONObject();
139                if (getVerificationMethod() != null) {
140                        o.put("method", getVerificationMethod().getValue());
141                }
142                if (dtz != null) {
143                        o.put("time", getVerificationTime().toISO8601String());
144                }
145                if (verifier != null) {
146                        o.put("verifier", getVerifier().toJSONObject());
147                }
148                if (getIdentityDocument() != null) {
149                        o.put("document", getIdentityDocument().toJSONObject());
150                }
151                return o;
152        }
153        
154        
155        /**
156         * Parses an identity document used as identity evidence from the
157         * specified JSON object.
158         *
159         * @param jsonObject The JSON object. Must not be {@code null}.
160         *
161         * @return The identity document used as identity evidence.
162         *
163         * @throws ParseException If parsing failed.
164         */
165        public static IDDocumentEvidence parse(final JSONObject jsonObject)
166                throws ParseException {
167                
168                ensureType(IdentityEvidenceType.ID_DOCUMENT, jsonObject);
169                
170                IdentityVerificationMethod method = null;
171                if (jsonObject.get("method") != null) {
172                        method = new IdentityVerificationMethod(JSONObjectUtils.getString(jsonObject, "method"));
173                }
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 = null;
186                if (jsonObject.get("document") != null) {
187                        idDocument = IDDocumentDescription.parse(JSONObjectUtils.getJSONObject(jsonObject, "document"));
188                }
189                
190                return new IDDocumentEvidence(method, verifier, dtz, idDocument);
191        }
192}