OneDrive
OneDrive and Microsoft Graph drive requests authenticated with a Microsoft Entra access token.
Inserts a sensitive Authorization: Bearer header; expires_in is rejected because this crate implements header authentication only.
Get started
cargo add reqsign --features onedrive
OneDrive requests go to Microsoft Graph with a Microsoft Entra access token in
an Authorization: Bearer header. Reqsign signs the request head; the caller
builds the Graph request, for example
GET https://graph.microsoft.com/v1.0/me/drive/root.
use reqsign::{default_context, onedrive::default_signer};
use reqsign::onedrive::{DefaultCredentialProvider, StaticCredentialProvider};
let ctx = default_context();
let provider = DefaultCredentialProvider::new()
.push_front(StaticCredentialProvider::new("graph-access-token"));
let signer = default_signer(ctx).with_credential_provider(provider);
let mut req = http::Request::builder()
.method(http::Method::GET)
.uri("https://graph.microsoft.com/v1.0/me/drive")
.body(())?
.into_parts()
.0;
// Sign in place, then send with the HTTP client you already use.
signer.sign(&mut req, None).await?;
Microsoft documents no environment variable or shared configuration file that
carries a Microsoft Graph user token, and MSAL token caches are
library-specific. default_signer(ctx) therefore composes a default provider
whose automatic chain is empty: it does not run the Azure CLI, read MSAL
caches, or start a login flow. Configure one source before signing:
- the documented refresh exchange, composed with
DefaultCredentialProvider::new().push_front(RefreshCredentialProvider::new(..)?) - a static access token or a custom provider, also composed with
push_front; the provider pushed last is tried first
Supplying a provider directly to Signer::new or
Signer::with_credential_provider is equivalent when you do not need the
default chain.
Refresh tokens
The authorization request must include offline_access to receive a refresh
token, plus the Microsoft Graph scope your access needs. Files.ReadWrite is
the least privileged delegated permission that lets the app read, create,
update, and delete the signed-in user's files, and Files.Read is the read-only
alternative. Reqsign refreshes through
https://login.microsoftonline.com/common/oauth2/v2.0/token.
A public client — desktop, mobile, browserless, or client-side app — cannot store an application secret, so the exchange sends none:
use reqsign::{default_context, onedrive::default_signer};
use reqsign::onedrive::{DefaultCredentialProvider, RefreshCredentialProvider};
let provider = DefaultCredentialProvider::new()
.push_front(RefreshCredentialProvider::new("client-id", "refresh-token")?);
let signer = default_signer(default_context()).with_credential_provider(provider);
A confidential client — web app, web API, or daemon that keeps the secret on the server side — adds it to the same form-body exchange:
use reqsign::{default_context, onedrive::default_signer};
use reqsign::onedrive::{DefaultCredentialProvider, RefreshCredentialProvider};
let provider = DefaultCredentialProvider::new().push_front(
RefreshCredentialProvider::builder("client-id", "refresh-token")
.client_secret("client-secret")
.build()?,
);
let signer = default_signer(default_context()).with_credential_provider(provider);
Microsoft returns a new refresh token with every successful exchange and documents that the client must discard the previous one. A provider keeps only the latest refresh token in memory, clones share it, and exchanges are serialized, so concurrent signing never races two exchanges with the same token. Reqsign never writes refresh tokens to disk; a refresh token lost with the process must be obtained again through the authorization flow.
A Microsoft Graph token response carries expires_in. A successful exchange
that omits it, or whose token is already expired, is rejected rather than
cached as a token with an unknown lifetime; the returned refresh token from
that response is still committed for the next exchange.
Access-token caching and proactive refresh belong to Signer.
Tenant, scope, and endpoint
The default token endpoint is the global Microsoft Entra v2.0 endpoint for the
common tenant. Microsoft documents common, organizations, consumers, or
a tenant identifier such as a domain name or GUID; select one with .tenant:
use reqsign::onedrive::RefreshCredentialProvider;
let provider = RefreshCredentialProvider::builder("client-id", "refresh-token")
.tenant("contoso.onmicrosoft.com")
.build()?;
By default the exchange omits scope, which keeps the new access token limited
to what the original grant received. To repeat the scope explicitly, use
.scope("offline_access Files.ReadWrite"); Microsoft requires the value to be
equivalent to or a subset of the original grant. Requesting a broader scope
than the user consented to fails the exchange.
National clouds publish their own Microsoft Entra authentication endpoints, for
example https://login.microsoftonline.us for Microsoft Entra ID for US
Government and https://login.partner.microsoftonline.cn for Microsoft Entra
China. Replace the whole endpoint in those clouds; it must be HTTPS:
use reqsign::onedrive::RefreshCredentialProvider;
let provider = RefreshCredentialProvider::builder("client-id", "refresh-token")
.token_endpoint("https://login.microsoftonline.us/common/oauth2/v2.0/token")
.build()?;
Request authentication
OneDrive uses header authentication only. Signing adds a sensitive
Authorization: Bearer header and rejects expires_in, because this crate
does not implement query authentication; it never modifies the URI or other
headers. Business headers and parameters such as $select, If-Match, or the
Graph drive path stay with the caller.
On wasm32-unknown-unknown, this provider enables Jiff's JavaScript clock.
Runtime signing or refresh requires a wasm-bindgen JavaScript host such as a
browser or Node; a bare WASM runtime without a JavaScript clock is not
supported. CI exercises the public signing and refresh paths under
wasm-bindgen-test in Node.
Credentials
Default credential chain
This provider has no ambient default source. Supply an explicit credential provider as described on this page.
Credential providers
Available ProvideCredential implementations. Construct any of them directly, reorder them, or compose them into your own chain — see Loading credentials:
StaticCredentialProvider— Static Microsoft Graph access token with optional absolute expiryRefreshCredentialProvider— Microsoft Entra refresh-token exchange for a public or confidential client