001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2013-2015 ForgeRock AS. 015 */ 016 017package org.forgerock.json.jose.builders; 018 019import java.net.URL; 020import java.util.List; 021 022import org.forgerock.json.jose.jwk.JWK; 023 024/** 025 * A base implementation of a JWT header builder, for the common security header parameters shared by the JWS and JWE 026 * headers, that provides a fluent builder pattern to creating JWT headers. 027 * <p> 028 * See {@link org.forgerock.json.jose.jws.JwtSecureHeader} for information on the JwtSecureHeader object that this 029 * builder creates. 030 * 031 * @param <T> the type of JwtBuilder that parents this JwtHeaderBuilder. 032 * @param <B> the type of this JwtHeaderBuilder 033 * 034 * @since 2.0.0 035 */ 036public abstract class JwtSecureHeaderBuilder<T extends JwtBuilder, B extends JwtSecureHeaderBuilder<T, B>> 037 extends JwtHeaderBuilder<T, B> { 038 039 /** 040 * Constructs a new JwtSecureHeaderBuilder, parented by the given JwtBuilder. 041 * 042 * @param jwtBuilder The JwtBuilder instance that this JwtSecureHeaderBuilder is a child of. 043 */ 044 public JwtSecureHeaderBuilder(T jwtBuilder) { 045 super(jwtBuilder); 046 } 047 048 /** 049 * Sets the JWK Set URL header parameter for this JWS. 050 * <p> 051 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setJwkSetUrl(java.net.URL) 052 * 053 * @param jku The JWK Set URL. 054 * @return This JwtSecureHeaderBuilder. 055 */ 056 @SuppressWarnings("unchecked") 057 public B jku(URL jku) { 058 header("jku", jku); 059 return (B) this; 060 } 061 062 /** 063 * Sets the JSON Web Key header parameter for this JWS. 064 * <p> 065 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setJsonWebKey(org.forgerock.json.jose.jwk.JWK) 066 * 067 * @param jwk The JSON Web Key. 068 * @return This JwtSecureHeaderBuilder. 069 */ 070 @SuppressWarnings("unchecked") 071 public B jwk(JWK jwk) { 072 header("jwk", jwk); 073 return (B) this; 074 } 075 076 /** 077 * Sets the X.509 URL header parameter for this JWS. 078 * <p> 079 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setX509Url(java.net.URL) 080 * 081 * @param x5u THe X.509 URL. 082 * @return This JwtSecureHeaderBuilder. 083 */ 084 @SuppressWarnings("unchecked") 085 public B x5u(URL x5u) { 086 header("x5u", x5u); 087 return (B) this; 088 } 089 090 /** 091 * Sets the X.509 Certificate Thumbprint header parameter for this JWS. 092 * <p> 093 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setX509CertificateThumbprint(String) 094 * 095 * @param x5t The X.509 Certificate Thumbprint. 096 * @return This JwtSecureHeaderBuilder. 097 */ 098 @SuppressWarnings("unchecked") 099 public B x5t(String x5t) { 100 header("x5t", x5t); 101 return (B) this; 102 } 103 104 /** 105 * Sets the X.509 Certificate Chain header parameter for this JWS. 106 * <p> 107 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setX509CertificateChain(java.util.List) 108 * 109 * @param x5c The X.509 Certificate Chain. 110 * @return This JwtSecureHeaderBuilder. 111 */ 112 @SuppressWarnings("unchecked") 113 public B x5c(List<String> x5c) { 114 header("x5c", x5c); 115 return (B) this; 116 } 117 118 /** 119 * Sets the Key ID header parameter for this JWS. 120 * <p> 121 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setKeyId(String) 122 * 123 * @param kid The Key ID. 124 * @return This JwtSecureHeaderBuilder. 125 */ 126 @SuppressWarnings("unchecked") 127 public B kid(String kid) { 128 header("kid", kid); 129 return (B) this; 130 } 131 132 /** 133 * Sets the content type header parameter for this JWS. 134 * <p> 135 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setContentType(String) 136 * 137 * @param cty The content type of the JWS payload. 138 * @return This JwtSecureHeaderBuilder. 139 */ 140 @SuppressWarnings("unchecked") 141 public B cty(String cty) { 142 header("cty", cty); 143 return (B) this; 144 } 145 146 /** 147 * Sets the critical header parameters for this JWS. 148 * <p> 149 * @see org.forgerock.json.jose.jws.JwtSecureHeader#setCriticalHeaders(java.util.List) 150 * 151 * @param crit A List of the JWS critical parameters. 152 * @return This JwtSecureHeaderBuilder. 153 */ 154 @SuppressWarnings("unchecked") 155 public B crit(List<String> crit) { 156 header("crit", crit); 157 return (B) this; 158 } 159}