Class Snws2AuthorizationBuilder
Authorization header values using the SolarNetwork
authentication scheme V2.
This builder can be used to calculate a one-off header value, for example:
String authHeader = new Snws2AuthorizationBuilder("my-token")
.path("/solarquery/api/v1/pub/...")
.build("my-token-secret");
Or the builder can be re-used for a given token:
// create a builder for a token
Snws2AuthorizationBuilder builder = new Snws2AuthorizationBuilder("my-token");
// elsewhere, re-use the builder for repeated requests
builder.reset()
.path("/solarquery/api/v1/pub/...")
.build("my-token-secret");
Additionally, a signing key can be generated and re-used for up to 7 days:
// create a builder for a token
Snws2AuthorizationBuilder builder = new Snws2AuthorizationBuilder("my-token")
.saveSigningKey("my-token-secret");
// elsewhere, re-use the builder for repeated requests
builder.reset()
.path("/solarquery/api/v1/pub/...")
.build(); // note no argument call here, so previously generated key used
This class is not thread safe and should not be used concurrently.
- Version:
- 1.0
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe defaulthostvalue.static final StringThe authorization scheme name.static final StringThe message used to sign the derived signing key.Fields inherited from class net.solarnetwork.security.AbstractAuthorizationBuilder
AUTHORIZATION_COMPONENT_CREDENTIAL, AUTHORIZATION_COMPONENT_HEADERS, AUTHORIZATION_COMPONENT_SIGNATURE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringcomputeCanonicalRequestMessage(String[] headerNames) Compute the canonical request message.contentMD5(String md5) Set the HTTP body content MD5 digest.contentType(String contentType) Set the HTTP content type.Set the request date.Set the HTTP body content digest.booleanGet the date header style.Set the HTTP method.parameterMap(Map<String, String[]> params) Set the HTTPGETquery parameters, orPOSTform-encoded parameters.queryParams(Map<String, String> params) Set the HTTPGETquery parameters, orPOSTform-encoded parameters, as a simpleMap.reset()Reset values to their defaults.protected StringGet the authorization scheme name.signedHttpHeaders(Set<String> signedHeaderNames) Set the explicit HTTP header names to sign with the digest.protected StringGet the signing key message.String[]Get all configured header names as a sorted array of lower-case values.useSnDate(boolean useSnDate) Set the date header style.Methods inherited from class net.solarnetwork.security.AbstractAuthorizationBuilder
appendContentSha256, appendHeaders, build, build, buildSignature, buildSignature, computeCanonicalRequestMessage, computeSignatureData, computeSigningKey, contentSha256, getContentSha256, getDate, getHeaders, getPath, getVerb, header, headers, headerValue, headerValues, host, path, saveSigningKey, signingKey, signingKeyHex, verb
-
Field Details
-
SCHEME_NAME
The authorization scheme name.- See Also:
-
SIGNING_KEY_MESSAGE
The message used to sign the derived signing key.- See Also:
-
DEFAULT_HOST
The defaulthostvalue.- See Also:
-
-
Constructor Details
-
Snws2AuthorizationBuilder
Construct with a token ID. The builder will be initialized and thenreset()will be called so default values are configured.- Parameters:
tokenId- The token ID to use.
-
-
Method Details
-
reset
Reset values to their defaults.All properties will be set to null except the following:
- method
- Will be set to
GET. - host
- Will be set to
DEFAULT_HOST. - path
- Will be set to
/. - date
- Will be set to the current time.
- headers
- Will be cleared.
- parameters
- Will be cleared.
- contentSha256
- Will be cleared.
- signingKey
- This value will not be changed.
- Overrides:
resetin classAbstractAuthorizationBuilder<Snws2AuthorizationBuilder>- Returns:
- this builder
-
useSnDate
Set the date header style.The existing date header will be renamed to match the desired header per
useSnDateif the desired header does not already exist. Either way, the old header value will be removed if it exists.- Parameters:
useSnDate- true to use the X-SN-Date header, false to use Date- Returns:
- this builder
-
isUseSnDate
public boolean isUseSnDate()Get the date header style.- Returns:
- true to use the X-SN-Date header, false to use Date
-
method
Set the HTTP method.This is an alias for
AbstractAuthorizationBuilder.verb(String).- Parameters:
method- the method- Returns:
- this builder
-
date
Set the request date.This will also set the date header with the date's formatted value.
- Overrides:
datein classAbstractAuthorizationBuilder<Snws2AuthorizationBuilder>- Parameters:
date- the date to use, or null for the current system time viaInstant.now(); will be truncated to second resolution- Returns:
- this builder
-
contentType
Set the HTTP content type.This is a shortcut for calling
#header(String, String)with a Content-Type key.- Parameters:
contentType- The content type to use.- Returns:
- this builder
-
contentMD5
Set the HTTP body content MD5 digest.This is a shortcut for calling
#header(String, String)with a Content-MD5 key.- Parameters:
md5- The content MD5 to use.- Returns:
- this builder
-
digest
Set the HTTP body content digest.This is a shortcut for calling
#headers(String, String)with a Digest key.- Parameters:
digest- The digest to use.- Returns:
- this builder
-
parameterMap
Set the HTTPGETquery parameters, orPOSTform-encoded parameters.- Parameters:
params- the request parameters to use- Returns:
- this builder
-
queryParams
Set the HTTPGETquery parameters, orPOSTform-encoded parameters, as a simpleMap.- Parameters:
params- the parameters to use- Returns:
- this builder
-
signedHttpHeaders
Set the explicit HTTP header names to sign with the digest.This completely overrides the header names provided in the
AbstractAuthorizationBuilder.getHeaders()map.- Parameters:
signedHeaderNames- the explicit HTTP header names to include in the computed digest- Returns:
- this builder
-
sortedHeaderNames
Description copied from class:AbstractAuthorizationBuilderGet all configured header names as a sorted array of lower-case values.- Overrides:
sortedHeaderNamesin classAbstractAuthorizationBuilder<Snws2AuthorizationBuilder>- Returns:
- the sorted array, never null
-
signingKeyMessageLiteral
Description copied from class:AbstractAuthorizationBuilderGet the signing key message.- Specified by:
signingKeyMessageLiteralin classAbstractAuthorizationBuilder<Snws2AuthorizationBuilder>- Returns:
- the signing key message
-
schemeName
Description copied from class:AbstractAuthorizationBuilderGet the authorization scheme name.- Specified by:
schemeNamein classAbstractAuthorizationBuilder<Snws2AuthorizationBuilder>- Returns:
- the scheme name
-
computeCanonicalRequestMessage
Description copied from class:AbstractAuthorizationBuilderCompute the canonical request message.- Specified by:
computeCanonicalRequestMessagein classAbstractAuthorizationBuilder<Snws2AuthorizationBuilder>- Parameters:
headerNames- the header names to include in the signature- Returns:
- the canonical request message
-