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. Must not be {@code null}.
066         * @param serialNumber The QES serial number. Must not be
067         *                     {@code null}.
068         * @param createdAt    The QES creation time. Must not be {@code null}.
069         */
070        public QESEvidence(final Issuer issuer, final String serialNumber, final DateWithTimeZoneOffset createdAt) {
071                
072                super(IdentityEvidenceType.QES);
073                
074                if (issuer == null) {
075                        throw new IllegalArgumentException("The QES issuer must not be null");
076                }
077                this.issuer = issuer;
078                
079                if (serialNumber == null) {
080                        throw new IllegalArgumentException("The QES serial number must not be null");
081                }
082                this.serialNumber = serialNumber;
083                
084                if (createdAt == null) {
085                        throw new IllegalArgumentException("The QES creation time must not be null");
086                }
087                this.createdAt = createdAt;
088        }
089        
090        
091        /**
092         * Returns the QES issuer.
093         * @return The QES issuer.
094         */
095        public Issuer getQESIssuer() {
096                return issuer;
097        }
098        
099        
100        /**
101         * Returns the QES serial number.
102         *
103         * @return The QES serial number string.
104         */
105        public String getQESSerialNumberString() {
106                return serialNumber;
107        }
108        
109        
110        /**
111         * Returns The QES creation time.
112         *
113         * @return The QES creation time.
114         */
115        public DateWithTimeZoneOffset getQESCreationTime() {
116                return createdAt;
117        }
118        
119        
120        @Override
121        public JSONObject toJSONObject() {
122                
123                JSONObject o = super.toJSONObject();
124                o.put("issuer", getQESIssuer().getValue());
125                o.put("serial_number", getQESSerialNumberString());
126                o.put("created_at", getQESCreationTime().toISO8601String());
127                return o;
128        }
129        
130        
131        /**
132         * Parses a new QES evidence from the specified JSON object.
133         *
134         * @param jsonObject The JSON object. Must not be {@code null}.
135         *
136         * @return The QES evidence.
137         *
138         * @throws ParseException If parsing failed.
139         */
140        public static QESEvidence parse(final JSONObject jsonObject)
141                throws ParseException {
142                
143                ensureType(IdentityEvidenceType.QES, jsonObject);
144                Issuer issuer = new Issuer(JSONObjectUtils.getString(jsonObject, "issuer"));
145                String serialNumber = JSONObjectUtils.getString(jsonObject, "serial_number");
146                DateWithTimeZoneOffset createdAt = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "created_at"));
147                return new QESEvidence(issuer, serialNumber, createdAt);
148        }
149}