Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.67% |
44 / 67 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Auth | |
65.67% |
44 / 67 |
|
25.00% |
2 / 8 |
24.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| me | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| login | |
65.38% |
17 / 26 |
|
0.00% |
0 / 1 |
3.37 | |||
| tokenLogin | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 | |||
| token | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
3.01 | |||
| invalidateToken | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| unauthorized | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| logout | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Controller; |
| 6 | |
| 7 | use Celemas\Core\Factory\Factory; |
| 8 | use Celemas\Core\Request; |
| 9 | use Celemas\Core\Response; |
| 10 | use Cosray\Middleware\Permission; |
| 11 | use Cosray\Validation; |
| 12 | |
| 13 | class Auth |
| 14 | { |
| 15 | public function __construct( |
| 16 | protected readonly Factory $factory, |
| 17 | protected readonly \Cosray\Auth $auth, |
| 18 | ) {} |
| 19 | |
| 20 | #[Permission('authenticated')] |
| 21 | public function me() |
| 22 | { |
| 23 | return [ |
| 24 | 'name' => 'User', |
| 25 | 'permissions' => [], |
| 26 | ]; |
| 27 | } |
| 28 | |
| 29 | public function login(Request $request): Response |
| 30 | { |
| 31 | $shape = new Validation\Login(); |
| 32 | $response = Response::create($this->factory); |
| 33 | $data = $request->json(); |
| 34 | $result = $shape->validate($data); |
| 35 | |
| 36 | if ($result->valid()) { |
| 37 | $values = $result->values(); |
| 38 | $user = $this->auth->authenticate( |
| 39 | $values['login'], |
| 40 | $values['password'], |
| 41 | $values['rememberme'], |
| 42 | true, |
| 43 | ); |
| 44 | |
| 45 | if ($user === false) { |
| 46 | return $response->json(array_merge( |
| 47 | ['error' => _('Falscher Benutzername oder Passwort'), 'loginType' => 'panel'], |
| 48 | $data, |
| 49 | ), 400); |
| 50 | } |
| 51 | |
| 52 | return $response->json($user->array()); |
| 53 | } |
| 54 | |
| 55 | $response->json( |
| 56 | array_merge( |
| 57 | ['error' => _('Bitte Benutzernamen und Passwort eingeben'), 'loginType' => 'panel'], |
| 58 | $data, |
| 59 | ), |
| 60 | 400, |
| 61 | ); |
| 62 | |
| 63 | return $response; |
| 64 | } |
| 65 | |
| 66 | public function tokenLogin(Request $request): Response |
| 67 | { |
| 68 | $shape = new Validation\TokenLogin(); |
| 69 | $response = Response::create($this->factory); |
| 70 | $result = $shape->validate($request->json()); |
| 71 | |
| 72 | if ($result->valid()) { |
| 73 | $values = $result->values(); |
| 74 | $user = $this->auth->authenticateByOneTimeToken( |
| 75 | $values['token'], |
| 76 | true, |
| 77 | ); |
| 78 | |
| 79 | if ($user === false) { |
| 80 | return $this->unauthorized($response, _('Invalid token'), 'token'); |
| 81 | } |
| 82 | |
| 83 | return $response->json($user->array()); |
| 84 | } |
| 85 | |
| 86 | return $this->unauthorized($response, _('No or invalid auth token provided'), 'token'); |
| 87 | } |
| 88 | |
| 89 | public function token(): Response |
| 90 | { |
| 91 | $response = Response::create($this->factory); |
| 92 | $authToken = $this->auth->getAuthToken(); |
| 93 | |
| 94 | if (!$authToken) { |
| 95 | return $this->unauthorized($response, _('No auth token provided'), 'token'); |
| 96 | } |
| 97 | |
| 98 | $oneTimeToken = $this->auth->getOneTimeToken($authToken); |
| 99 | |
| 100 | if (!$oneTimeToken) { |
| 101 | return $this->unauthorized($response, _('Invalid auth token'), 'token'); |
| 102 | } |
| 103 | |
| 104 | return $response->json([ |
| 105 | 'onetimeToken' => $oneTimeToken, |
| 106 | ], 200); |
| 107 | } |
| 108 | |
| 109 | public function invalidateToken(Request $request): Response |
| 110 | { |
| 111 | $token = $request->json()['token']; |
| 112 | |
| 113 | if ($token) { |
| 114 | $this->auth->invalidateOneTimeToken($token); |
| 115 | } |
| 116 | |
| 117 | return Response::create($this->factory)->json([ |
| 118 | 'success' => true, |
| 119 | ], 200); |
| 120 | } |
| 121 | |
| 122 | protected function unauthorized(Response $response, string $message, string $loginType) |
| 123 | { |
| 124 | $response->header('WWW-Authenticate', 'Bearer realm="Cosray CMS"'); |
| 125 | |
| 126 | return $response->json([ |
| 127 | 'error' => $message, |
| 128 | 'loginType' => $loginType, |
| 129 | ], 401); |
| 130 | } |
| 131 | |
| 132 | public function logout(): array |
| 133 | { |
| 134 | $this->auth->logout(); |
| 135 | |
| 136 | return ['ok' => true]; |
| 137 | } |
| 138 | } |