I am monitoring the entries in a document database collection by means of REST API from a front end application.
The RESP API queries for a list of documents in the collection based on certain filter criteria.
The token for authenticating the REST API call is generated by means of the .NET SDK.
Here's the code snippet used for the token generation :
string GenerateAuthToken(string verb, string resourceId, string resourceType, string key, string keyType, string tokenVersion)
{
var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(key) };
string verbInput = verb ?? "";
string resourceIdInput = resourceId ?? "";
string resourceTypeInput = resourceType ?? "";
string dateString = DateTime.UtcNow.ToString("r").ToLower();
string payLoad = string.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0}\n{1}\n{2}\n{3}\n{4}\n",
verb.ToLowerInvariant(),
resourceType.ToLowerInvariant(),
resourceId,
dateString,
""
);
byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payLoad));
string signature = Convert.ToBase64String(hashPayLoad);
return System.Web.HttpUtility.UrlEncode(String.Format(System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",
keyType,
tokenVersion,
signature));
}
As per the API documentation:
Resource tokens must be generated by an intermediate server. The server serves as the master-key guardian and generates time-constrained tokens for untrusted clients, such as web browsers.
What is the default expiry time for this token? Is there a way to extend the expiry of the token?
By default, the validity period of a resource token is 1 hour. The validity period can be overridden to up to 5 hours.
If you’re using REST, it must be set in the “x-ms-documentdb-expiry-seconds” header when you create/replace/read Permission.
For more information on how to create one, please refer to https://docs.microsoft.com/en-us/rest/api/documentdb/permissions