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.federation.trust.marks; 019 020 021import java.util.Map; 022import java.util.Objects; 023 024import net.jcip.annotations.Immutable; 025import net.minidev.json.JSONObject; 026 027import com.nimbusds.jose.JWSObject; 028import com.nimbusds.jwt.SignedJWT; 029import com.nimbusds.oauth2.sdk.ParseException; 030import com.nimbusds.oauth2.sdk.id.Identifier; 031import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 032 033 034/** 035 * Trust mark entry. 036 * 037 * <p>Related specifications: 038 * 039 * <ul> 040 * <li>OpenID Connect Federation 1.0, sections 3.1 and 5.3. 041 * </ul> 042 */ 043@Immutable 044public final class TrustMarkEntry implements Map.Entry<Identifier, SignedJWT> { 045 046 047 /** 048 * The trust mark identifier. 049 */ 050 private final Identifier id; 051 052 053 /** 054 * The trust mark. 055 */ 056 private final SignedJWT trustMark; 057 058 059 /** 060 * Creates a new trust mark entry. 061 * 062 * @param id The identifier. Must not be {@code null}. 063 * @param trustMark The trust mark. Must not be {@code null}. 064 */ 065 public TrustMarkEntry(final Identifier id, final SignedJWT trustMark) { 066 Objects.requireNonNull(id); 067 this.id = id; 068 Objects.requireNonNull(trustMark); 069 if (JWSObject.State.UNSIGNED.equals(trustMark.getState())) { 070 throw new IllegalArgumentException("The trust mark must be in a signed state"); 071 } 072 this.trustMark = trustMark; 073 } 074 075 076 /** 077 * Returns the identifier. 078 * 079 * @return The identifier. 080 */ 081 public Identifier getID() { 082 return id; 083 } 084 085 086 /** 087 * Returns the trust mark. 088 * 089 * @return The trust mark. 090 */ 091 public SignedJWT getTrustMark() { 092 return trustMark; 093 } 094 095 096 @Override 097 public Identifier getKey() { 098 return getID(); 099 } 100 101 102 @Override 103 public SignedJWT getValue() { 104 return getTrustMark(); 105 } 106 107 108 @Override 109 public SignedJWT setValue(SignedJWT signedJWT) { 110 throw new UnsupportedOperationException(); 111 } 112 113 114 /** 115 * Returns a JSON object representation of this entry. 116 * 117 * @return The JSON object. 118 */ 119 public JSONObject toJSONObject() { 120 JSONObject o = new JSONObject(); 121 o.put("id", getID().getValue()); 122 o.put("trust_mark", getTrustMark().serialize()); 123 return o; 124 } 125 126 127 /** 128 * Parses a trust mark entry from the specified JSON object. 129 * 130 * @param jsonObject The JSON object. Must not be {@code null}. 131 * 132 * @return The trust mark entry. 133 * 134 * @throws ParseException If parsing failed. 135 */ 136 public static TrustMarkEntry parse(final JSONObject jsonObject) 137 throws ParseException { 138 139 String idString = JSONObjectUtils.getString(jsonObject, "id"); 140 String jwtString = JSONObjectUtils.getString(jsonObject, "trust_mark"); 141 try { 142 return new TrustMarkEntry(new Identifier(idString), SignedJWT.parse(jwtString)); 143 } catch (java.text.ParseException e) { 144 throw new ParseException(e.getMessage(), e); 145 } 146 } 147}