Yancy/Plugin/Auth/Github.pm

filter: Statementstmt 28.0% 23 / 82 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 25.0% 4 / 16 Podpod 80.0% 4 / 5 totaltotal 22.4% 31 / 138 CC 17 SCAR 30.1
CC
17
Cov
21%
init
42.0
__ANON__
42.0
_get_user
20.0
CRAP 20.2
« Yancy/Plugin/Auth/OAuth2.pm ↑ Summary Yancy/Command/backend/copy.pm »
1 package Yancy::Plugin::Auth::Github;
2 2 our $VERSION = '1.078';
3 # ABSTRACT: Authenticate using Github's OAuth2 provider
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use Mojolicious::Lite;
8 #pod plugin Yancy => {
9 #pod backend => 'sqlite://myapp.db',
10 #pod };
11 #pod app->yancy->plugin( 'Auth::Github' => {
12 #pod client_id => 'CLIENT_ID',
13 #pod client_secret => $ENV{ OAUTH_GITHUB_SECRET },
14 #pod schema => 'users',
15 #pod username_field => 'username',
16 #pod # TODO: Get other user information from Github, requesting
17 #pod # scopes if necessary
18 #pod } );
19 #pod
20 #pod =head1 DESCRIPTION
21 #pod
22 #pod This module allows authenticating using the Github OAuth2 API.
23 #pod
24 #pod This module extends the L<Yancy::Auth::Plugin::OAuth2> module to add
25 #pod Github features.
26 #pod
27 #pod This module composes the L<Yancy::Auth::Plugin::Role::RequireUser> role
28 #pod to provide the
29 #pod L<require_user|Yancy::Auth::Plugin::Role::RequireUser/require_user>
30 #pod authorization method.
31 #pod
32 #pod =head1 CONFIGURATION
33 #pod
34 #pod This plugin has the following configuration options.
35 #pod
36 #pod =head2 client_id
37 #pod
38 #pod The client ID, provided by Github.
39 #pod
40 #pod =head2 client_secret
41 #pod
42 #pod The client secret, provided by Github.
43 #pod
44 #pod =head2 login_label
45 #pod
46 #pod The label for the button to log in using Github. Defaults
47 #pod to C<Login with Github>.
48 #pod
49 #pod =head2 Sessions
50 #pod
51 #pod This module uses L<Mojolicious
52 #pod sessions|https://mojolicious.org/perldoc/Mojolicious/Controller#session>
53 #pod to store the login information in a secure, signed cookie.
54 #pod
55 #pod To configure the default expiration of a session, use
56 #pod L<Mojolicious::Sessions
57 #pod default_expiration|https://mojolicious.org/perldoc/Mojolicious/Sessions#default_expiration>.
58 #pod
59 #pod use Mojolicious::Lite;
60 #pod # Expire a session after 1 day of inactivity
61 #pod app->sessions->default_expiration( 24 * 60 * 60 );
62 #pod
63 #pod =head1 HELPERS
64 #pod
65 #pod This plugin inherits all helpers from L<Yancy::Plugin::Auth::OAuth2>.
66 #pod
67 #pod =head1 TEMPLATES
68 #pod
69 #pod To override these templates, add your own at the designated path inside
70 #pod your app's C<templates/> directory.
71 #pod
72 #pod =head2 yancy/auth/github/login_form.html.ep
73 #pod
74 #pod Display the button to log in using Github.
75 #pod
76 #pod =head2 layouts/yancy/auth.html.ep
77 #pod
78 #pod The layout that Yancy uses when displaying the login form, the
79 #pod unauthorized error message, and other auth-related pages.
80 #pod
81 #pod =head1 SEE ALSO
82 #pod
83 #pod L<Yancy::Plugin::Auth>, L<Yancy::Plugin::Auth::OAuth2>
84 #pod
85 #pod =cut
86  
87 2 ▶use Mojo::Base 'Yancy::Plugin::Auth::OAuth2';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Yancy::Util qw( currym match derp );
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 2 has moniker => 'github';
93 0 ▶has authorize_url => sub { Mojo::URL->new( 'https://github.com/login/oauth/authorize' ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
94 0 ▶has token_url => sub { Mojo::URL->new( 'https://github.com/login/oauth/access_token' ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
95 0 ▶has api_url => sub { Mojo::URL->new( 'https://api.github.com/' ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
96 2 has schema =>;
97 2 has username_field => 'username';
98 2 has plugin_field =>;
99 2 has allow_register => 0;
100 2 has login_label => 'Login with Github';
101  
102 sub init {
103 0 ▶ my ( $self, $app, $config ) = @_;
Subroutine init: not called CC=6 SCAR=37.4
Pod undocumented
104 0 ▶ if ( $config->{collection} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"collection"}) 0 0 0
105 0 $self->schema( $config->{collection} );
106 0 derp "'collection' configuration in Auth::Github is now 'schema'. Please fix your configuration.\n";
107 }
108 0 for my $attr ( qw( schema username_field plugin_field allow_register ) ) {
109 0 ▶ next if !$config->{ $attr };
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$attr} 0 0 0
110 0 $self->$attr( $config->{ $attr } );
111 }
112 0 for my $url_attr ( qw( api_url ) ) {
113 0 ▶ next if !$config->{ $url_attr };
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$url_attr} 0 0 0
114 0 $self->$url_attr( Mojo::URL->new( $config->{ $url_attr } ) );
115 }
116 0 return $self->SUPER::init( $app, $config );
117 }
118  
119 #pod =method current_user
120 #pod
121 #pod Returns the user row of the currently-logged-in user.
122 #pod
123 #pod =cut
124  
125 sub current_user {
126 0 ▶ my ( $self, $c ) = @_;
Subroutine current_user: not called CC=2 SCAR=17.9
Pod documented
127 0 ▶ my $username = $c->session->{yancy}{ $self->moniker }{ github_login } || return undef;
Condition0%0/2
$c->session->{'yancy'}{$self->moniker}{'github_login'} || (return undef)
l!l
00
MC/DC0%0/1
$c->session->{'yancy'}{$self->moniker}{'github_login'} || (return undef)
$c->session->{'yancy'}{$self->moniker}{'github_login'}
Truth table0%0/2
A
A= $c->session->{'yancy'}{$self->moniker}{'github_login'}
Aresult
11
00
128 0 return $self->_get_user( $c, $username );
129 }
130  
131 #pod =method login_form
132 #pod
133 #pod Get a link to log in using Github.
134 #pod
135 #pod =cut
136  
137 sub login_form {
138 0 ▶ my ( $self, $c ) = @_;
Subroutine login_form: not called CC=1 SCAR=6.9
Pod documented
139 0 return $c->render_to_string(
140 'yancy/auth/github/login_form',
141 label => $self->login_label,
142 url => $self->route->render,
143 );
144 }
145  
146 sub _get_user {
147 0 ▶ my ( $self, $c, $username ) = @_;
Subroutine _get_user: not called CC=4 SCAR=30.0
148 0 my $schema_name = $self->schema;
149 0 my $schema = $c->yancy->schema( $schema_name );
150 0 my $username_field = $self->username_field;
151 0 my %search;
152 0 ▶ if ( my $field = $self->plugin_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = $self->plugin_field) 0 0 0
153 0 $search{ $field } = $self->moniker;
154 }
155 0 ▶ if ( $username_field && $username_field ne $schema->{'x-id-field'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($username_field and $username_field ne $schema->{"x-id-field"}) 0 0 0
Condition0%0/3
$username_field and $username_field ne $schema->{"x-id-field"}
!ll&&!rl&&r
000
MC/DC0%0/2
$username_field and $username_field ne $schema->{"x-id-field"}
$username_field $username_field ne $schema->{"x-id-field"}
Truth table0%0/3
A and B
A= $username_fieldB= $username_field ne $schema->{"x-id-field"}
ABresult
0X0
100
111
156 0 $search{ $username_field } = $username;
157 0 my ( $user ) = @{ $c->yancy->backend->list( $schema_name, \%search, { limit => 1 } )->{items} };
158 0 return $user;
159 }
160 0 return $c->yancy->backend->get( $schema_name, $username );
161 }
162  
163 sub _handle_auth {
164 0 ▶ my ( $self, $c ) = @_;
Subroutine _handle_auth: not called CC=3 SCAR=24.8
165  
166 # Verify the CSRF from Github
167 0 ▶ if ( my $code = $c->param( 'code' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $c->param("code")) 0 0 0
168 0 ▶ if ( $c->param( 'state' ) ne $c->csrf_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->param("state") ne $c->csrf_token) 0 0 0
169 0 $c->render( status => 400, text => 'CSRF token failure' );
170 }
171 }
172  
173 0 return $self->SUPER::_handle_auth( $c );
174 }
175  
176 sub get_authorize_url {
177 0 ▶ my ( $self, $c ) = @_;
Subroutine get_authorize_url: not called CC=1 SCAR=6.9
Pod documented
178 0 my %client_info = (
179 client_id => $self->client_id,
180 state => $c->csrf_token,
181 );
182 0 return $self->authorize_url->clone->query( \%client_info );
183 }
184  
185 sub handle_token_p {
186 0 ▶ my ( $self, $c, $token ) = @_;
Subroutine handle_token_p: not called CC=1 SCAR=6.9
Pod documented
187 0 my $api_url = $self->api_url->clone;
188 0 $api_url->path->trailing_slash( '1' )->merge( 'user' );
189 return $self->ua->get_p(
190 $api_url,
191 { Authorization => join( ' ', 'token', $token ) },
192 )
193 ->then( sub {
194 0 ▶ my ( $tx ) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
195 0 my $login = $tx->res->json( '/login' );
196 0 ▶ if ( !$login ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($login) 0 0 0
197 0 $c->render( text => 'Error getting Github login: ' . $tx->res->body );
198 }
199 0 $c->session->{yancy}{ $self->moniker }{ github_login } = $login;
200 0 ▶ if ( !$self->_get_user( $c, $login ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_get_user($c, $login)) 0 0 0
201 0 ▶ if ( !$self->allow_register ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->allow_register) 0 0 0
202 0 $c->app->log->error( 'Registration not allowed (set allow_register)' );
203 0 $c->stash( status => 403 );
204 0 die 'Registration of new users is not allowed';
205 0 return;
206 }
207 0 my $schema = $c->yancy->schema( $self->schema );
208 $c->yancy->create(
209 $self->schema,
210 0 ▶ { $self->username_field || $schema->{'x-id-field'} || 'id' => $login },
Condition0%0/5
$self->username_field || $schema->{'x-id-field'} || 'id'
l!l
00
$self->username_field or $schema->{"x-id-field"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->username_field || $schema->{'x-id-field'} || 'id'
$self->username_field $schema->{"x-id-field"}
Truth table0%0/3
A or B
A= $self->username_fieldB= $schema->{"x-id-field"}
ABresult
1X1
011
000
211 );
212 }
213 } )
214 ->catch( sub {
215 0 ▶ my ( $err ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 0 $c->render( text => $err );
217 0 } );
218 }
219  
220 2 1;
221  
222 __END__
« Yancy/Plugin/Auth/OAuth2.pm ↑ Summary Yancy/Command/backend/copy.pm »