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