|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2016-2016 The callingallpapers.com Developer Team |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
* in the Software without restriction, including without limitation the rights |
|
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
* furnished to do so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
13
|
|
|
* all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
21
|
|
|
* THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Andreas Heigl<[email protected]> |
|
24
|
|
|
* @copyright 2016-2016 The callingallpapers.com Developer Team |
|
25
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
|
26
|
|
|
* @version 0.0 |
|
27
|
|
|
* @since 16.01.2016 |
|
28
|
|
|
* @link https://github.com/joindin/callingallpapers |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
namespace Callingallpapers\Api\Middleware; |
|
32
|
|
|
|
|
33
|
|
|
use Callingallpapers\Api\PersistenceLayer\UserPersistenceLayer; |
|
34
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
35
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
36
|
|
|
|
|
37
|
|
|
class OAuth |
|
38
|
|
|
{ |
|
39
|
|
|
protected $app; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct($app) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->app = $app; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Example middleware invokable class |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request |
|
50
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response |
|
51
|
|
|
* @param callable $next Next middleware |
|
52
|
|
|
* |
|
53
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __invoke( |
|
56
|
|
|
ServerRequestInterface $request, |
|
57
|
|
|
ResponseInterface $response, |
|
58
|
|
|
callable $next |
|
59
|
|
|
) { |
|
60
|
|
|
$auth = $request->getHeader('Authenticate'); |
|
61
|
|
|
if ($request->getMethod() === 'GET') { |
|
62
|
|
|
// Get is allowed without authentication |
|
63
|
|
|
// Rate-Limit is handlede by another Middleware |
|
64
|
|
|
return $next($request, $response); |
|
65
|
|
|
} |
|
66
|
|
|
if (empty($auth)) { |
|
67
|
|
|
return $this->getNoAuthResponse($response); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$bearer = explode(' ', $auth[0]); |
|
71
|
|
|
if (! isset($bearer[1])) { |
|
72
|
|
|
return $this->getNoAuthResponse($response); |
|
73
|
|
|
} |
|
74
|
|
|
$bearer = $bearer[1]; |
|
75
|
|
|
|
|
76
|
|
|
$upl = new UserPersistenceLayer($this->app->getContainer()['pdo']); |
|
77
|
|
|
try { |
|
78
|
|
|
$user = $upl->getUserForToken($bearer); |
|
79
|
|
|
} catch (\Exception $e) { |
|
80
|
|
|
return $this->getNoAuthResponse($response, 'invalid token', 'Invalid Access-Token provided'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$request = $request->withAttribute('user', $user['user']); |
|
84
|
|
|
|
|
85
|
|
|
return $next($request, $response); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function getNoAuthResponse($response, $error = 'no token', $description = 'No Access-Token provided') |
|
89
|
|
|
{ |
|
90
|
|
|
$response = $response->withHeader('WWW-Authenticate', 'Bearer realm="callingallpapers", error="' . $error . '", error_desciption="' . $description . '"'); |
|
91
|
|
|
$response = $response->withStatus(401); |
|
92
|
|
|
|
|
93
|
|
|
return $response; |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|