OAuth:...bServer(3pm)

πŸ“¦
version 0.11 Β· 2 versions
, in debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ 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_uri is 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 server
  • redirect_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 list last_request twice in a row β€” once for the HTTP::Request and once for the HTTP::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 (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