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.JSONAware;
023import net.minidev.json.JSONObject;
024
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.id.Issuer;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028import com.nimbusds.oauth2.sdk.util.date.DateWithTimeZoneOffset;
029
030
031/**
032 * Qualified electronic signature (QES) used as identity evidence.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OpenID Connect for Identity Assurance 1.0, section 4.1.1.
038 * </ul>
039 */
040@Immutable
041public final class QESEvidence extends IdentityEvidence implements JSONAware {
042        
043        
044        /**
045         * The QES issuer.
046         */
047        private final Issuer issuer;
048        
049        
050        /**
051         * The QES serial number.
052         */
053        private final String serialNumber;
054        
055        
056        /**
057         * The QES creation time.
058         */
059        private final DateWithTimeZoneOffset createdAt;
060        
061        
062        /**
063         * Creates a new QES used as identity evidence.
064         *
065         * @param issuer       The QES issuer, {@code null} if not specified.
066         * @param serialNumber The QES serial number, {@code null} if not
067         *                     specified.
068         * @param createdAt    The QES creation time, {@code null} if not
069         *                     specified.
070         */
071        public QESEvidence(final Issuer issuer, final String serialNumber, final DateWithTimeZoneOffset createdAt) {
072                
073                super(IdentityEvidenceType.QES);
074                this.issuer = issuer;
075                this.serialNumber = serialNumber;
076                this.createdAt = createdAt;
077        }
078        
079        
080        /**
081         * Returns the QES issuer.
082         * @return The QES issuer, {@code null} if not specified.
083         */
084        public Issuer getQESIssuer() {
085                return issuer;
086        }
087        
088        
089        /**
090         * Returns the QES serial number.
091         *
092         * @return The QES serial number string, {@code null} if not specified.
093         */
094        public String getQESSerialNumberString() {
095                return serialNumber;
096        }
097        
098        
099        /**
100         * Returns The QES creation time.
101         *
102         * @return The QES creation time, {@code null} if not specified.
103         */
104        public DateWithTimeZoneOffset getQESCreationTime() {
105                return createdAt;
106        }
107        
108        
109        @Override
110        public JSONObject toJSONObject() {
111                
112                JSONObject o = super.toJSONObject();
113                if (getQESIssuer() != null) {
114                        o.put("issuer", getQESIssuer().getValue());
115                }
116                if (getQESSerialNumberString() != null) {
117                        o.put("serial_number", getQESSerialNumberString());
118                }
119                if (getQESCreationTime() != null) {
120                        o.put("created_at", getQESCreationTime().toISO8601String());
121                }
122                return o;
123        }
124        
125        
126        /**
127         * Parses a new QES evidence from the specified JSON object.
128         *
129         * @param jsonObject The JSON object. Must not be {@code null}.
130         *
131         * @return The QES evidence.
132         *
133         * @throws ParseException If parsing failed.
134         */
135        public static QESEvidence parse(final JSONObject jsonObject)
136                throws ParseException {
137                
138                ensureType(IdentityEvidenceType.QES, jsonObject);
139                
140                Issuer issuer = null;
141                if (jsonObject.get("issuer") != null) {
142                        issuer = new Issuer(JSONObjectUtils.getString(jsonObject, "issuer"));
143                }
144                
145                String serialNumber = JSONObjectUtils.getString(jsonObject, "serial_number", null);
146                
147                DateWithTimeZoneOffset createdAt = null;
148                if (jsonObject.get("created_at") != null) {
149                        createdAt = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "created_at"));
150                }
151                
152                return new QESEvidence(issuer, serialNumber, createdAt);
153        }
154}