nazar-pc /
CleverStyle-Framework
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package OAuth2 |
||
| 4 | * @category modules |
||
| 5 | * @author Nazar Mokrynskyi <[email protected]> |
||
| 6 | * @license 0BSD |
||
| 7 | */ |
||
| 8 | namespace cs\modules\OAuth2; |
||
| 9 | use |
||
| 10 | cs\Config, |
||
| 11 | cs\Event, |
||
| 12 | cs\ExitException, |
||
| 13 | cs\Request, |
||
| 14 | cs\Response; |
||
| 15 | |||
| 16 | Event::instance() |
||
| 17 | ->on( |
||
| 18 | 'System/Request/routing_replace/after', |
||
| 19 | function ($data) { |
||
| 20 | if (!Config::instance()->module('Blogs')->enabled()) { |
||
| 21 | return; |
||
| 22 | } |
||
| 23 | if (!$data['regular_path']) { |
||
| 24 | return; |
||
| 25 | } |
||
| 26 | if ($data['current_module'] == 'OAuth2') { |
||
| 27 | Response::instance() |
||
| 28 | ->header('cache-control', 'no-store') |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 29 | ->header('pragma', 'no-cache'); |
||
| 30 | } |
||
| 31 | $request_data = Request::instance()->data; |
||
| 32 | Event::instance()->once( |
||
| 33 | 'System/User/construct/after', |
||
| 34 | function () use ($request_data) { |
||
| 35 | Request::instance()->data = $request_data; |
||
| 36 | } |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | ) |
||
| 40 | ->on( |
||
| 41 | 'System/Session/del_all', |
||
| 42 | function ($data) { |
||
| 43 | if (Config::instance()->module('OAuth2')->enabled()) { |
||
| 44 | OAuth2::instance()->del_access(0, $data['id']); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | ) |
||
| 48 | ->on( |
||
| 49 | 'System/User/construct/before', |
||
| 50 | function () { |
||
| 51 | if (!Config::instance()->module('OAuth2')->enabled()) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | $Request = Request::instance(); |
||
| 55 | /** |
||
| 56 | * Works only for API requests |
||
| 57 | */ |
||
| 58 | if (!$Request->api_path) { |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | if (preg_match('/Bearer ([0-9a-z]{32})/i', $Request->header('authorization'), $access_token)) { |
||
| 62 | $access_token = $access_token[1]; |
||
| 63 | } else { |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | $OAuth2 = OAuth2::instance(); |
||
| 67 | $token_data = $OAuth2->get_token($access_token); |
||
| 68 | if (!$token_data) { |
||
|
0 ignored issues
–
show
|
|||
| 69 | $e = new ExitException( |
||
| 70 | [ |
||
| 71 | 'access_denied', |
||
| 72 | 'access_token expired' |
||
| 73 | ], |
||
| 74 | 403 |
||
| 75 | ); |
||
| 76 | $e->setJson(); |
||
| 77 | throw $e; |
||
| 78 | } |
||
| 79 | $client = $OAuth2->get_client($token_data['client_id']); |
||
| 80 | if (!$client) { |
||
| 81 | $e = new ExitException( |
||
| 82 | [ |
||
| 83 | 'access_denied', |
||
| 84 | 'Invalid client id' |
||
| 85 | ], |
||
| 86 | 400 |
||
| 87 | ); |
||
| 88 | $e->setJson(); |
||
| 89 | throw $e; |
||
| 90 | } elseif (!$client['active']) { |
||
| 91 | $e = new ExitException( |
||
| 92 | [ |
||
| 93 | 'access_denied', |
||
| 94 | 'Inactive client id' |
||
| 95 | ], |
||
| 96 | 403 |
||
| 97 | ); |
||
| 98 | $e->setJson(); |
||
| 99 | throw $e; |
||
| 100 | } |
||
| 101 | /** @noinspection IfConditionalsWithoutCurvyBracketsInspection */ |
||
| 102 | if ($token_data['type'] == 'token') { |
||
| 103 | // TODO: add some mark if this is client-side only token, so that it can be accounted by components |
||
| 104 | // Also admin access should be blocked for client-side only tokens |
||
| 105 | } |
||
| 106 | $Request->headers['user-agent'] = "OAuth2-$client[name]-$client[id]"; |
||
| 107 | $Request->data['session'] = $token_data['session']; |
||
| 108 | Response::instance()->cookie('session', $token_data['session'], 0, true); |
||
| 109 | } |
||
| 110 | ) |
||
| 111 | ->on( |
||
| 112 | 'admin/System/modules/install/after', |
||
| 113 | function ($data) { |
||
| 114 | if ($data['name'] != 'OAuth2') { |
||
| 115 | return; |
||
| 116 | } |
||
| 117 | Config::instance()->module('OAuth2')->set( |
||
| 118 | [ |
||
| 119 | 'expiration' => 3600, |
||
| 120 | 'automatic_prolongation' => 1 |
||
| 121 | ] |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | ); |
||
| 125 |