|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace App\Http\Middleware; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Psr\Http\Message\ServerRequestInterface; |
|
19
|
|
|
use O2System\Psr\Http\Server\RequestHandlerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class WebToken |
|
23
|
|
|
* |
|
24
|
|
|
* @package O2System\Framework\Http\Middleware |
|
25
|
|
|
*/ |
|
26
|
|
|
class WebToken implements RequestHandlerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* WebToken::handle |
|
30
|
|
|
* |
|
31
|
|
|
* Handles a request and produces a response |
|
32
|
|
|
* |
|
33
|
|
|
* May call other collaborating code to generate the response. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function handle(ServerRequestInterface $request) |
|
36
|
|
|
{ |
|
37
|
|
|
if (services()->has('webTokenAuthentication')) { |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* $token |
|
41
|
|
|
* |
|
42
|
|
|
* This is an example to implement HTTP X-WEB-TOKEN authentication. |
|
43
|
|
|
* The web token can be generated freely according to your own token generator concept. |
|
44
|
|
|
* |
|
45
|
|
|
* @example |
|
46
|
|
|
* This token is generated from simple generator concept. |
|
47
|
|
|
* $token = md5(json_encode(['uid' => 7, 'username' => 'steevenz'] )); |
|
48
|
|
|
* |
|
49
|
|
|
* // result: ed3d68c4d51f52734e5bb6add37147d2 |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* $users |
|
56
|
|
|
* |
|
57
|
|
|
* This is a users database thats hold users accounts. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
$users = [ |
|
62
|
|
|
'ed3d68c4d51f52734e5bb6add37147d2' => [ |
|
63
|
|
|
'uid' => 7, |
|
64
|
|
|
'username' => 'steevenz', |
|
65
|
|
|
] |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
if($token = input()->webToken()) { |
|
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Let's verify it with Web Token Authentication service callback. |
|
71
|
|
|
*/ |
|
72
|
|
|
$validate = services('webTokenAuthentication')->verify($token, function($token) use ($users) { |
|
|
|
|
|
|
73
|
|
|
return array_key_exists($token, $users); |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
if($validate) { |
|
77
|
|
|
$payload = $users[ $token ]; // this is an example payload |
|
78
|
|
|
globals()->store('payload', $payload); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if(empty($payload)) { |
|
83
|
|
|
output()->sendError(403, [ |
|
84
|
|
|
'message' => language()->getLine('403_INVALID_WEBTOKEN') |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.