
com.nimbusds.jose.PlainHeader Maven / Gradle / Ivy
Go to download
Java library for Javascript Object Signing and Encryption (JOSE) and
JSON Web Tokens (JWT)
package com.nimbusds.jose;
import java.text.ParseException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import net.minidev.json.JSONObject;
import com.nimbusds.jose.util.Base64URL;
import com.nimbusds.jose.util.JSONObjectUtils;
/**
* Plaintext JOSE header.
*
* Supports all {@link #getReservedParameterNames reserved header parameters}
* of the plain specification:
*
*
* - alg (set to {@link Algorithm#NONE "none"}).
*
- typ
*
- cty
*
*
* The header may also carry {@link #setCustomParameters custom parameters};
* these will be serialised and parsed along the reserved ones.
*
*
Example:
*
*
* {
* "alg" : "none"
* }
*
*
* @author Vladimir Dzhuvinov
* @version $version$ (2013-01-08)
*/
public class PlainHeader extends Header implements ReadOnlyPlainHeader {
/**
* The reserved parameter names.
*/
private static final Set RESERVED_PARAMETER_NAMES;
/**
* Initialises the reserved parameter name set.
*/
static {
Set p = new HashSet();
p.add("alg");
p.add("typ");
p.add("cty");
RESERVED_PARAMETER_NAMES = Collections.unmodifiableSet(p);
}
/**
* Creates a new plain header with algorithm
* {@link Algorithm#NONE none}.
*/
public PlainHeader() {
super(Algorithm.NONE);
}
/**
* Gets the reserved parameter names for plain headers.
*
* @return The reserved parameter names, as an unmodifiable set.
*/
public static Set getReservedParameterNames() {
return RESERVED_PARAMETER_NAMES;
}
@Override
public Algorithm getAlgorithm() {
return alg;
}
/**
* @throws IllegalArgumentException If the specified parameter name
* matches a reserved parameter name.
*/
@Override
public void setCustomParameter(final String name, final Object value) {
if (getReservedParameterNames().contains(name))
throw new IllegalArgumentException("The parameter name \"" + name + "\" matches a reserved name");
super.setCustomParameter(name, value);
}
@Override
public Set getIncludedParameters() {
Set includedParameters =
new HashSet(getCustomParameters().keySet());
includedParameters.add("alg");
if (getType() != null)
includedParameters.add("typ");
if (getContentType() != null)
includedParameters.add("cty");
return includedParameters;
}
/**
* Parses a plain header from the specified JSON object.
*
* @param json The JSON object to parse. Must not be {@code null}.
*
* @return The plain header.
*
* @throws ParseException If the specified JSON object doesn't represent
* a valid plain header.
*/
public static PlainHeader parse(final JSONObject json)
throws ParseException {
// Get the "alg" parameter
Algorithm alg = Header.parseAlgorithm(json);
if (alg != Algorithm.NONE)
throw new ParseException("The algorithm \"alg\" header parameter must be \"none\"", 0);
// Create a minimal header, type may be set later
PlainHeader h = new PlainHeader();
// Parse optional + custom parameters
for(final String name: json.keySet()) {
if (name.equals("alg"))
continue; // skip
else if (name.equals("typ"))
h.setType(new JOSEObjectType(JSONObjectUtils.getString(json, name)));
else if (name.equals("cty"))
h.setContentType(JSONObjectUtils.getString(json, name));
else
h.setCustomParameter(name, json.get(name));
}
return h;
}
/**
* Parses a plain header from the specified JSON string.
*
* @param s The JSON string to parse. Must not be {@code null}.
*
* @return The plain header.
*
* @throws ParseException If the specified JSON string doesn't
* represent a valid plain header.
*/
public static PlainHeader parse(final String s)
throws ParseException {
JSONObject jsonObject = JSONObjectUtils.parseJSONObject(s);
return parse(jsonObject);
}
/**
* Parses a plain header from the specified Base64URL.
*
* @param base64URL The Base64URL to parse. Must not be {@code null}.
*
* @return The plain header.
*
* @throws ParseException If the specified Base64URL doesn't represent a
* valid plain header.
*/
public static PlainHeader parse(final Base64URL base64URL)
throws ParseException {
if (base64URL == null)
throw new ParseException("The Base64URL must not be null", 0);
return parse(base64URL.decodeToString());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy