Mojolicious-Plugin-OAuth2
view release on metacpan or search on metacpan
NAME
Mojolicious::Plugin::OAuth2 - Auth against OAuth2 APIs
DESCRIPTION
This Mojolicious plugin allows you to easily authenticate against a
OAuth2 <http://oauth.net> provider. It includes configurations for a few
popular providers, but you can add your own easily as well.
Note that OAuth2 requires https, so you need to have the optional
Mojolicious dependency required to support it. Run the command below to
check if IO::Socket::SSL is installed.
$ mojo version
References
* <http://oauth.net/documentation/>
* <http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified>
* <http://homakov.blogspot.jp/2013/03/oauth1-oauth2-oauth.html>
* <http://en.wikipedia.org/wiki/OAuth#OAuth_2.0>
SYNOPSIS
Example web application
use Mojolicious::Lite;
plugin "OAuth2" => {
facebook => {
key => "some-public-app-id",
secret => $ENV{OAUTH2_FACEBOOK_SECRET},
},
};
get "/connect" => sub {
my $c = shift;
$c->delay(
sub {
my $delay = shift;
my $args = {redirect_uri => $c->url_for('connect')->userinfo(undef)->to_abs};
$c->oauth2->get_token(facebook => $args, $delay->begin);
},
sub {
my ($delay, $err, $data) = @_;
return $c->render("connect", error => $err) unless $data->{access_token};
return $c->session(token => $c->redirect_to('profile'));
},
);
};
Example blocking web application
use Mojolicious::Lite;
plugin "OAuth2" => {
facebook => {
key => "some-public-app-id",
secret => $ENV{OAUTH2_FACEBOOK_SECRET},
},
};
get "/connect" => sub {
my $c = shift;
my $args = {redirect_uri => $c->url_for('connect')->userinfo(undef)->to_abs};
if (my $err = $c->param('error')) {
# do stuff with error from OAuth2 provider
} elsif (my $data = $c->oauth2->get_token(facebook => $args)) {
# do stuff with $data->{access_token};
} else {
# already redirected by OAuth2 plugin
return;
}
( run in 3.182 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )