Yancy/Plugin/Auth/Token.pm

filter: Statementstmt 85.9% 67 / 78 Branchbran 56.6% 17 / 30 Conditioncond 50.0% 8 / 16 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 91.6% 11 / 12 Podpod 25.0% 2 / 8 totaltotal 69.7% 106 / 152 CC 22 SCAR 19.9
CC
22
Cov
74%
init
13.7
check_token
12.0
__ANON__
5.5
CRAP 7.3
« Yancy/Util.pm ↑ Summary Yancy/Controller/Yancy/MultiTenant.pm »
1 package Yancy::Plugin::Auth::Token;
2 3 our $VERSION = '1.088';
3 # ABSTRACT: A simple token-based auth
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use Mojolicious::Lite;
8 #pod plugin Yancy => {
9 #pod backend => 'sqlite://myapp.db',
10 #pod schema => {
11 #pod tokens => {
12 #pod properties => {
13 #pod id => { type => 'integer', readOnly => 1 },
14 #pod username => { type => 'string' },
15 #pod token => { type => 'string' },
16 #pod },
17 #pod },
18 #pod },
19 #pod };
20 #pod app->yancy->plugin( 'Auth::Token' => {
21 #pod schema => 'tokens',
22 #pod username_field => 'username',
23 #pod token_field => 'token',
24 #pod token_digest => {
25 #pod type => 'SHA-1',
26 #pod },
27 #pod } );
28 #pod
29 #pod =head1 DESCRIPTION
30 #pod
31 #pod B<Note:> This module is C<EXPERIMENTAL> and its API may change before
32 #pod Yancy v2.000 is released.
33 #pod
34 #pod This plugin provides a basic token-based authentication scheme for
35 #pod a site. Tokens are provided in the HTTP C<Authorization> header:
36 #pod
37 #pod Authorization: Token
38 #pod
39 #pod =head1 CONFIGURATION
40 #pod
41 #pod This plugin has the following configuration options.
42 #pod
43 #pod =head2 schema
44 #pod
45 #pod The name of the Yancy schema that holds tokens. Required.
46 #pod
47 #pod =head2 token_field
48 #pod
49 #pod The name of the field to use for the token. Defaults to C<token>. The
50 #pod token itself is meaningless except to authenticate a user. It must be
51 #pod unique, and it should be treated like a password.
52 #pod
53 #pod =head2 token_digest
54 #pod
55 #pod This is the hashing mechanism that should be used for creating new
56 #pod tokens via the L<add_token|/yancy.auth.add_token> helper. The default type is C<SHA-1>.
57 #pod
58 #pod This value should be a hash of digest configuration. The one required
59 #pod field is C<type>, and should be a type supported by the L<Digest> module:
60 #pod
61 #pod =over
62 #pod
63 #pod =item * MD5 (part of core Perl)
64 #pod
65 #pod =item * SHA-1 (part of core Perl)
66 #pod
67 #pod =item * SHA-256 (part of core Perl)
68 #pod
69 #pod =item * SHA-512 (part of core Perl)
70 #pod
71 #pod =back
72 #pod
73 #pod Additional fields are given as configuration to the L<Digest> module.
74 #pod Not all Digest types require additional configuration.
75 #pod
76 #pod =head2 username_field
77 #pod
78 #pod The name of the field in the schema which is the user's identifier.
79 #pod This can be a user name, ID, or e-mail address, and is used to keep track
80 #pod of who owns the token.
81 #pod
82 #pod This field is optional. If not specified, no user name will be stored.
83 #pod
84 #pod =head1 HELPERS
85 #pod
86 #pod This plugin has the following helpers.
87 #pod
88 #pod =head2 yancy.auth.current_user
89 #pod
90 #pod Get the current user from the session, if any. Returns C<undef> if no
91 #pod user was found in the session.
92 #pod
93 #pod my $user = $c->yancy->auth->current_user
94 #pod || return $c->render( status => 401, text => 'Unauthorized' );
95 #pod
96 #pod =head2 yancy.auth.require_user
97 #pod
98 #pod Validate there is a logged-in user and optionally that the user data has
99 #pod certain values. See L<Yancy::Plugin::Auth::Role::RequireUser/require_user>.
100 #pod
101 #pod # Display the user dashboard, but only to logged-in users
102 #pod my $auth_route = $app->routes->under( '/user', $app->yancy->auth->require_user );
103 #pod $auth_route->get( '' )->to( 'user#dashboard' );
104 #pod
105 #pod =head2 yancy.auth.add_token
106 #pod
107 #pod $ perl myapp.pl eval 'app->yancy->auth->add_token( "username" )'
108 #pod
109 #pod Generate a new token and add it to the database. C<"username"> is the
110 #pod username for the token. The token will be generated as a base-64 encoded
111 #pod hash of the following input:
112 #pod
113 #pod =over
114 #pod
115 #pod =item * The username
116 #pod
117 #pod =item * The site's L<secret|https://mojolicious.org/perldoc/Mojolicious#secrets>
118 #pod
119 #pod =item * The current L<time|perlfunc/time>
120 #pod
121 #pod =item * A random number
122 #pod
123 #pod =back
124 #pod
125 #pod =head1 SEE ALSO
126 #pod
127 #pod L<Yancy::Plugin::Auth>
128 #pod
129 #pod =cut
130  
131 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 3 ▶use Yancy::Util qw( currym derp );
Subroutine BEGIN: called CC=1 SCAR=0.0
133 3 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 3 has schema =>;
136 3 has username_field =>;
137 3 has token_field =>;
138 3 has token_digest =>;
139 3 has plugin_field => undef;
140 3 has moniker => 'token';
141  
142 2 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
143 2 my ( $self, $app, $config ) = @_;
144 2 $self->init( $app, $config );
145 2 $app->helper(
146 'yancy.auth.current_user' => currym( $self, 'current_user' ),
147 );
148 2 $app->helper(
149 'yancy.auth.add_token' => currym( $self, 'add_token' ),
150 );
151 2 $app->helper(
152 'yancy.auth.require_user' => currym( $self, 'require_user' ),
153 );
154 }
155  
156 3 ▶sub init {
Subroutine init: called CC=10 SCAR=26.2
Pod undocumented
157 3 my ( $self, $app, $config ) = @_;
158 my $schema = $config->{schema} || $config->{collection}
159 3 ▶ || die "Error configuring Auth::Token plugin: No schema defined\n";
Condition40%2/5
$config->{'schema'} || $config->{'collection'} || die("Error configuring Auth::Token plugin: No schema defined\n")
l!l
30
$config->{"schema"} or $config->{"collection"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$config->{'schema'} || $config->{'collection'} || die("Error configuring Auth::Token plugin: No schema defined\n")
$config->{"schema"} $config->{"collection"}
Truth table33%1/3
A or B
A= $config->{"schema"}B= $config->{"collection"}
ABresult
1X1
011
000
160 derp "'collection' configuration in Auth::Token is now 'schema'. Please fix your configuration.\n"
161 3 ▶ if $config->{collection};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"collection"} 0 3 3
162 3 ▶ die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
unless $app->yancy->schema($schema) 0 3 3
163 q{Error configuring Auth::Token plugin: Schema "%s" not found}."\n",
164 $schema,
165 ) unless $app->yancy->schema( $schema );
166  
167 3 $self->schema( $schema );
168 3 $self->username_field( $config->{username_field} );
169 $self->token_field(
170 3 ▶ $config->{token_field} || $config->{password_field} || 'token'
Condition60%3/5
$config->{'token_field'} || $config->{'password_field'} || 'token'
l!l
30
$config->{"token_field"} or $config->{"password_field"}
l!l&&r!l&&!r
210
MC/DC0%0/2
$config->{'token_field'} || $config->{'password_field'} || 'token'
$config->{"token_field"} $config->{"password_field"}
Truth table66%2/3
A or B
A= $config->{"token_field"}B= $config->{"password_field"}
ABresult
1X1
011
000
171 );
172  
173 3 my $digest_type = delete $config->{token_digest}{type};
174 3 ▶ if ( $digest_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digest_type) 2 1 3
175 2 my $digest = eval {
176 2 Digest->new( $digest_type, %{ $config->{token_digest} } )
177 };
178 2 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 2 2
179 0 ▶ if ( $error =~ m{Can't locate Digest/${digest_type}\.pm in \@INC} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ m[Can't locate Digest/$digest_type\.pm in \@INC]u) 0 0 0
180 0 die sprintf(
181 q{Error configuring Auth::Token plugin: Token digest type "%s" not found}."\n",
182 $digest_type,
183 );
184 }
185 0 die "Error configuring Auth::Token plugin: Error loading Digest module: $@\n";
186 }
187 2 $self->token_digest( $digest );
188 }
189  
190 my $route = $app->yancy->routify(
191 $config->{route},
192 3 '/yancy/auth/' . $self->moniker,
193 );
194 3 $route->to( cb => currym( $self, 'check_token' ) );
195 }
196  
197 8 ▶sub current_user {
Subroutine current_user: called CC=5 SCAR=16.2
Pod undocumented
198 8 my ( $self, $c ) = @_;
199 8 ▶ return undef unless my $auth = $c->req->headers->authorization;
Branch: 1 branch
BranchTrueFalseTotal
unless my $auth = $c->req->headers->authorization 4 4 8
200 4 ▶ return undef unless my ( $token ) = $auth =~ /^Token\ (\S+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless my($token) = $auth =~ /^Token\ (\S+)$/u 1 3 4
201 3 my $schema = $self->schema;
202 3 my %search;
203 3 $search{ $self->token_field } = $token;
204 3 ▶ if ( my $field = $self->plugin_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = $self->plugin_field) 1 2 3
205 1 $search{ $field } = $self->moniker;
206 }
207 3 my @users = $c->yancy->list( $schema, \%search );
208 3 ▶ if ( @users > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@users > 1) 0 3 3
209 0 die "Refusing to auth: Multiple users with the same token found";
210 0 return undef;
211 }
212 3 return $users[0];
213 }
214  
215 0 ▶sub check_token {
Subroutine check_token: not called CC=3 SCAR=24.8
Pod undocumented
216 0 my ( $self, $c ) = @_;
217 0 my $field = $self->username_field;
218 0 ▶ if ( my $user = $self->current_user( $c ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $self->current_user($c)) 0 0 0
219 return $c->render(
220 0 ▶ text => $field ? $user->{ $field } : 'Ok',
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 0 0 0
221 );
222 }
223 0 return $c->render(
224 status => 401,
225 text => 'Unauthorized',
226 );
227 }
228  
229 2 ▶sub login_form {
Subroutine login_form: called CC=1 SCAR=0.0
Pod undocumented
230 # There is no login form for a token
231 2 return undef;
232 }
233  
234 3 ▶sub logout {
Subroutine logout: called CC=1 SCAR=0.0
Pod undocumented
235 # There is no way to log out a token
236 3 return;
237 }
238  
239 1 ▶sub add_token {
Subroutine add_token: called CC=3 SCAR=11.2
Pod undocumented
240 1 my ( $self, $c, $username, %user ) = @_;
241 1 my @parts = ( $username, $c->app->secrets->[0], $$, scalar time, int rand 1_000_000 );
242 1 my $token = $self->token_digest->clone->add( join "", @parts )->b64digest;
243 1 my $username_field = $self->username_field;
244 1 ▶ $c->yancy->create( $self->schema, {
Branch: 2 branches
BranchTrueFalseTotal
$username_field ? : 1 0 1
$self->plugin_field ? : 0 1 1
245 ( $username_field ? ( $username_field => $username ) : () ),
246 $self->token_field => $token,
247 ( $self->plugin_field ? ( $self->plugin_field => $self->moniker ) : () ),
248 %user,
249 } );
250 1 return $token;
251 }
252  
253 #pod =method require_user
254 #pod
255 #pod my $subref = $c->yancy->auth->require_user( \%match );
256 #pod
257 #pod Build a callback to validate there is a logged-in user, and optionally
258 #pod that the current user has certain fields set. C<\%match> is optional and
259 #pod is a L<SQL::Abstract where clause|SQL::Abstract/WHERE CLAUSES> matched
260 #pod with L<Yancy::Util/match>.
261 #pod
262 #pod # Ensure the user is logged-in
263 #pod my $user_cb = $app->yancy->auth->require_user;
264 #pod my $user_only = $app->routes->under( $user_cb );
265 #pod
266 #pod # Ensure the user's "is_admin" field is set to 1
267 #pod my $admin_cb = $app->yancy->auth->require_user( { is_admin => 1 } );
268 #pod my $admin_only = $app->routes->under( $admin_cb );
269 #pod
270 #pod =cut
271  
272 1 ▶sub require_user {
Subroutine require_user: called CC=1 SCAR=0.1
Pod documented
273 1 my ( $self, $c, $where ) = @_;
274 3 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=17.0
275 3 my ( $c ) = @_;
276 #; say "Are you authorized? " . $c->yancy->auth->current_user;
277 3 my $user = $c->yancy->auth->current_user;
278 3 ▶ if ( !$where && $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $where and $user) 1 2 3
Condition66%2/3
not $where and $user
!ll&&!rl&&r
021
MC/DC50%1/2
not $where and $user
not $where $user
Truth table66%2/3
not(A) and B
A= not $whereB= $user
ABresult
0X0
100
111
279 1 return 1;
280 }
281 2 ▶ if ( $where && match( $where, $user ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where and match($where, $user)) 0 2 2
Condition33%1/3
$where and match($where, $user)
!ll&&!rl&&r
200
MC/DC0%0/2
$where and match($where, $user)
$where match($where, $user)
Truth table33%1/3
A and B
A= $whereB= match($where, $user)
ABresult
0X0
100
111
282 0 return 1;
283 }
284 $c->stash(
285 2 template => 'yancy/auth/unauthorized',
286 status => 401,
287 );
288 2 $c->respond_to(
289 json => {},
290 html => {},
291 );
292 2 return undef;
293 1 };
294 }
295  
296 3 1;
297  
298 __END__
« Yancy/Util.pm ↑ Summary Yancy/Controller/Yancy/MultiTenant.pm »