001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2021, 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.attachment;
019
020
021import com.nimbusds.common.contenttype.ContentType;
022import com.nimbusds.jose.util.Base64;
023import com.nimbusds.oauth2.sdk.ParseException;
024import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
025import net.jcip.annotations.Immutable;
026import net.minidev.json.JSONObject;
027
028import java.util.Objects;
029
030
031/**
032 * Embedded attachment.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OpenID Connect for Identity Assurance 1.0
038 * </ul>
039 */
040@Immutable
041public class EmbeddedAttachment extends Attachment {
042        
043        
044        /**
045         * The content.
046         */
047        private final Content content;
048        
049        
050        /**
051         * Creates a new embedded attachment.
052         *
053         * @param content The content. Must not be {@code null}.
054         */
055        public EmbeddedAttachment(final Content content) {
056                
057                super(AttachmentType.EMBEDDED, content.getDescription());
058                this.content = content;
059        }
060        
061        
062        /**
063         * Returns the content.
064         *
065         * @return The content.
066         */
067        public Content getContent() {
068                return content;
069        }
070        
071        
072        @Override
073        public JSONObject toJSONObject() {
074                JSONObject o = super.toJSONObject();
075                o.put("content_type", getContent().getType().toString());
076                o.put("content", getContent().getBase64().toString());
077                return o;
078        }
079        
080        
081        @Override
082        public boolean equals(Object o) {
083                if (this == o) return true;
084                if (!(o instanceof EmbeddedAttachment)) return false;
085                if (!super.equals(o)) return false;
086                EmbeddedAttachment that = (EmbeddedAttachment) o;
087                return getContent().equals(that.getContent());
088        }
089        
090        
091        @Override
092        public int hashCode() {
093                return Objects.hash(super.hashCode(), getContent());
094        }
095        
096        
097        /**
098         * Parses an embedded attachment from the specified JSON object.
099         *
100         * @param jsonObject The JSON object. Must not be {@code null}.
101         *
102         * @return The embedded attachment.
103         *
104         * @throws ParseException If parsing failed.
105         */
106        public static EmbeddedAttachment parse(final JSONObject jsonObject)
107                throws ParseException {
108                
109                ContentType type;
110                try {
111                        type = ContentType.parse(JSONObjectUtils.getString(jsonObject, "content_type"));
112                } catch (java.text.ParseException e) {
113                        throw new ParseException("Invalid content_type: " + e.getMessage(), e);
114                }
115                
116                Base64 base64 = Base64.from(JSONObjectUtils.getNonBlankString(jsonObject, "content"));
117                
118                String description = JSONObjectUtils.getString(jsonObject, "desc", null);
119                
120                return new EmbeddedAttachment(new Content(type, base64, description));
121        }
122}