Skip to main content

Crate cosmos_signature

Crate cosmos_signature 

Source
Expand description

The Azure Cosmos DB master-key Authorization header.

Pure functions and known-answer tests. No HTTP client, no async runtime, no TLS stack — this crate computes a header value and stops.

§Why it is worth having separately

Three details of the signing algorithm each produce an indistinguishable 401, and none can be checked against a live account without a subscription:

  1. The case of each line. The verb and the resource type are lowercased; the resource link is not. Lowercasing the link too is the classic mistake.
  2. The date appears identically in two places — in the string to sign and in the x-ms-date header — and must be lowercase RFC 1123 in GMT in both.
  3. There is a trailing empty line. The string to sign ends \n\n.

All three can be checked against the published algorithm, here, offline. MasterKey does that; the transport is somebody else’s problem.

§Examples

use cosmos_signature::{MasterKey, ResourceType, date};
use time::macros::datetime;

// The account key, as the Azure portal prints it.
let key = MasterKey::new("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")?;

let when = date(datetime!(2026-08-22 09:15:00 UTC));
let header = key.authorization("GET", ResourceType::Docs, "dbs/MyDb/colls/MyColl/docs/Config", &when);

// Send `header` as `Authorization` and `when` as `x-ms-date` — the same
// string, in both places.
assert!(header.starts_with("type%3Dmaster"));
assert_eq!(when, "sat, 22 aug 2026 09:15:00 gmt");

Structs§

MasterKey
A validated Cosmos DB account key, ready to sign with.

Enums§

KeyError
Why an account key could not be used for signing.
ResourceType
Which kind of resource a request addresses.

Constants§

DEFAULT_API_VERSION
The x-ms-version to send alongside a signature computed here.

Functions§

container_link
The resource link for a container: dbs/{database}/colls/{container}.
database_link
The resource link for a database: dbs/{database}.
date
The date format Cosmos wants: lowercase RFC 1123 in GMT.
document_link
The resource link for one document.
document_url
The URL for one document.
documents_url
The URL for a container’s document collection.
now
date for the current moment.
string_to_sign
The five-line string a Cosmos signature covers.