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