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 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 Detail

      • DEFAULT_DATE_FORMAT

        public static final java.lang.String DEFAULT_DATE_FORMAT
        The default date format: "EEE, dd MMM yyyy HH:mm:ss z".
        See Also:
        Constant Field Values
      • DEFAULT_TIMEZONE

        public static final java.lang.String DEFAULT_TIMEZONE
        See Also:
        Constant Field Values
    • Constructor Detail

      • CosmosAuthorizationHeader

        public CosmosAuthorizationHeader()
    • Method Detail

      • getResourceType

        @NonNull
        public @NonNull java.lang.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 java.lang.String resourceType)
        The ResourceType portion of the string identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs".
      • getResourceId

        public java.lang.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​(java.lang.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 java.lang.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​(java.lang.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 java.lang.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​(java.lang.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.