diff options
author | gregor herrmann <gregoa@debian.org> | 2022-02-13 00:19:29 +0100 |
---|---|---|
committer | gregor herrmann <gregoa@debian.org> | 2022-02-13 00:19:29 +0100 |
commit | 3133ded601c998f12e820554ce7aac03bb313476 (patch) | |
tree | bbcebd0f57e32c81f897b7a49561aabeff8e415b | |
parent | 58cac96b3ef5c40401d69bde73c5d5a601f2a464 (diff) | |
parent | e5ca9d9d815c2bac03389219760d9ff03744babf (diff) |
New upstream version 2.02
-rw-r--r-- | .pls_cache/index | bin | 8316 -> 8316 bytes | |||
-rw-r--r-- | Changes | 5 | ||||
-rw-r--r-- | META.json | 2 | ||||
-rw-r--r-- | META.yml | 2 | ||||
-rw-r--r-- | README.md | 60 | ||||
-rw-r--r-- | lib/Mojolicious/Plugin/OAuth2.pm | 79 | ||||
-rw-r--r-- | t/Helper.pm | 17 | ||||
-rw-r--r-- | t/delayed.t | 2 | ||||
-rw-r--r-- | t/error.t | 18 |
9 files changed, 128 insertions, 57 deletions
diff --git a/.pls_cache/index b/.pls_cache/index Binary files differindex 2962189..255ce21 100644 --- a/.pls_cache/index +++ b/.pls_cache/index @@ -1,5 +1,10 @@ Revision history for perl distribution Mojolicious-Plugin-OAuth2 +2.02 2022-02-08T18:49:21+0900 + - Add support for passing in custom "ua" + - Add support for "providers" key in plugin config + - Add support for "proxy" 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 @@ -57,7 +57,7 @@ "web" : "https://github.com/marcusramberg/Mojolicious-Plugin-OAuth2" } }, - "version" : "2.01", + "version" : "2.02", "x_contributors" : [ "Marcus Ramberg <mramberg@cpan.org>", "Jan Henning Thorsen <jhthorsen@cpan.org>" @@ -28,7 +28,7 @@ resources: bugtracker: https://github.com/marcusramberg/Mojolicious-Plugin-OAuth2/issues homepage: https://github.com/marcusramberg/Mojolicious-Plugin-OAuth2 repository: https://github.com/marcusramberg/Mojolicious-Plugin-OAuth2.git -version: '2.01' +version: '2.02' x_contributors: - 'Marcus Ramberg <mramberg@cpan.org>' - 'Jan Henning Thorsen <jhthorsen@cpan.org>' @@ -9,9 +9,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}, + }, }, }; @@ -213,24 +215,48 @@ Holds a hash of provider information. See ["oauth2.providers"](#oauth2-providers ## register $app->plugin(OAuth2 => \%provider_config); + $app->plugin(OAuth2 => {providers => \%provider_config, proxy => 1, ua => Mojo::UserAgent->new}); Will register this plugin in your application with a given `%provider_config`. The keys in `%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 `%provider_config`, it is possible to pass in a +more complex config, with these keys: + +- providers + + The `%provider_config` must be present under this key. + +- proxy + + Setting this to a true value will automatically detect proxy settings using + ["detect" in Mojo::UserAgent::Proxy](https://metacpan.org/pod/Mojo%3A%3AUserAgent%3A%3AProxy#detect). + +- ua + + A custom [Mojo::UserAgent](https://metacpan.org/pod/Mojo%3A%3AUserAgent), in case you want to change proxy settings, + timeouts or other attributes. + +Instead of just passing in `%provider_config`, it is possible to pass in a +hash-ref "providers" (`%provider_config`) and "ua" (a custom +[Mojo::UserAgent](https://metacpan.org/pod/Mojo%3A%3AUserAgent) 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', + }, }, }); @@ -238,10 +264,12 @@ For [OpenID Connect](https://openid.net/connect/), `authorize_url` and `token_ur `well_known_url` so these are replaced by the `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/lib/Mojolicious/Plugin/OAuth2.pm b/lib/Mojolicious/Plugin/OAuth2.pm index bab484f..0312a5e 100644 --- a/lib/Mojolicious/Plugin/OAuth2.pm +++ b/lib/Mojolicious/Plugin/OAuth2.pm @@ -9,7 +9,7 @@ use Mojo::UserAgent; use constant MOJO_JWT => eval 'use Mojo::JWT 0.09; use Crypt::OpenSSL::RSA; use Crypt::OpenSSL::Bignum; 1'; our @CARP_NOT = qw(Mojolicious::Plugin::OAuth2 Mojolicious::Renderer); -our $VERSION = '2.01'; +our $VERSION = '2.02'; has providers => sub { return { @@ -50,7 +50,15 @@ 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}; + $self->_ua->proxy->detect if $config->{proxy}; + } + 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 +67,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 +248,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 +471,52 @@ Holds a hash of provider information. See L</oauth2.providers>. =head2 register $app->plugin(OAuth2 => \%provider_config); + $app->plugin(OAuth2 => {providers => \%provider_config, proxy => 1, 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 * proxy + +Setting this to a true value will automatically detect proxy settings using +L<Mojo::UserAgent::Proxy/detect>. + +=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 +524,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 { @@ -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); + }); } ); |