line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Mojolicious::Plugin::MozPersona; |
3
|
|
|
|
|
|
|
{ |
4
|
|
|
|
|
|
|
$Mojolicious::Plugin::MozPersona::VERSION = '0.04'; |
5
|
|
|
|
|
|
|
} |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# ABSTRACT: Minimalistic integration of Mozillas "Persona" authentication system in Mojolicious apps |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
20327
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
10
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
814
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
10072
|
|
|
1
|
|
|
|
|
9
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
1433
|
use File::Basename 'dirname'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
95
|
|
16
|
1
|
|
|
1
|
|
785
|
use File::Spec::Functions 'catdir'; |
|
1
|
|
|
|
|
836
|
|
|
1
|
|
|
|
|
66
|
|
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
615
|
use Mojolicious::Plugin::MozPersona::Controller; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
20
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my %defaults = ( |
22
|
|
|
|
|
|
|
audience => '', |
23
|
|
|
|
|
|
|
siteName => '', |
24
|
|
|
|
|
|
|
service => 'https://verifier.login.persona.org/verify', |
25
|
|
|
|
|
|
|
namespace => 'Mojolicious::Plugin::MozPersona::Controller', |
26
|
|
|
|
|
|
|
signinId => 'personaSignin', |
27
|
|
|
|
|
|
|
signinPath => '/_persona/signin', |
28
|
|
|
|
|
|
|
signoutId => 'personaSignout', |
29
|
|
|
|
|
|
|
signoutPath => '/_persona/signout', |
30
|
|
|
|
|
|
|
autoHook => { css => 0, jquery => 1, persona => 1, local => 1, uid => 1 }, |
31
|
|
|
|
|
|
|
localJsPath => '/_persona/localjs', |
32
|
|
|
|
|
|
|
localJsTpl => '_persona/local_js.txt.ep', |
33
|
|
|
|
|
|
|
personaJsPath => 'https://login.persona.org/include.js', |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub register { |
38
|
0
|
|
|
0
|
1
|
|
my ( $self, $app ) = @_; |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
my $defaultHooks = delete $defaults{'autoHook'}; |
41
|
0
|
0
|
|
|
|
|
my (%conf) = ( %defaults, %{ $_[2] || {} } ); |
|
0
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
$conf{'autoHook'} = {} unless exists $conf{'autoHook'}; |
44
|
0
|
|
|
|
|
|
foreach my $h ( keys %{$defaultHooks} ) { |
|
0
|
|
|
|
|
|
|
45
|
0
|
0
|
0
|
|
|
|
if ( ref $conf{'autoHook'} && ! exists $conf{'autoHook'}->{$h} ) { |
46
|
0
|
|
|
|
|
|
$conf{'autoHook'}->{$h} = $defaultHooks->{$h}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
$conf{'siteName'} =~ tr/"'//d; |
51
|
0
|
|
|
|
|
|
$conf{'signinPath'} =~ tr/"'//d; |
52
|
0
|
|
|
|
|
|
$conf{'signoutPath'} =~ tr/"'//d; |
53
|
0
|
|
|
|
|
|
$conf{'signinId'} =~ tr/"'#//d; |
54
|
0
|
|
|
|
|
|
$conf{'signoutId'} =~ tr/"'#//d; |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
die "Missing required configuration parameter: 'audience'!" unless $conf{'audience'}; |
57
|
0
|
0
|
|
|
|
|
die "Missing required configuration parameter: 'siteName'!" unless $conf{'siteName'}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Append "templates" and "public" directories |
60
|
0
|
|
|
|
|
|
my $base = catdir( dirname(__FILE__), 'MozPersona' ); |
61
|
0
|
|
|
|
|
|
push @{ $app->renderer->paths }, catdir( $base, 'templates' ); |
|
0
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
push @{ $app->static->paths }, catdir( $base, 'public' ); |
|
0
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
push @{ $app->renderer->classes }, __PACKAGE__; |
|
0
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
push @{ $app->static->classes }, __PACKAGE__; |
|
0
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
$app->routes->route( $conf{signinPath} )->via('POST')->to( |
68
|
|
|
|
|
|
|
namespace => $conf{namespace}, |
69
|
|
|
|
|
|
|
action => 'signin', |
70
|
|
|
|
|
|
|
_persona_audience => $conf{audience}, |
71
|
|
|
|
|
|
|
_persona_service => $conf{service}, |
72
|
|
|
|
|
|
|
); |
73
|
0
|
|
|
|
|
|
$app->routes->route( $conf{signoutPath} )->via('POST')->to( |
74
|
|
|
|
|
|
|
namespace => $conf{namespace}, |
75
|
|
|
|
|
|
|
action => 'signout', |
76
|
|
|
|
|
|
|
_persona_audience => $conf{audience}, |
77
|
|
|
|
|
|
|
_persona_service => $conf{service}, |
78
|
|
|
|
|
|
|
); |
79
|
0
|
|
|
|
|
|
$app->routes->route( $conf{localJsPath} )->via('GET')->to( |
80
|
|
|
|
|
|
|
namespace => $conf{namespace}, |
81
|
|
|
|
|
|
|
action => 'js', |
82
|
|
|
|
|
|
|
_persona_conf => \%conf, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
if ( $conf{'autoHook'} ) { |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $head_block = ''; |
88
|
0
|
0
|
|
|
|
|
if ( $conf{'autoHook'}->{'css'} ) { |
89
|
0
|
|
|
|
|
|
$head_block .= ''; |
90
|
|
|
|
|
|
|
} |
91
|
0
|
0
|
|
|
|
|
if ( $conf{'autoHook'}->{'jquery'} ) { |
92
|
0
|
|
|
|
|
|
$head_block .= ''; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my $end_block = ''; |
96
|
0
|
0
|
|
|
|
|
if ( $conf{'autoHook'}->{'persona'} ) { |
97
|
0
|
|
|
|
|
|
$end_block .= qq||; |
98
|
|
|
|
|
|
|
} |
99
|
0
|
0
|
|
|
|
|
if ( $conf{'autoHook'}->{'local'} ) { |
100
|
0
|
|
|
|
|
|
$end_block .= qq||; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
$app->hook( |
104
|
|
|
|
|
|
|
after_dispatch => sub { |
105
|
0
|
|
|
0
|
|
|
my ($c) = @_; |
106
|
0
|
0
|
|
|
|
|
return unless index( $c->res->headers->content_type, 'html' ) >= 0; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
my $body = $c->res->body; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ( $conf{'autoHook'}->{'uid'} ) { |
|
|
0
|
|
|
|
|
|
111
|
0
|
0
|
0
|
|
|
|
if ( defined( $c->session('_persona') ) |
112
|
|
|
|
|
|
|
&& $c->session('_persona')->{'status'} eq 'okay' |
113
|
|
|
|
|
|
|
) |
114
|
|
|
|
|
|
|
{ |
115
|
0
|
|
|
|
|
|
my $email = $c->session('_persona')->{'email'}; |
116
|
0
|
|
|
|
|
|
$body |
117
|
|
|
|
|
|
|
=~ s!!$head_block!o; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else { |
120
|
0
|
|
|
|
|
|
$body |
121
|
|
|
|
|
|
|
=~ s!!$head_block!o; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
elsif ($head_block) { |
125
|
0
|
|
|
|
|
|
$body =~ s!!$head_block!o; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
if ($end_block) { |
129
|
0
|
|
|
|
|
|
$body =~ s!!$end_block!o; |
130
|
|
|
|
|
|
|
} |
131
|
0
|
|
|
|
|
|
$c->res->body($body); |
132
|
|
|
|
|
|
|
} |
133
|
0
|
|
|
|
|
|
); |
134
|
|
|
|
|
|
|
} ## end if ( $conf{'autoHook'}) |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
return; |
137
|
|
|
|
|
|
|
} ## end sub register |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
__END__ |