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.oauth2.sdk.dpop; 019 020 021import com.nimbusds.jose.JOSEException; 022import com.nimbusds.jose.jwk.JWK; 023import com.nimbusds.jose.util.Base64URL; 024import com.nimbusds.jwt.JWTClaimsSet; 025import com.nimbusds.oauth2.sdk.ParseException; 026import com.nimbusds.oauth2.sdk.cnf.AbstractConfirmation; 027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 028import net.jcip.annotations.Immutable; 029import net.minidev.json.JSONObject; 030 031import java.util.AbstractMap; 032import java.util.Map; 033import java.util.Objects; 034 035 036/** 037 * JSON Web Key (JWK) SHA-256 thumbprint confirmation. 038 */ 039@Immutable 040public final class JWKThumbprintConfirmation extends AbstractConfirmation { 041 042 043 /** 044 * The JWK SHA-256 thumbprint. 045 */ 046 private final Base64URL jkt; 047 048 049 /** 050 * Creates a new JWK SHA-256 thumbprint. 051 * 052 * @param jkt The JWK SHA-256 thumbprint. Must not be {@code null}. 053 */ 054 public JWKThumbprintConfirmation(final Base64URL jkt) { 055 this.jkt = Objects.requireNonNull(jkt); 056 } 057 058 059 /** 060 * Returns the JWK SHA-256 thumbprint. 061 * 062 * @return The JWK SHA-256 thumbprint. 063 */ 064 public Base64URL getValue() { 065 066 return jkt; 067 } 068 069 070 @Override 071 public Map.Entry<String,JSONObject> toJWTClaim() { 072 073 JSONObject cnf = new JSONObject(); 074 cnf.put("jkt", jkt.toString()); 075 076 return new AbstractMap.SimpleImmutableEntry<>( 077 "cnf", 078 cnf 079 ); 080 } 081 082 083 @Override 084 public boolean equals(Object o) { 085 if (this == o) return true; 086 if (!(o instanceof JWKThumbprintConfirmation)) return false; 087 JWKThumbprintConfirmation that = (JWKThumbprintConfirmation) o; 088 return jkt.equals(that.jkt); 089 } 090 091 092 @Override 093 public int hashCode() { 094 return Objects.hash(jkt); 095 } 096 097 098 /** 099 * Parses a JWK SHA-256 thumbprint confirmation from the specified JWT 100 * claims set. 101 * 102 * @param jwtClaimsSet The JWT claims set. 103 * 104 * @return The JWK SHA-256 thumbprint confirmation, {@code null} if not 105 * found. 106 */ 107 public static JWKThumbprintConfirmation parse(final JWTClaimsSet jwtClaimsSet) { 108 109 JSONObject cnf = parseConfirmationJSONObject(jwtClaimsSet); 110 111 if (cnf == null) { 112 return null; 113 } 114 115 return parseFromConfirmationJSONObject(cnf); 116 } 117 118 119 /** 120 * Parses a JWK SHA-256 thumbprint confirmation from the specified JSON 121 * object representation of a JWT claims set. 122 * 123 * @param jsonObject The JSON object. 124 * 125 * @return The JWK SHA-256 thumbprint confirmation, {@code null} if not 126 * found. 127 */ 128 public static JWKThumbprintConfirmation parse(final JSONObject jsonObject) { 129 130 if (! jsonObject.containsKey("cnf")) { 131 return null; 132 } 133 134 try { 135 return parseFromConfirmationJSONObject(JSONObjectUtils.getJSONObject(jsonObject, "cnf")); 136 } catch (ParseException e) { 137 return null; 138 } 139 } 140 141 142 /** 143 * Parses a JWK SHA-256 thumbprint confirmation from the specified 144 * confirmation ("cnf") JSON object. 145 * 146 * @param cnf The confirmation JSON object, {@code null} if none. 147 * 148 * @return The JWK SHA-256 thumbprint confirmation, {@code null} if not 149 * found. 150 */ 151 public static JWKThumbprintConfirmation parseFromConfirmationJSONObject(final JSONObject cnf) { 152 153 if (cnf == null) { 154 return null; 155 } 156 157 try { 158 String jktString = JSONObjectUtils.getNonBlankString(cnf, "jkt"); 159 return new JWKThumbprintConfirmation(new Base64URL(jktString)); 160 } catch (ParseException e) { 161 return null; 162 } 163 } 164 165 166 /** 167 * Creates a confirmation of the specified JWK. 168 * 169 * @param jwk The JWK. 170 * 171 * @return The JWK SHA-256 thumbprint confirmation. 172 * 173 * @throws JOSEException If the thumbprint computation failed. 174 */ 175 public static JWKThumbprintConfirmation of(final JWK jwk) 176 throws JOSEException { 177 178 return new JWKThumbprintConfirmation(jwk.computeThumbprint()); 179 } 180}