From 4a5cb81df3be6411f82e68b0d3b82db842a0ef53 Mon Sep 17 00:00:00 2001 From: Jan Henning Thorsen Date: Tue, 8 Feb 2022 17:43:10 +0900 Subject: Add support for passing in custom "ua" --- Changes | 4 +++ lib/Mojolicious/Plugin/OAuth2.pm | 71 +++++++++++++++++++++++++++++----------- t/Helper.pm | 17 ++++++---- t/delayed.t | 2 +- t/error.t | 18 ++++------ 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. =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, 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 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, C and C so these are replaced by the C 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); + }); } ); -- cgit v1.2.3