File Coverage

blib/lib/Mojolicious/Plugin/Web/Auth.pm
Criterion Covered Total %
statement 9 35 25.7
branch 0 12 0.0
condition 0 11 0.0
subroutine 3 6 50.0
pod 1 1 100.0
total 13 65 20.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Web::Auth;
2              
3 1     1   68275 use strict;
  1         2  
  1         30  
4 1     1   7 use warnings;
  1         2  
  1         40  
5              
6             our $VERSION = '0.16';
7              
8 1     1   519 use Mojo::Base 'Mojolicious::Plugin';
  1         190607  
  1         6  
9              
10             sub register {
11 0     0 1   my ( $self, $app, $args ) = @_;
12 0 0         my $module = delete $args->{module} or die "Missing mandatory parameter: module";
13 0           my $klass = join '::', __PACKAGE__, 'Site', $module;
14 0 0         if ($Mojolicious::VERSION >= 5.81) {
15 0           Mojo::Loader::load_class($klass);
16             } else {
17 0           Mojo::Loader->load($klass);
18             }
19              
20 0           my $moniker = $klass->moniker();
21 0   0       my $authenticate_path = delete $args->{authenticate_path} || "/auth/${moniker}/authenticate";
22 0   0       my $callback_path = delete $args->{callback_path} || "/auth/${moniker}/callback";
23              
24             # handlers
25 0 0         my $on_finished = delete $args->{on_finished} or die "Missing mandatory parameter: on_finished";
26             my $on_error = delete $args->{on_error} || sub {
27 0     0     my ( $c, $err ) = @_;
28 0           die "Authentication error in $module: $err";
29 0   0       };
30              
31             # auth object
32 0           my $auth = $klass->new(%$args);
33              
34             $app->hook( before_dispatch => sub {
35 0     0     my $c = shift;
36 0           my $path = $c->req->url->path;
37 0 0         if ( $path->contains($authenticate_path) ) {
    0          
38 0           my $forwarded_proto = $c->req->headers->header('x-forwarded-proto');
39 0 0 0       $c->req->url->base->scheme('https') if (defined $forwarded_proto && $forwarded_proto eq 'https');
40 0           my $callback = $c->req->url->path($callback_path)->to_abs;
41 0           return $c->redirect_to( $auth->auth_uri( $c, $callback ) );
42             }
43             elsif ( $path->contains($callback_path) ) {
44             return $auth->callback( $c, +{
45             on_finished => sub {
46 0           $on_finished->($c, @_);
47             },
48             on_error => sub {
49 0           $on_error->($c, @_);
50             },
51 0           } );
52             }
53 0           } );
54              
55 0           return $self;
56             }
57              
58             1;
59              
60             __END__