| 1 | package Yancy::Plugin::Auth::Github; | ||||||||||||||||||||||||||||
| 2 | 3 | our $VERSION = '1.088'; | |||||||||||||||||||||||||||
| 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 | 3 | ▶ | use Mojo::Base 'Yancy::Plugin::Auth::OAuth2'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | use Yancy::Util qw( currym match derp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 3 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 3 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | has moniker => 'github'; | |||||||||||||||||||||||||||
| 93 | 1 | ▶ | has authorize_url => sub { Mojo::URL->new( 'https://github.com/login/oauth/authorize' ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | has token_url => sub { Mojo::URL->new( 'https://github.com/login/oauth/access_token' ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | has api_url => sub { Mojo::URL->new( 'https://api.github.com/' ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 3 | has schema =>; | |||||||||||||||||||||||||||
| 97 | 3 | has username_field => 'username'; | |||||||||||||||||||||||||||
| 98 | 3 | has plugin_field =>; | |||||||||||||||||||||||||||
| 99 | 3 | has allow_register => 0; | |||||||||||||||||||||||||||
| 100 | 3 | has login_label => 'Login with Github'; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 3 | my ( $self, $app, $config ) = @_; | |||||||||||||||||||||||||||
| 104 | 3 | ▶ | if ( $config->{collection} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | $self->schema( $config->{collection} ); | |||||||||||||||||||||||||||
| 106 | 0 | derp "'collection' configuration in Auth::Github is now 'schema'. Please fix your configuration.\n"; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 3 | for my $attr ( qw( schema username_field plugin_field allow_register ) ) { | |||||||||||||||||||||||||||
| 109 | 12 | ▶ | next if !$config->{ $attr }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 8 | $self->$attr( $config->{ $attr } ); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 3 | for my $url_attr ( qw( api_url ) ) { | |||||||||||||||||||||||||||
| 113 | 3 | ▶ | next if !$config->{ $url_attr }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 2 | $self->$url_attr( Mojo::URL->new( $config->{ $url_attr } ) ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 3 | 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 | 3 | ▶ | sub current_user { | ||||||||||||||||||||||||||
|
Subroutine
current_user: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 127 | 3 | ▶ | my $username = $c->session->{yancy}{ $self->moniker }{ github_login } || return undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
$c->session->{'yancy'}{$self->moniker}{'github_login'} || (return undef)
MC/DC0%0/1
$c->session->{'yancy'}{$self->moniker}{'github_login'} || (return undef)
$c->session->{'yancy'}{$self->moniker}{'github_login'}
Truth table50%1/2
A
A= $c->session->{'yancy'}{$self->moniker}{'github_login'}
| |||||||||||||||||||||||||||||
| 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 | 3 | ▶ | sub login_form { | ||||||||||||||||||||||||||
|
Subroutine
login_form: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 139 | 3 | 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 | 3 | ▶ | sub _get_user { | ||||||||||||||||||||||||||
|
Subroutine
_get_user: called CC=4 SCAR=17.0
| |||||||||||||||||||||||||||||
| 147 | 3 | my ( $self, $c, $username ) = @_; | |||||||||||||||||||||||||||
| 148 | 3 | my $schema_name = $self->schema; | |||||||||||||||||||||||||||
| 149 | 3 | my $schema = $c->yancy->schema( $schema_name ); | |||||||||||||||||||||||||||
| 150 | 3 | my $username_field = $self->username_field; | |||||||||||||||||||||||||||
| 151 | 3 | my %search; | |||||||||||||||||||||||||||
| 152 | 3 | ▶ | if ( my $field = $self->plugin_field ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $search{ $field } = $self->moniker; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 3 | ▶ | if ( $username_field && $username_field ne $schema->{'x-id-field'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$username_field and $username_field ne $schema->{"x-id-field"}
MC/DC0%0/2
$username_field and $username_field ne $schema->{"x-id-field"}
$username_field
$username_field ne $schema->{"x-id-field"}
Truth table33%1/3
A and B
A= $username_fieldB= $username_field ne $schema->{"x-id-field"}
| |||||||||||||||||||||||||||||
| 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 | 3 | return $c->yancy->backend->get( $schema_name, $username ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 6 | ▶ | sub _handle_auth { | ||||||||||||||||||||||||||
|
Subroutine
_handle_auth: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 164 | 6 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Verify the CSRF from Github | ||||||||||||||||||||||||||||
| 167 | 6 | ▶ | if ( my $code = $c->param( 'code' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 3 | ▶ | if ( $c->param( 'state' ) ne $c->csrf_token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 3 | $c->render( status => 400, text => 'CSRF token failure' ); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 6 | return $self->SUPER::_handle_auth( $c ); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 3 | ▶ | sub get_authorize_url { | ||||||||||||||||||||||||||
|
Subroutine
get_authorize_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 178 | 3 | my %client_info = ( | |||||||||||||||||||||||||||
| 179 | client_id => $self->client_id, | ||||||||||||||||||||||||||||
| 180 | state => $c->csrf_token, | ||||||||||||||||||||||||||||
| 181 | ); | ||||||||||||||||||||||||||||
| 182 | 3 | return $self->authorize_url->clone->query( \%client_info ); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 3 | ▶ | sub handle_token_p { | ||||||||||||||||||||||||||
|
Subroutine
handle_token_p: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 3 | my ( $self, $c, $token ) = @_; | |||||||||||||||||||||||||||
| 187 | 3 | my $api_url = $self->api_url->clone; | |||||||||||||||||||||||||||
| 188 | 3 | $api_url->path->trailing_slash( '1' )->merge( 'user' ); | |||||||||||||||||||||||||||
| 189 | return $self->ua->get_p( | ||||||||||||||||||||||||||||
| 190 | $api_url, | ||||||||||||||||||||||||||||
| 191 | { Authorization => join( ' ', 'token', $token ) }, | ||||||||||||||||||||||||||||
| 192 | ) | ||||||||||||||||||||||||||||
| 193 | 3 | ▶ | ->then( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.3
| |||||||||||||||||||||||||||||
| 194 | 3 | my ( $tx ) = @_; | |||||||||||||||||||||||||||
| 195 | 3 | my $login = $tx->res->json( '/login' ); | |||||||||||||||||||||||||||
| 196 | 3 | ▶ | if ( !$login ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 0 | $c->render( text => 'Error getting Github login: ' . $tx->res->body ); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 3 | $c->session->{yancy}{ $self->moniker }{ github_login } = $login; | |||||||||||||||||||||||||||
| 200 | 3 | ▶ | if ( !$self->_get_user( $c, $login ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 3 | ▶ | if ( !$self->allow_register ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 1 | $c->app->log->error( 'Registration not allowed (set allow_register)' ); | |||||||||||||||||||||||||||
| 203 | 1 | $c->stash( status => 403 ); | |||||||||||||||||||||||||||
| 204 | 1 | die 'Registration of new users is not allowed'; | |||||||||||||||||||||||||||
| 205 | 0 | return; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 2 | my $schema = $c->yancy->schema( $self->schema ); | |||||||||||||||||||||||||||
| 208 | $c->yancy->create( | ||||||||||||||||||||||||||||
| 209 | $self->schema, | ||||||||||||||||||||||||||||
| 210 | 2 | ▶ | { $self->username_field || $schema->{'x-id-field'} || 'id' => $login }, | ||||||||||||||||||||||||||
|
Condition40%2/5
$self->username_field || $schema->{'x-id-field'} || 'id'
$self->username_field or $schema->{"x-id-field"}
MC/DC0%0/2
$self->username_field || $schema->{'x-id-field'} || 'id'
$self->username_field
$schema->{"x-id-field"}
Truth table33%1/3
A or B
A= $self->username_fieldB= $schema->{"x-id-field"}
| |||||||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } ) | ||||||||||||||||||||||||||||
| 214 | 1 | ▶ | ->catch( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 215 | 1 | my ( $err ) = @_; | |||||||||||||||||||||||||||
| 216 | 1 | $c->render( text => $err ); | |||||||||||||||||||||||||||
| 217 | 3 | } ); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 3 | 1; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | __END__ | ||||||||||||||||||||||||||||