Requests_oauth.OauthRFC 6749 OAuth 2.0 Authorization Framework.
This module implements the OAuth 2.0 authorization framework as specified in RFC 6749.
The Implicit Grant (Section 4.2) is intentionally not supported as it is deprecated per RFC 8996.
This module supports Proof Key for Code Exchange (PKCE) per RFC 7636 to protect against authorization code interception attacks, especially for public clients.
(* Client credentials grant *)
let config = Oauth.make_config
~client_id:"my-client"
~client_secret:"my-secret"
~token_endpoint:"https://auth.example.com/token"
() in
match Oauth.client_credentials session config with
| Ok token -> Printf.printf "Got token: %s\n" (Oauth.get_access_token token)
| Error e -> Printf.printf "Error: %a\n" Oauth.pp_error e
(* Authorization code flow with PKCE *)
let pkce = Oauth.generate_pkce () in
let state = Oauth.generate_state () in
let auth_url = Oauth.authorization_url ~config ~state ~pkce () in
(* ... redirect user to auth_url, receive code ... *)
match Oauth.exchange_code session config ~code ~pkce_verifier:pkce.verifier () with
| Ok token -> ...
| Error e -> ...type config = {client_id : string;client_secret : string option;token_endpoint : string;redirect_uri : string option;scopes : string list;}OAuth 2.0 client configuration.
Per RFC 6749 Section 2, clients are identified by a client ID and optionally authenticated with a client secret.
val make_config :
client_id:string ->
?client_secret:string ->
token_endpoint:string ->
?authorization_endpoint:string ->
?redirect_uri:string ->
?scopes:string list ->
unit ->
configmake_config ~client_id ~token_endpoint ... creates an OAuth client configuration.
type token = {access_token : string;The access token issued by the authorization server.
*)token_type : string;The type of the token, typically "Bearer".
*)expires_at : Ptime.t option;When the token expires. None if no expiry was provided.
refresh_token : string option;The refresh token for obtaining new access tokens.
*)scope : string option;The scope of the access token.
*)}Token response from the authorization server. Per Section 5.1.
val get_access_token : token -> stringget_access_token token returns the access token string.
val get_refresh_token : token -> string optionget_refresh_token token returns the refresh token if present.
val is_expired : token -> boolis_expired token returns true if the token has expired. Returns false if the token has no expiry information.
val expires_within : Ptime.Span.t -> token -> boolexpires_within span token returns true if the token expires within span. Returns false if the token has no expiry information.
OAuth error response.
val pp_error : Format.formatter -> error -> unitPretty printer for OAuth errors.
val error_code_to_string : error_code -> stringerror_code_to_string code returns the RFC 6749 string representation.
Per RFC 7636.
type pkce = {verifier : string;The code verifier (43-128 URL-safe characters).
*)challenge : string;The code challenge derived from the verifier.
*)method_ : pkce_method;The challenge derivation method.
*)}PKCE state for authorization code flow.
val generate_pkce : ?method_:pkce_method -> unit -> pkcegenerate_pkce () generates PKCE verifier and challenge. Default method is S256.
val pkce_method_to_string : pkce_method -> stringReturns "plain" or "S256".
generate_state () generates a cryptographically random state value for CSRF protection per RFC 6749 Section 10.12.
validate_state ~expected ~received performs constant-time comparison.
val authorization_url :
config:config ->
state:string ->
?pkce:pkce ->
?extra_params:(string * string) list ->
unit ->
stringauthorization_url ~config ~state () builds the authorization URL.
These functions use a Requests.t session to make HTTP calls.
val client_credentials : Requests.t -> config -> (token, error) resultclient_credentials session config performs the client credentials grant. Per Section 4.4.
val password_grant :
Requests.t ->
config ->
username:string ->
password:string ->
(token, error) resultpassword_grant session config ~username ~password performs the resource owner password credentials grant.
Per Section 4.3.
Warning: This grant type should only be used for legacy or high-trust scenarios.
val exchange_code :
Requests.t ->
config ->
code:string ->
?pkce_verifier:string ->
unit ->
(token, error) resultexchange_code session config ~code () exchanges an authorization code for tokens. Per Section 4.1.3.
val refresh :
Requests.t ->
config ->
refresh_token:string ->
(token, error) resultrefresh session config ~refresh_token exchanges a refresh token for a new access token. Per Section 6.
Thread-safe automatic token refresh.
val create :
Requests.t ->
config ->
token ->
?on_refresh:(token -> unit) ->
unit ->
tcreate session config token () creates managed OAuth state.
val get_access_token_managed : t -> stringget_access_token_managed t returns the current access token, refreshing if needed.