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 java.util.Objects;
022
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 5.1.1.5.
038 * </ul>
039 *
040 * @deprecated Use {@link ElectronicSignatureEvidence} instead.
041 */
042@Deprecated
043public class QESEvidence extends IdentityEvidence {
044        
045        
046        /**
047         * The QES issuer.
048         */
049        private final Issuer issuer;
050        
051        
052        /**
053         * The QES serial number.
054         */
055        private final String serialNumber;
056        
057        
058        /**
059         * The QES creation time.
060         */
061        private final DateWithTimeZoneOffset createdAt;
062        
063        
064        /**
065         * Creates a new QES used as identity evidence.
066         *
067         * @param issuer       The QES issuer, {@code null} if not specified.
068         * @param serialNumber The QES serial number, {@code null} if not
069         *                     specified.
070         * @param createdAt    The QES creation time, {@code null} if not
071         *                     specified.
072         */
073        public QESEvidence(final Issuer issuer, final String serialNumber, final DateWithTimeZoneOffset createdAt) {
074                
075                super(IdentityEvidenceType.QES, null);
076                this.issuer = issuer;
077                this.serialNumber = serialNumber;
078                this.createdAt = createdAt;
079        }
080        
081        
082        /**
083         * Returns the QES issuer.
084         * @return The QES issuer, {@code null} if not specified.
085         */
086        public Issuer getQESIssuer() {
087                return issuer;
088        }
089        
090        
091        /**
092         * Returns the QES serial number.
093         *
094         * @return The QES serial number string, {@code null} if not specified.
095         */
096        public String getQESSerialNumberString() {
097                return serialNumber;
098        }
099        
100        
101        /**
102         * Returns The QES creation time.
103         *
104         * @return The QES creation time, {@code null} if not specified.
105         */
106        public DateWithTimeZoneOffset getQESCreationTime() {
107                return createdAt;
108        }
109        
110        
111        @Override
112        public JSONObject toJSONObject() {
113                
114                JSONObject o = super.toJSONObject();
115                if (getQESIssuer() != null) {
116                        o.put("issuer", getQESIssuer().getValue());
117                }
118                if (getQESSerialNumberString() != null) {
119                        o.put("serial_number", getQESSerialNumberString());
120                }
121                if (getQESCreationTime() != null) {
122                        o.put("created_at", getQESCreationTime().toISO8601String());
123                }
124                return o;
125        }
126        
127        
128        @Override
129        public boolean equals(Object o) {
130                if (this == o) return true;
131                if (!(o instanceof QESEvidence)) return false;
132                QESEvidence evidence = (QESEvidence) o;
133                return Objects.equals(getQESIssuer(), evidence.getQESIssuer()) &&
134                        Objects.equals(getQESSerialNumberString(), evidence.getQESSerialNumberString()) &&
135                        Objects.equals(getQESCreationTime(), evidence.getQESCreationTime());
136        }
137        
138        
139        @Override
140        public int hashCode() {
141                return Objects.hash(getQESIssuer(), getQESSerialNumberString(), getQESCreationTime());
142        }
143        
144        
145        /**
146         * Parses a new QES evidence from the specified JSON object.
147         *
148         * @param jsonObject The JSON object. Must not be {@code null}.
149         *
150         * @return The QES evidence.
151         *
152         * @throws ParseException If parsing failed.
153         */
154        public static QESEvidence parse(final JSONObject jsonObject)
155                throws ParseException {
156                
157                ensureType(IdentityEvidenceType.QES, jsonObject);
158                
159                Issuer issuer = null;
160                if (jsonObject.get("issuer") != null) {
161                        issuer = new Issuer(JSONObjectUtils.getString(jsonObject, "issuer"));
162                }
163                
164                String serialNumber = JSONObjectUtils.getString(jsonObject, "serial_number", null);
165                
166                DateWithTimeZoneOffset createdAt = null;
167                if (jsonObject.get("created_at") != null) {
168                        createdAt = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "created_at"));
169                }
170                
171                return new QESEvidence(issuer, serialNumber, createdAt);
172        }
173}