summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Henning Thorsen <jhthorsen@cpan.org>2022-02-08 17:43:10 +0900
committerJan Henning Thorsen <jhthorsen@cpan.org>2022-02-08 18:01:09 +0900
commit4a5cb81df3be6411f82e68b0d3b82db842a0ef53 (patch)
tree87074353cb8ce9a4be0e0addd3b7c1d5bd5dd06f
parent3c7e34d08cd30f650b3b3a68b23e662d217ba7d4 (diff)
Add support for passing in custom "ua"
-rw-r--r--Changes4
-rw-r--r--lib/Mojolicious/Plugin/OAuth2.pm71
-rw-r--r--t/Helper.pm17
-rw-r--r--t/delayed.t2
-rw-r--r--t/error.t18
5 files changed, 74 insertions, 38 deletions
diff --git a/Changes b/Changes
index ce87af4..3899c08 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
Revision history for perl distribution Mojolicious-Plugin-OAuth2
+2.02 Not Released
+ - Add support for passing in custom "ua"
+ - Add support for "providers" key in plugin config
+
2.01 2021-10-28T18:29:45+0900
- Test suite is compatible with older versions of Mojolicious
- OpenID Connect require Mojo::JWT 0.09
diff --git a/lib/Mojolicious/Plugin/OAuth2.pm b/lib/Mojolicious/Plugin/OAuth2.pm
index 95b4db9..8aae532 100644
--- a/lib/Mojolicious/Plugin/OAuth2.pm
+++ b/lib/Mojolicious/Plugin/OAuth2.pm
@@ -50,7 +50,14 @@ has _ua => sub { Mojo::UserAgent->new };
sub register {
my ($self, $app, $config) = @_;
- my $providers = $self->providers;
+
+ if ($config->{providers}) {
+ $self->_config_to_providers($config->{providers});
+ $self->_ua($config->{ua}) if $config->{ua};
+ }
+ else {
+ $self->_config_to_providers($config);
+ }
$app->helper('oauth2.auth_url' => sub { $self->_call(_auth_url => @_) });
$app->helper('oauth2.get_refresh_token_p' => sub { $self->_call(_get_refresh_token_p => @_) });
@@ -59,8 +66,7 @@ sub register {
$app->helper('oauth2.logout_url' => sub { $self->_call(_logout_url => @_) });
$app->helper('oauth2.providers' => sub { $self->providers });
- $self->_config_to_providers($config);
- $self->_apply_mock($providers->{mocked}) if $providers->{mocked}{key};
+ $self->_apply_mock($self->providers->{mocked}) if $self->providers->{mocked}{key};
$self->_warmup_openid($app);
}
@@ -241,9 +247,11 @@ Mojolicious::Plugin::OAuth2 - Auth against OAuth2 APIs including OpenID Connect
use Mojolicious::Lite;
plugin OAuth2 => {
- facebook => {
- key => 'some-public-app-id',
- secret => $ENV{OAUTH2_FACEBOOK_SECRET},
+ providers => {
+ facebook => {
+ key => 'some-public-app-id',
+ secret => $ENV{OAUTH2_FACEBOOK_SECRET},
+ },
},
};
@@ -462,24 +470,47 @@ Holds a hash of provider information. See L</oauth2.providers>.
=head2 register
$app->plugin(OAuth2 => \%provider_config);
+ $app->plugin(OAuth2 => {providers => \%provider_config, ua => Mojo::UserAgent->new});
Will register this plugin in your application with a given C<%provider_config>.
The keys in C<%provider_config> are provider names and the values are
configuration for each provider. Note that the value will be merged with the
predefined providers below.
+Instead of just passing in C<%provider_config>, it is possible to pass in a
+more complex config, with these keys:
+
+=over 2
+
+=item * providers
+
+The C<%provider_config> must be present under this key.
+
+=item * ua
+
+A custom L<Mojo::UserAgent>, in case you want to change proxy settings,
+timeouts or other attributes.
+
+=back
+
+Instead of just passing in C<%provider_config>, it is possible to pass in a
+hash-ref "providers" (C<%provider_config>) and "ua" (a custom
+L<Mojo::UserAgent> object).
+
Here is an example to add adddition information like "key" and "secret":
$app->plugin(OAuth2 => {
- custom_provider => {
- key => 'APP_ID',
- secret => 'SECRET_KEY',
- authorize_url => 'https://provider.example.com/auth',
- token_url => 'https://provider.example.com/token',
- },
- github => {
- key => 'APP_ID',
- secret => 'SECRET_KEY',
+ providers => {
+ custom_provider => {
+ key => 'APP_ID',
+ secret => 'SECRET_KEY',
+ authorize_url => 'https://provider.example.com/auth',
+ token_url => 'https://provider.example.com/token',
+ },
+ github => {
+ key => 'APP_ID',
+ secret => 'SECRET_KEY',
+ },
},
});
@@ -487,10 +518,12 @@ For L<OpenID Connect|https://openid.net/connect/>, C<authorize_url> and C<token_
C<well_known_url> so these are replaced by the C<well_known_url> key.
$app->plugin(OAuth2 => {
- azure_ad => {
- key => 'APP_ID',
- secret => 'SECRET_KEY',
- well_known_url => 'https://login.microsoftonline.com/tenant-id/v2.0/.well-known/openid-configuration',
+ providers => {
+ azure_ad => {
+ key => 'APP_ID',
+ secret => 'SECRET_KEY',
+ well_known_url => 'https://login.microsoftonline.com/tenant-id/v2.0/.well-known/openid-configuration',
+ },
},
});
diff --git a/t/Helper.pm b/t/Helper.pm
index 086d9c6..0659b95 100644
--- a/t/Helper.pm
+++ b/t/Helper.pm
@@ -9,13 +9,16 @@ sub make_app {
$app->plugin(
OAuth2 => {
- test => {
- authorize_url => '/oauth/authorize',
- token_url => '/oauth/token',
- key => 'fake_key',
- secret => 'fake_secret',
- scope => 'a,b,c',
- }
+ ua => $app->ua,
+ providers => {
+ test => {
+ authorize_url => '/oauth/authorize',
+ token_url => '/oauth/token',
+ key => 'fake_key',
+ secret => 'fake_secret',
+ scope => 'a,b,c',
+ },
+ },
}
);
diff --git a/t/delayed.t b/t/delayed.t
index 9c35389..6df1934 100644
--- a/t/delayed.t
+++ b/t/delayed.t
@@ -4,7 +4,7 @@ use t::Helper;
my $app = t::Helper->make_app;
my $t = Test::Mojo->new($app);
-Mojo::Util::monkey_patch('Mojolicious::Plugin::OAuth2', _ua => sub { $t->ua });
+$t->app->ua->server($t->ua->server);
$app->routes->get(
'/oauth-delayed' => sub {
diff --git a/t/error.t b/t/error.t
index 59794ca..973da16 100644
--- a/t/error.t
+++ b/t/error.t
@@ -4,22 +4,18 @@ use t::Helper;
my $app = t::Helper->make_app;
my $t = Test::Mojo->new($app);
-Mojo::Util::monkey_patch('Mojolicious::Plugin::OAuth2', _ua => sub { $t->ua });
+$t->app->ua->server($t->ua->server);
$app->routes->get(
'/oauth-error' => sub {
my $c = shift;
- $c->oauth2->get_token_p('test')->then(
- sub {
- return unless my $provider_res = shift;
- return $c->render(text => "Token $provider_res->{access_token}");
- }
- )->catch(
- sub {
- return $c->render(text => shift, status => 500);
- }
- );
+ $c->oauth2->get_token_p('test')->then(sub {
+ return unless my $provider_res = shift;
+ return $c->render(text => "Token $provider_res->{access_token}");
+ })->catch(sub {
+ return $c->render(text => shift, status => 500);
+ });
}
);