Class CosmosAuthorizationHeader
- All Implemented Interfaces:
AdaptrisComponent
,ComponentLifecycle
,ComponentLifecycleExtension
,MessageEventGenerator
,Service
,StateManagedComponent
Builds an authorization header using master keys based on the
CosmosDB documentation.
Once the key is built, then you can send it as your Authorization
header. Under the covers we actually use
com.microsoft.azure.documentdb.internal.BaseAuthorizationTokenProvider
from the Cosmos Java SDK; but effectively what we
do is this (you could do this in an com.adaptris.core.services.EmbeddedScriptingService
or similar).
verb = "GET";
resourceType = "the resource type";
resourceId = "the resource id";
// From java.time
date = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneId.of("GMT"));
key = "The key";
keyType = "master";
tokenVersion = "1.0";
stringToSign = verb.toLowerCase() + "\n"
+ resourceType.toLowerCase() + "\n"
+ resourceId
+ "\n" + date.toLowerCase() + "\n"
+ "" + "\n";
// From org.apache.commons.codec, unbase64 the key, and use it to hmac, then back to base64 again.
sigBytes = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, Base64.decodeBase64(key))
.hmac(stringToSign.getBytes("UTF-8"));
signature = Base64.encodeBase64String(sigBytes);
// from java.net
authorization = URLEncoder.encode("type=" + keyType + "&ver=" + tokenVersion + "&sig=" + signature, "UTF-8");
Since the date is also part of the signed data, it will also add the date it used against the key "x-ms-date"
so that you
can send it as a HTTP request header.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The default date format: "EEE, dd MMM yyyy HH:mm:ss z".static final String
Fields inherited from class com.adaptris.interlok.azure.cosmosdb.CosmosAuthorizationHeaderImpl
DEFAULT_METADATA_KEY, X_MS_DATE
Fields inherited from class com.adaptris.core.ServiceImp
log
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
doService
(AdaptrisMessage msg) Override the default date format.The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive.@NonNull String
The ResourceType portion of the string identifies the type of resource that the request is for, Eg.Set the timezone as required.void
prepare()
void
setDateFormat
(String dateFormat) Override the default date format.void
setResourceId
(String resourceId) The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive.void
setResourceType
(@NonNull String resourceType) The ResourceType portion of the string identifies the type of resource that the request is for, Eg.void
setTimezone
(String timezone) Set the timezone as required.Methods inherited from class com.adaptris.interlok.azure.cosmosdb.CosmosAuthorizationHeaderImpl
closeService, getHttpVerb, getMasterKey, getTargetKey, initService, setHttpVerb, setMasterKey, setTargetKey, targetKey, withHttpVerb, withMasterKey, withTargetKey
Methods inherited from class com.adaptris.core.ServiceImp
changeState, close, continueOnFailure, createName, createQualifier, getContinueOnFail, getIsTrackingEndpoint, getUniqueId, init, isBranching, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, setContinueOnFail, setIsTrackingEndpoint, setUniqueId, start, stop
-
Field Details
-
DEFAULT_DATE_FORMAT
The default date format: "EEE, dd MMM yyyy HH:mm:ss z".- See Also:
-
DEFAULT_TIMEZONE
- See Also:
-
-
Constructor Details
-
CosmosAuthorizationHeader
public CosmosAuthorizationHeader()
-
-
Method Details
-
prepare
- Specified by:
prepare
in interfaceComponentLifecycleExtension
- Overrides:
prepare
in classCosmosAuthorizationHeaderImpl
- Throws:
CoreException
-
doService
- Throws:
ServiceException
-
withResourceId
-
withResourceType
-
getResourceType
The ResourceType portion of the string identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs". -
setResourceType
The ResourceType portion of the string identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs". -
getResourceId
The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive. For example, a collection it might look like: "dbs/MyDatabase/colls/MyCollection". -
setResourceId
The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive. For example, a collection it might look like: "dbs/MyDatabase/colls/MyCollection". -
getTimezone
Set the timezone as required.By default it is set to GMT, as this is the expected date style for CosmosDB. You shouldn't need to change it.
-
setTimezone
Set the timezone as required.By default it is set to GMT, as this is the expected date style for CosmosDB. You shouldn't need to change it.
-
getDateFormat
Override the default date format.By default it is set to be "EEE, dd MMM yyyy HH:mm:ss z" which imposes a leading digit on the day if required;
DateTimeFormatter.RFC_1123_DATE_TIME
doesn't appear suitable for use with Azure CosmosDB as it doesn't provide the leading digit. -
setDateFormat
Override the default date format.By default it is set to be "EEE, dd MMM yyyy HH:mm:ss z" which imposes a leading digit on the day if required;
DateTimeFormatter.RFC_1123_DATE_TIME
doesn't appear suitable for use with Azure CosmosDB as it doesn't provide the leading digit.
-