NAME

Mojo::OAuth2 - A safe and simple OAuth2 implementation.

VERSION

version 0.001

SYNOPSIS

my $metadata = Mojo::OAuth2::Metadata->new_from_discovery($issuer);
my $client = $metadata->create_client(client_id => 'me');

my ($url, $ticket) = $client->create_authorization_request(%args);

... # Redirect to $url and receive a response

my $authorization = $client->accept_authorization_response($response, $ticket);
# delete ticket from store
$authorization->throw if not $authorization->is_success;
my $tokens = $authorization->get_tokens;
my $user_agent = $tokens->ua('https://example.com');
my $new_tokens = $tokens->refresh;

DESCRIPTION

This is a module implements an OAuth2 client. It allows for both synchronous and asynchronous usage.

ATTRIBUTES

client_id

This returns the client identifier.

metadata

This returns the metadata.

METHODS

new

Mojo::OAuth2->new(metadata => )

This creates a new OAuth2 client.

metadata

This must be an instance of Mojo::OAuth2::Metadata, or of a subclass thereof (such as Mojo::OIDC::Metadata). Mandatory.

client_id

This is the client identifier as agreed with the authorization server. Mandatory.

client_secret

The client secret, as agreed with the authorization server. Optional.

client_auth_method

The client authentication method used. This must be one of the following values:

  • client_secret_basic

    Use a basic authentication header to pass the client ID and secret.

  • client_secret_post

    Use the body to pass the client ID and secret.

  • none

    Use the body to pass the client ID, but no secret.

ua

The user agent to be used for fetching tokens.

create_authorization_request

my ($url, $ticket) = $client->create_authorization_request(
    redirect_uri => $redirect_uri,
    scope        => 'email',
);

This creates a new authorization request, it takes the following named arguments:

redirect_uri

The redirection URI, used to receive a response the response from the authorization server. This should be able receive the response according to the value of the response_mode parameter.

response_mode

The desired response mode. If the value is query (the default), the redirect_uri will receive the response as query parameters to a GET request. If the value is post_form, the response will be received as a POST form. Server support can be checked with $metadata-response_modes_supported>.

scope

The scope of the authorization request, as space separated words. On most servers, this is mandatory.

It returns two values. The first is the URL that the user should be redirected to. The second is the ticket containing information needed later to process the response. This ticket MUST be stored securely, preferably server-side though it can be stored client-side if encrypted (e.g. if you use encrypted cookies).

accept_authorization_response

my $authorization = $client->accept_authorization_response($parameters, $ticket);

This receives a response and tries to return an authorization. It will throw an exception on invalid input. On a successful return, one MUST ensure the ticket will not be reused.

The return value from the server will be a Mojo::OAuth2::Authorization on success or a Mojo::OAuth2::Error on authorization failure, this can be checked

get_tokens

my $token = $client->get_tokens($authorization->payload);

Fetch a token from the token endpoint. This is usually used via $authorization-get_tokens>.

get_tokens_p

This is like get_token, but it returns a promise instead.

SECURITY

The authorization flow logic's security is depends on the ticket being secret and unmalleable. One must take care to store it carefully.

SEE ALSO

Mojo::OIDC.

AUTHOR

Leon Timmermans <fawaka@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Leon Timmermans.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.