OAuth:...bServer(3pm)
β¨ OAuth::Lite2::Client::WebServer β¨
okay bestie, come sit with me, because this one is literally the whole "log in with your account" pipeline in Perl and it's iconic π
It lives in the Perl docs as OAuth:...bServer(3pm), filed under User Contributed Perl Documentation, and it's the OAuth 2.0 Web Server Profile Client π
πΈ NAME πΈ
OAuth::Lite2::Client::WebServer β the OAuth 2.0 Web Server Profile Client. that's it, that's the name, no cap π¦
π SYNOPSIS π
here's the whole vibe in one go, bestie. this is the main character code block so imma give it to you exactly as it is:
my $client = OAuth::Lite2::Client::WebServer->new(
id => q{my_client_id},
secret => q{my_client_secret},
authorize_uri => q{http://example.org/authorize},
access_token_uri => q{http://example.org/token},
);
# redirect user to authorize page.
sub start_authorize {
my $your_app = shift;
my $redirect_url = $client->uri_to_redirect(
redirect_uri => q{http://yourapp/callback},
scope => q{photo},
state => q{optional_state},
);
$your_app->res->redirect( $redirect_url );
}
# this method corresponds to the url 'http://yourapp/callback'
sub callback {
my $your_app = shift;
my $code = $your_app->request->param("code");
my $access_token = $client->get_access_token(
code => $code,
redirect_uri => q{http://yourapp/callback},
) or return $your_app->error( $client->errstr );
$your_app->store->save( access_token => $access_token->access_token );
$your_app->store->save( expires_at => time() + $access_token->expires_in );
$your_app->store->save( refresh_token => $access_token->refresh_token );
}
sub refresh_access_token {
my $your_app = shift;
my $access_token = $client->refresh_access_token(
refresh_token => $refresh_token,
) or return $your_app->error( $client->errstr );
$your_app->store->save( access_token => $access_token->access_token );
$your_app->store->save( expires_at => time() + $access_token->expires_in );
$your_app->store->save( refresh_token => $access_token->refresh_token );
}
sub access_to_protected_resource {
my $your_app = shift;
my $access_token = $your_app->store->get("access_token");
my $expires_at = $your_app->store->get("expires_at");
my $refresh_token = $your_app->store->get("refresh_token");
unless ($access_token) {
$your_app->start_authorize();
return;
}
if ($expires_at < time()) {
$your_app->refresh_access_token();
return;
}
my $req = HTTP::Request->new( GET => q{http://example.org/photo} );
$req->header( Authorization => sprintf(q{OAuth %s}, $access_token) );
my $agent = LWP::UserAgent->new;
my $res = $agent->request($req);
...
}
slay. π«Ά
π DESCRIPTION π
it's the client library for the OAuth 2.0 Web Server Profile. literally that. it handles the whole handshake so you don't have to think about it, and honestly? obsessed. π
π METHODS π
π
new( %params )
the constructor, the moment, the beginning of everything β¨ here's what you can hand it:
idβ your Client ID πsecretβ your Client secret π (keep it secret, keep it safe, fr fr)authorize_uriβ the authorization page uri on the auth-server.access_token_uriβ the token endpoint uri on the auth-server.refresh_token_uriβ the refresh-token endpoint uri on the auth-server. if you omit this,access_token_uriis used instead. cute shortcut, love that for you πagentβ the user agent. if you omit this,LWP::UserAgent's object is set by default. You can use your custom agent or preset-agents. π
See also
OAuth::Lite2::Agent::Dump OAuth::Lite2::Agent::Strict OAuth::Lite2::Agent::PSGIMock
π¦ uri_to_redirect( %params )
(yep, that's the whole entry, bestie β it's just vibes and a signature over here πΈ)
β get_access_token( %params )
this one executes verification, and returns an OAuth::Lite2::Client::Token object. so that's your little token baby, fresh out of the oven π₯
params it takes:
codeβ Authorization-code that is issued beforehand by serverredirect_uriβ The URL that has used for user authorization's callback
π refresh_access_token( %params )
Refresh access token by refresh_token, returns OAuth::Lite2::Client::Token object. it's giving "new token, who dis" π₯
params:
refresh_token
π§ get_server_state
Obtain OAuth::Lite2::Client::ServerState object. boom, state acquired π«
π last_request
Returns a HTTP::Request object that is used when you obtain or refresh access token last time internally.
π last_request
Returns a HTTP::Response object that is used when you obtain or refresh access token last time internally.
okay bestie real talk, the docs literally listlast_requesttwice in a row β once for theHTTP::Requestand once for theHTTP::Response. I'm not touching it, I'm just reporting it, no cap π that's exactly what the page says, character for character.
π AUTHOR π
Ryo Ito, <ritou.06@gmail.com>
Lyo Kato, <lyo.kato@gmail.com>
thank you legends π«Ά
π COPYRIGHT AND LICENSE π
Copyright (C) 2010 by Lyo Kato
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
and that's the whole page, bestie π this was perl v5.36.0, dated 2023-07-01, straight from OAuth:...bServer(3pm) in the User Contributed Perl Documentation. go get those tokens, you absolute icon πβ¨π
π¦ where this page comes from
- debian trixie: liboauth-lite2-perl 0.11-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: liboauth-lite2-perl 0.11-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: liboauth-lite2-perl 0.11-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: liboauth-lite2-perl 0.11-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms