1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2016 SURFnet. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
8
|
|
|
* License, or (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\OAuth; |
20
|
|
|
|
21
|
|
|
use fkooman\RemoteStorage\Http\BeforeHookInterface; |
22
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
23
|
|
|
use fkooman\RemoteStorage\Http\Request; |
24
|
|
|
|
25
|
|
|
class BearerAuthenticationHook implements BeforeHookInterface |
26
|
|
|
{ |
27
|
|
|
/** @var TokenStorage */ |
28
|
|
|
private $tokenStorage; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $realm; |
32
|
|
|
|
33
|
|
|
public function __construct(TokenStorage $tokenStorage, $realm = 'Protected Area') |
34
|
|
|
{ |
35
|
|
|
$this->tokenStorage = $tokenStorage; |
36
|
|
|
$this->realm = $realm; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function executeBefore(Request $request, array $hookData) |
40
|
|
|
{ |
41
|
|
|
$authorizationHeader = $request->getHeader('HTTP_AUTHORIZATION', false, null); |
42
|
|
|
|
43
|
|
|
// is authorization header there? |
44
|
|
|
if (is_null($authorizationHeader)) { |
45
|
|
|
throw new HttpException( |
46
|
|
|
'no_token', |
47
|
|
|
401, |
48
|
|
|
['WWW-Authenticate' => sprintf('Bearer realm="%s"', $this->realm)] |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// validate the HTTP_AUTHORIZATION header |
53
|
|
|
if (false === $bearerToken = self::getBearerToken($authorizationHeader)) { |
54
|
|
|
throw $this->invalidTokenException(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$accessTokenKey = $bearerToken[0]; |
58
|
|
|
$accessToken = $bearerToken[1]; |
59
|
|
|
|
60
|
|
|
$tokenInfo = $this->tokenStorage->get($accessTokenKey); |
61
|
|
|
if (false === $tokenInfo) { |
62
|
|
|
throw $this->invalidTokenException(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// time safe string compare, using polyfill on PHP < 5.6 |
66
|
|
|
if (hash_equals($tokenInfo['access_token'], $accessToken)) { |
67
|
|
|
return new TokenInfo($tokenInfo); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw $this->invalidTokenException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private static function getBearerToken($authorizationHeader) |
74
|
|
|
{ |
75
|
|
|
if (1 !== preg_match('|^Bearer ([[:alpha:][:digit:]-._~+/]+=*)$|', $authorizationHeader, $m)) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$bearerToken = $m[1]; |
80
|
|
|
if (false === strpos($bearerToken, '.')) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return explode('.', $bearerToken); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function invalidTokenException() |
88
|
|
|
{ |
89
|
|
|
return new HttpException( |
90
|
|
|
'invalid_token', |
91
|
|
|
401, |
92
|
|
|
[ |
93
|
|
|
'WWW-Authenticate' => sprintf( |
94
|
|
|
'Bearer realm="%s",error="invalid_token"', |
95
|
|
|
$this->realm |
96
|
|
|
), |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|