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.List;
022import java.util.Objects;
023
024import net.minidev.json.JSONObject;
025
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.id.Issuer;
028import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
029import com.nimbusds.oauth2.sdk.util.date.DateWithTimeZoneOffset;
030import com.nimbusds.openid.connect.sdk.assurance.evidences.attachment.Attachment;
031
032
033/**
034 * Electronic signature used as identity evidence.
035 *
036 * <p>Related specifications:
037 *
038 * <ul>
039 *     <li>OpenID Connect for Identity Assurance 1.0, section 5.1.1.5.
040 * </ul>
041 */
042public class ElectronicSignatureEvidence extends IdentityEvidence {
043        
044        
045        /**
046         * The signature type.
047         */
048        private final SignatureType signatureType;
049        
050        
051        /**
052         * The signature issuer.
053         */
054        private final Issuer issuer;
055        
056        
057        /**
058         * The certificate serial number.
059         */
060        private final SerialNumber certificateSerialNumber;
061        
062        
063        /**
064         * The signature creation time.
065         */
066        private final DateWithTimeZoneOffset createdAt;
067        
068        
069        /**
070         * Creates a new signature used as identity evidence.
071         *
072         * @param signatureType            The signature type. Must not be
073         *                                 {@code null}.
074         * @param issuer                   The signature issuer, {@code null}
075         *                                 if not specified.
076         * @param certificateSerialNumber  The certificate serial number,
077         *                                 {@code null} if not specified.
078         * @param createdAt                The signature creation time,
079         *                                 {@code null} if not specified.
080         * @param attachments              The optional attachments,
081         *                                 {@code null} if not specified.
082         */
083        public ElectronicSignatureEvidence(final SignatureType signatureType,
084                                           final Issuer issuer,
085                                           final SerialNumber certificateSerialNumber,
086                                           final DateWithTimeZoneOffset createdAt,
087                                           final List<Attachment> attachments) {
088                
089                super(IdentityEvidenceType.ELECTRONIC_SIGNATURE, attachments);
090                Objects.requireNonNull(signatureType);
091                this.signatureType = signatureType;
092                this.issuer = issuer;
093                this.certificateSerialNumber = certificateSerialNumber;
094                this.createdAt = createdAt;
095        }
096        
097        
098        /**
099         * Returns the signature type.
100         *
101         * @return The signature type.
102         */
103        public SignatureType getSignatureType() {
104                return signatureType;
105        }
106        
107        
108        /**
109         * Returns the signature issuer.
110         *
111         * @return The signature issuer, {@code null} if not specified.
112         */
113        public Issuer getIssuer() {
114                return issuer;
115        }
116        
117        
118        /**
119         * Returns the certificate serial number.
120         *
121         * @return The certificate serial number string, {@code null} if not
122         *         specified.
123         */
124        public SerialNumber getCertificateSerialNumber() {
125                return certificateSerialNumber;
126        }
127        
128        
129        /**
130         * Returns The signature creation time.
131         *
132         * @return The signature creation time, {@code null} if not specified.
133         */
134        public DateWithTimeZoneOffset getCreationTime() {
135                return createdAt;
136        }
137        
138        
139        @Override
140        public JSONObject toJSONObject() {
141                
142                JSONObject o = super.toJSONObject();
143                
144                o.put("signature_type", getSignatureType().getValue());
145                
146                if (getIssuer() != null) {
147                        o.put("issuer", getIssuer().getValue());
148                }
149                if (getCertificateSerialNumber() != null) {
150                        o.put("serial_number", getCertificateSerialNumber().getValue());
151                }
152                if (getCreationTime() != null) {
153                        o.put("created_at", getCreationTime().toISO8601String());
154                }
155                return o;
156        }
157        
158        
159        @Override
160        public boolean equals(Object o) {
161                if (this == o) return true;
162                if (!(o instanceof ElectronicSignatureEvidence)) return false;
163                ElectronicSignatureEvidence evidence = (ElectronicSignatureEvidence) o;
164                return getSignatureType().equals(evidence.getSignatureType()) &&
165                        Objects.equals(getIssuer(), evidence.getIssuer()) &&
166                        Objects.equals(getCertificateSerialNumber(), evidence.getCertificateSerialNumber()) &&
167                        Objects.equals(createdAt, evidence.createdAt);
168        }
169        
170        
171        @Override
172        public int hashCode() {
173                return Objects.hash(getSignatureType(), getIssuer(), getCertificateSerialNumber(), createdAt);
174        }
175        
176        
177        /**
178         * Parses a new signature evidence from the specified JSON object.
179         *
180         * @param jsonObject The JSON object. Must not be {@code null}.
181         *
182         * @return The signature evidence.
183         *
184         * @throws ParseException If parsing failed.
185         */
186        public static ElectronicSignatureEvidence parse(final JSONObject jsonObject)
187                throws ParseException {
188                
189                ensureType(IdentityEvidenceType.ELECTRONIC_SIGNATURE, jsonObject);
190                
191                SignatureType signatureType = new SignatureType(JSONObjectUtils.getString(jsonObject, "signature_type"));
192                
193                Issuer issuer = null;
194                if (jsonObject.get("issuer") != null) {
195                        issuer = new Issuer(JSONObjectUtils.getString(jsonObject, "issuer"));
196                }
197                
198                SerialNumber serialNumber = null;
199                if (jsonObject.get("serial_number") != null) {
200                        serialNumber = new SerialNumber(JSONObjectUtils.getString(jsonObject, "serial_number", null));
201                }
202                
203                DateWithTimeZoneOffset createdAt = null;
204                if (jsonObject.get("created_at") != null) {
205                        createdAt = DateWithTimeZoneOffset.parseISO8601String(JSONObjectUtils.getString(jsonObject, "created_at"));
206                }
207                
208                List<Attachment> attachments = null;
209                if (jsonObject.get("attachments") != null) {
210                        attachments = Attachment.parseList(JSONObjectUtils.getJSONArray(jsonObject, "attachments"));
211                }
212                
213                return new ElectronicSignatureEvidence(signatureType, issuer, serialNumber, createdAt, attachments);
214        }
215}