401 The input date header is invalid format
My requests to the CosmosDB API (both the emulator and an instance in Azure) fails with 401 Unauthorized, and this body:
{"code":"Unauthorized","message":"The input date header is invalid format. Please pass in RFC 1123 style date format.\r\nActivityId: 0b8ed7e2-f634-492b-990d-72bc67f7513d, Microsoft.Azure.Documents.Common/2.14.0"}
I'm using golang, so need to construct the hashed token signature for a master token, as described in the docs. My implementation matches the documented example, which is to say:
verb := "GET"
resourceType := "dbs"
resourceLink := "dbs/ToDoList"
masterKey := "dsZQi3KtZmCv1ljt3VNWNm7sQUF1y5rJfC6kv5JiwvW0EndXdDku/dkKBp8/ufDToSxLzR4y+O/0H/t4bQtVNw=="
nowTime, _ := time.Parse(time.RFC1123, "Thu, 27 Apr 2017 00:51:12 GMT")
// got matches expect
got, _ := hashTokenSignature(verb, resourceType, resourceLink, masterKey, nowTime)
expect := "c09PEVJrgp2uQRkr934kFbTqhByc7TVr3OHyqlu+c+c="
Now, create the auth token:
// got matches expect
got := url.QueryEscape("type=master&ver=1.0&sig=c09PEVJrgp2uQRkr934kFbTqhByc7TVr3OHyqlu+c+c=")
expect := "type%3Dmaster%26ver%3D1.0%26sig%3Dc09PEVJrgp2uQRkr934kFbTqhByc7TVr3OHyqlu%2Bc%2Bc%3D"
Now let’s consider the date header. The string I use for x-ms-date
is identical to the one I use in the signature constructor: Thu, 27 Apr 2017 00:51:12 GMT
Case does not seem to matter; the error persists whether the RFC 1123 date string is lower-cased or not.
Here is an example query against the emulator, running locally in Docker.
Signature payload (plus an extra newline character):
get
dbs
sat, 07 aug 2021 23:39:32 pdt
Master key:
C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
Request:
Method: GET
URI: https://localhost:8081/dbs/
X-Ms-Version: "2018-12-31"
X-Ms-Date: "Sat, 07 Aug 2021 23:39:32 PDT"
Authorization: "type%3Dmaster%26ver%3D1.0%26sig%3Dhgfum5w73%2FmoXN%2BZDXt1BxmmmGRbUxaRSresf24ZeRY%3D"
Response:
401 Unauthorized {"code":"Unauthorized","message":"The input date header is invalid format. Please pass in RFC 1123 style date format.\r\nActivityId: bc0c28b1-987c-46c3-ad1c-027f1e9781af, Microsoft.Azure.Documents.Common/2.14.0"}