-
Notifications
You must be signed in to change notification settings - Fork 7
/
OAuth2Functions.pm
347 lines (283 loc) · 8.97 KB
/
OAuth2Functions.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package OAuth2Functions;
use strict;
use warnings;
use DateTime;
use Exporter::Easy (
OK => [ qw/
oauth2_functions
/ ],
);
sub oauth2_functions {
my ( $self ) = @_;
$self->plugin(
'OAuth2::Server' => {
login_resource_owner => \&_resource_owner_logged_in,
confirm_by_resource_owner => \&_resource_owner_confirm_scopes,
verify_client => \&_verify_client,
store_auth_code => \&_store_auth_code,
verify_auth_code => \&_verify_auth_code,
store_access_token => \&_store_access_token,
verify_access_token => \&_verify_access_token,
},
);
return 1;
}
sub _resource_owner_logged_in {
my ( $c ) = @_;
if ( ! $c->session( 'session_id' ) ) {
# we need to redirect back to the /oauth/authorize route after
# login (with the original params)
my $uri = join( '?',$c->url_for('current'),$c->url_with->query );
$c->flash( 'redirect_after_login' => $uri );
$c->redirect_to( '/oauth/login' );
return 0;
}
return 1;
}
sub _resource_owner_confirm_scopes {
my ( $c,$client_id,$scopes_ref ) = @_;
my $is_allowed = $c->flash( "oauth_${client_id}" );
# if user hasn't yet allowed the client access, or if they denied
# access last time, we check [again] with the user for access
if ( ! $is_allowed ) {
$c->flash( client_id => $client_id );
$c->flash( scopes => $scopes_ref );
my $uri = join( '?',$c->url_for('current'),$c->url_with->query );
$c->flash( 'redirect_after_login' => $uri );
$c->redirect_to( '/oauth/confirm_scopes' );
}
return $is_allowed;
}
sub _verify_client {
my ( $c,$client_id,$scopes_ref ) = @_;
if ( my $client = $c->model->rs( 'Oauth2Client' )->find( $client_id ) ) {
if ( ! $client->active ) {
$c->app->log->debug( "Client ($client_id) is not active" );
return ( 0,'unauthorized_client' );
}
foreach my $rqd_scope ( @{ $scopes_ref // [] } ) {
if ( my $scope = $c->model->rs( 'Oauth2ClientScope' )->find({
'scope.description' => $rqd_scope,
'client_id' => $client_id,
},{ join => [ qw/ scope / ] }
) ) {
if ( ! $scope->allowed ) {
$c->app->log->debug( "Client disallowed scope ($rqd_scope)" );
return ( 0,'access_denied' );
}
} else {
$c->app->log->debug( "Client lacks scope ($rqd_scope)" );
return ( 0,'invalid_scope' );
}
}
return ( 1 );
}
$c->app->log->debug( "Client ($client_id) does not exist" );
return ( 0,'unauthorized_client' );
}
sub _store_auth_code {
my ( $c,$auth_code,$client_id,$expires_in,$uri,@scopes ) = @_;
my $user_id = $c->session( 'user_id' );
$c->model->rs( 'Oauth2AuthCode' )->create({
auth_code => $auth_code,
client_id => $client_id,
user_id => $user_id,
expires => DateTime->from_epoch( epoch => time + $expires_in ),
redirect_uri => $uri,
verified => 0,
});
foreach my $rqd_scope ( @scopes ) {
if ( my $scope = $c->model->rs( 'Oauth2Scope' )->find({
description => $rqd_scope
}) ) {
$scope->create_related(
'oauth2_auth_code_scopes',
{ auth_code => $auth_code, allowed => 1 }
);
} else {
$c->app->log->error(
"Unknown scope ($rqd_scope) in _store_auth_code"
);
}
}
return;
}
sub _verify_auth_code {
my ( $c,$client_id,$client_secret,$auth_code,$uri ) = @_;
my $client = $c->model->rs( 'Oauth2Client' )->find( $client_id )
|| return ( 0,'unauthorized_client' );
my $ac = $c->model->rs( 'Oauth2AuthCode' )->find({
client_id => $client_id,
auth_code => $auth_code,
});
if (
! $ac
or $ac->verified
or ( $uri ne $ac->redirect_uri )
or ( $ac->expires->epoch <= time )
or ! _check_password( $client_secret,$client->secret )
) {
$c->app->log->debug( "Auth code does not exist" )
if ! $ac;
$c->app->log->debug( "Client secret does not match" )
if ! _check_password( $client_secret,$client->secret );
if ( $ac ) {
$c->app->log->debug( "Client secret does not match" )
if ( $uri && $ac->redirect_uri ne $uri );
$c->app->log->debug( "Auth code expired" )
if ( $ac->expires->epoch <= time );
if ( $ac->verified ) {
# the auth code has been used before - we must revoke the auth code
# and any associated access tokens (same client_id and user_id)
$c->app->log->debug(
"Auth code already used to get access token, "
. "revoking all associated access tokens"
);
$ac->delete;
if ( my $rs = $c->model->rs( 'Oauth2AccessToken' )->search({
client_id => $client_id,
user_id => $ac->user_id,
}) ) {
while ( my $row = $rs->next ) {
$row->delete;
}
}
}
}
return ( 0,'invalid_grant' );
}
$ac->verified( 1 );
$ac->update;
# scopes are those that were requested in the authorization request, not
# those stored in the client (i.e. what the auth request restriced scopes
# to and not everything the client is capable of)
my %scope = map { $_->scope->description => 1 }
$ac->oauth2_auth_code_scopes->all;
return ( $client_id,undef,{ %scope } );
}
sub _check_password {
my ( $hashed_password,$password ) = @_;
die "Implement _check_password";
}
sub _store_access_token {
my (
$c,$client,$auth_code,$access_token,$refresh_token,
$expires_in,$scope,$old_refresh_token
) = @_;
my ( $user_id );
if ( ! defined( $auth_code ) && $old_refresh_token ) {
# must have generated an access token via a refresh token so revoke the
# old access token and refresh token (also copy required data if missing)
my $prt = $c->model->rs( 'Oauth2RefreshToken' )
->find( $old_refresh_token );
my $pat = $c->model->rs( 'Oauth2AccessToken' )
->find( $prt->access_token );
# access tokens can be revoked, whilst refresh tokens can remain so we
# need to get the data from the refresh token as the access token may
# no longer exist at the point that the refresh token is used
$scope //= {
map { $_->scope->description => 1 }
$prt->oauth2_refresh_token_scopes->all
};
$user_id = $prt->user_id;
} else {
my $ac = $c->model->rs( 'Oauth2AuthCode' )->find( $auth_code );
$user_id = $ac->user_id;
}
if ( ref( $client ) ) {
$scope //= $client->{scope};
$user_id //= $client->{user_id};
$client = $client->{client_id};
}
foreach my $token_type ( qw/ Access Refresh / ) {
# if the client has en existing access/refresh token we need to revoke it
if ( my $rs = $c->model->rs( "Oauth2${token_type}Token" )->search({
client_id => $client,
user_id => $user_id,
}) ) {
$c->app->log->debug( "Revoking existing @{[lc $token_type]} token" );
while ( my $row = $rs->next ) {
$row->delete;
}
}
}
# N.B. you should probably encrypt the access tokens and refresh tokens here
$c->model->rs( 'Oauth2AccessToken' )->create({
access_token => $access_token,
refresh_token => $refresh_token,
client_id => $client,
user_id => $user_id,
expires => DateTime->from_epoch( epoch => time + $expires_in ),
});
$c->model->rs( 'Oauth2RefreshToken' )->create({
refresh_token => $refresh_token,
access_token => $access_token,
client_id => $client,
user_id => $user_id,
});
foreach my $rqd_scope ( keys( %{ $scope } ) ) {
if ( my $db_scope = $c->model->rs( 'Oauth2Scope' )->find({
description => $rqd_scope
}) ) {
foreach my $related ( qw/ access_token refresh_token / ) {
# N.B. you should probably encrypt the access tokens and refresh tokens here
$db_scope->create_related( "oauth2_${related}_scopes",{
allowed => $scope->{$rqd_scope},
$related => $related eq 'access_token'
? $access_token : $refresh_token,
});
}
} else {
$c->app->log->error(
"Unknown scope ($rqd_scope) in _store_access_token"
);
}
}
return;
}
sub _verify_access_token {
my ( $c,$access_token,$scopes_ref ) = @_;
if (
my $rt = $c->model->rs( 'Oauth2RefreshToken' )->find( $access_token )
) {
foreach my $scope ( @{ $scopes_ref // [] } ) {
my $db_scope = $c->model->rs( 'Oauth2RefreshTokenScope' )->find({
'scope.description' => $scope,
'refresh_token' => $access_token,
},{ join => [ qw/ scope / ] }
);
if ( ! $db_scope || ! $db_scope->allowed ) {
$c->app->log->debug( "Refresh token doesn't have scope ($scope)" );
return ( 0,'invalid_grant' );
}
}
return $rt->client_id;
} elsif (
my $at = $c->model->rs( 'Oauth2AccessToken' )->find( $access_token )
) {
if ( $at->expires->epoch <= time ) {
$c->app->log->debug( "Access token has expired" );
$at->delete;
return ( 0,'invalid_grant' );
}
foreach my $scope ( @{ $scopes_ref // [] } ) {
my $db_scope = $c->model->rs( 'Oauth2AccessTokenScope' )->find({
'scope.description' => $scope,
'access_token' => $access_token,
},{ join => [ qw/ scope / ] }
);
if ( ! $db_scope || ! $db_scope->allowed ) {
$c->app->log->debug( "Access token doesn't have scope ($scope)" );
return ( 0,'invalid_grant' );
}
}
return {
client_id => $at->client_id,
user_id => $at->user_id,
};
} else {
$c->app->log->debug( "Access token does not exist" );
return ( 0,'invalid_grant' );
}
}
1;