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