![JAR search and dependency download from the Maven repository](/logo.png)
net.adamcin.httpsig.api.Constants Maven / Gradle / Ivy
/*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to
*/
package net.adamcin.httpsig.api;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Constant values used by the Signature Specification
*/
public final class Constants {
/**
* Identifier for the Signature Authentication scheme
*/
public static final String SCHEME = "Signature";
/**
* Http response header representing a server authentication challenge
*
* @see RFC 2617: HTTP Authentication: Basic and Digest Access Authentication
*/
public static final String CHALLENGE = "WWW-Authenticate";
/**
* Challenge header "realm" parameter
*/
public static final String REALM = "realm";
/**
* Parameter name for challenge-selected SSH Public Key Fingerprint
*/
public static final String KEY_ID = "keyId";
/**
* replaced with {@code request-target} in draft-cavage-http-signatures-03
*/
@Deprecated
public static final String HEADER_REQUEST_LINE = "request-line";
public static final String HEADER_REQUEST_TARGET = "(request-target)";
public static final String HEADER_DATE = "date";
public static final List DEFAULT_HEADERS = Arrays.asList(HEADER_DATE);
/**
* List of headers to always exclude from signature calculation
*/
public static final List IGNORE_HEADERS = Arrays.asList("authorization");
/**
* Http request header representing client credentials
*
* @see RFC 2617: HTTP Authentication: Basic and Digest Access Authentication
*/
public static final String AUTHORIZATION = "Authorization";
/**
* Parameter name for the "headers" authorization parameter
*/
public static final String HEADERS = "headers";
/**
* Parameter name for the "signature" authorization parameter
*/
public static final String SIGNATURE = "signature";
/**
* Challenge header "algorithms" parameter
*/
public static final String ALGORITHMS = "algorithms";
/**
* Challenge header "algorithm" parameter
*/
public static final String ALGORITHM = "algorithm";
/**
* Default encoding for header values.
*/
public static final Charset CHARSET = Charset.forName("ISO-8859-1");
/**
*
*/
public static final Pattern RFC2617_PARAM = Pattern.compile("(\\w+)=\"([^\"]*)\"");
public static List parseTokens(String tokens) {
if (tokens == null || tokens.trim().isEmpty()) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(Arrays.asList(tokens.trim().split("\\s+")));
}
}
public static String constructTokensString(List tokens) {
StringBuilder sb = new StringBuilder();
if (tokens != null && !tokens.isEmpty()) {
for (String token : tokens) {
sb.append(token).append(" ");
}
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
public static Map parseRFC2617(String header) {
Map params = new HashMap();
if (header != null && header.toLowerCase().startsWith(Constants.SCHEME.toLowerCase())) {
final Matcher matcher = RFC2617_PARAM.matcher(header.substring(Constants.SCHEME.length() + 1));
while (matcher.find()) {
params.put(matcher.group(1), matcher.group(2));
}
}
return Collections.unmodifiableMap(params);
}
public static String constructRFC2617(Map params) {
StringBuilder sb = new StringBuilder(SCHEME + ' ');
if (params != null && !params.isEmpty()) {
for (Map.Entry param : params.entrySet()) {
sb.append(param.getKey()).append("=\"").append(param.getValue()).append("\"").append(",");
}
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
public static List filterHeaders(List headers) {
if (headers == null) {
return Collections.emptyList();
}
List _headers = new ArrayList(headers.size());
for (String header : headers) {
if (!IGNORE_HEADERS.contains(header.toLowerCase())) {
_headers.add(header.toLowerCase());
}
}
return Collections.unmodifiableList(_headers);
}
public static final KeyId DEFAULT_KEY_IDENTIFIER = new KeyId() {
public String getId(Key key) {
return key.getId();
}
};
public static final Collection ALL_SUPPORTED_ALGORITHMS = Arrays.asList(Algorithm.values());
public static final Challenge PREEMPTIVE_CHALLENGE = new Challenge(
"", DEFAULT_HEADERS, ALL_SUPPORTED_ALGORITHMS
);
private Constants() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy