|
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\Exception\HttpException; |
|
22
|
|
|
use fkooman\RemoteStorage\Http\Request; |
|
23
|
|
|
|
|
24
|
|
|
class BearerAuthentication |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var TokenStorage */ |
|
27
|
|
|
private $tokenStorage; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
private $realm; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(TokenStorage $tokenStorage, $realm = 'Protected Area') |
|
33
|
|
|
{ |
|
34
|
|
|
$this->tokenStorage = $tokenStorage; |
|
35
|
|
|
$this->realm = $realm; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function optionalAuth(Request $request) |
|
39
|
|
|
{ |
|
40
|
|
|
$authorizationHeader = $request->getHeader('HTTP_AUTHORIZATION', false, null); |
|
41
|
|
|
|
|
42
|
|
|
// is authorization header there? |
|
43
|
|
|
if (is_null($authorizationHeader)) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->requireAuth($request); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function requireAuth(Request $request) |
|
51
|
|
|
{ |
|
52
|
|
|
$authorizationHeader = $request->getHeader('HTTP_AUTHORIZATION'); |
|
53
|
|
|
|
|
54
|
|
|
// validate the HTTP_AUTHORIZATION header |
|
55
|
|
|
if (false === $bearerToken = self::getBearerToken($authorizationHeader)) { |
|
56
|
|
|
throw $this->invalidTokenException(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$accessTokenKey = $bearerToken[0]; |
|
60
|
|
|
$accessToken = $bearerToken[1]; |
|
61
|
|
|
|
|
62
|
|
|
$tokenInfo = $this->tokenStorage->get($accessTokenKey); |
|
63
|
|
|
if (false === $tokenInfo) { |
|
64
|
|
|
throw $this->invalidTokenException(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// time safe string compare, using polyfill on PHP < 5.6 |
|
68
|
|
|
if (hash_equals($tokenInfo['access_token'], $accessToken)) { |
|
69
|
|
|
return new TokenInfo($tokenInfo); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
throw $this->invalidTokenException(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private static function getBearerToken($authorizationHeader) |
|
76
|
|
|
{ |
|
77
|
|
|
if (1 !== preg_match('|^Bearer ([[:alpha:][:digit:]-._~+/]+=*)$|', $authorizationHeader, $m)) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$bearerToken = $m[1]; |
|
82
|
|
|
if (false === strpos($bearerToken, '.')) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return explode('.', $bearerToken); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function invalidTokenException() |
|
90
|
|
|
{ |
|
91
|
|
|
return new HttpException( |
|
92
|
|
|
'invalid_token', |
|
93
|
|
|
401, |
|
94
|
|
|
[ |
|
95
|
|
|
'WWW-Authenticate' => sprintf( |
|
96
|
|
|
'Bearer realm="%s",error="invalid_token"', |
|
97
|
|
|
$this->realm |
|
98
|
|
|
), |
|
99
|
|
|
] |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|