Class CosmosAuthorizationHeader

All Implemented Interfaces:
AdaptrisComponent, ComponentLifecycle, ComponentLifecycleExtension, MessageEventGenerator, Service, StateManagedComponent

@ComponentProfile(summary="Builds an authorization header for Azure CosmosDB", since="3.9.2", tag="azure,cosmosdb,cosmos") public class CosmosAuthorizationHeader extends CosmosAuthorizationHeaderImpl
Builds an authorization header for Azure CosmosDB from an explicit ResourceID / ResourceType

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 Details

  • Constructor Details

    • CosmosAuthorizationHeader

      public CosmosAuthorizationHeader()
  • Method Details

    • prepare

      public void prepare() throws CoreException
      Specified by:
      prepare in interface ComponentLifecycleExtension
      Overrides:
      prepare in class CosmosAuthorizationHeaderImpl
      Throws:
      CoreException
    • doService

      public void doService(AdaptrisMessage msg) throws ServiceException
      Throws:
      ServiceException
    • withResourceId

      public CosmosAuthorizationHeader withResourceId(String s)
    • withResourceType

      public CosmosAuthorizationHeader withResourceType(String s)
    • getResourceType

      @NonNull public @NonNull String getResourceType()
      The ResourceType portion of the string identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs".
    • setResourceType

      public void setResourceType(@NonNull @NonNull String resourceType)
      The ResourceType portion of the string identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs".
    • getResourceId

      public String 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

      public 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. For example, a collection it might look like: "dbs/MyDatabase/colls/MyCollection".
    • getTimezone

      public String 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

      public void setTimezone(String timezone)
      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

      public String 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

      public void setDateFormat(String dateFormat)
      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.