1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Controller/v1/Auth/GetTokenController.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace App\Controller\v1\Auth; |
10
|
|
|
|
11
|
|
|
use App\Utils\JSON; |
12
|
|
|
use JsonException; |
13
|
|
|
use OpenApi\Attributes as OA; |
14
|
|
|
use OpenApi\Attributes\JsonContent; |
15
|
|
|
use OpenApi\Attributes\Property; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
20
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
21
|
|
|
use function sprintf; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class GetTokenController |
25
|
|
|
* |
26
|
|
|
* @package App\Controller\v1\Auth |
27
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
#[AsController] |
30
|
|
|
class GetTokenController |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Endpoint action to get user Json Web Token (JWT) for authentication. |
34
|
|
|
* |
35
|
|
|
* @throws HttpException |
36
|
|
|
* @throws JsonException |
37
|
|
|
*/ |
38
|
1 |
|
#[Route( |
39
|
|
|
path: '/v1/auth/get_token', |
40
|
|
|
methods: [Request::METHOD_POST], |
41
|
|
|
)] |
42
|
|
|
#[OA\RequestBody( |
43
|
|
|
request: 'body', |
44
|
|
|
description: 'Credentials object', |
45
|
|
|
required: true, |
46
|
|
|
content: new JsonContent( |
47
|
|
|
properties: [ |
48
|
|
|
new Property(property: 'username', type: 'string'), |
49
|
|
|
new Property(property: 'password', type: 'string'), |
50
|
|
|
], |
51
|
|
|
type: 'object', |
52
|
|
|
example: [ |
53
|
|
|
'username' => 'username', |
54
|
|
|
'password' => 'password', |
55
|
|
|
], |
56
|
|
|
), |
57
|
|
|
)] |
58
|
|
|
#[OA\Response( |
59
|
|
|
response: 200, |
60
|
|
|
description: 'JSON Web Token for user', |
61
|
|
|
content: new JsonContent( |
62
|
|
|
properties: [ |
63
|
|
|
new Property( |
64
|
|
|
property: 'token', |
65
|
|
|
description: 'Json Web Token', |
66
|
|
|
type: 'string', |
67
|
|
|
), |
68
|
|
|
], |
69
|
|
|
type: 'object', |
70
|
|
|
example: [ |
71
|
|
|
'token' => '_json_web_token_', |
72
|
|
|
], |
73
|
|
|
), |
74
|
|
|
)] |
75
|
|
|
#[OA\Response( |
76
|
|
|
response: 400, |
77
|
|
|
description: 'Bad Request', |
78
|
|
|
content: new JsonContent( |
79
|
|
|
properties: [ |
80
|
|
|
new Property(property: 'code', type: 'integer'), |
81
|
|
|
new Property(property: 'message', type: 'string'), |
82
|
|
|
], |
83
|
|
|
type: 'object', |
84
|
|
|
example: [ |
85
|
|
|
'code' => 400, |
86
|
|
|
'message' => 'Bad Request', |
87
|
|
|
], |
88
|
|
|
), |
89
|
|
|
)] |
90
|
|
|
#[OA\Response( |
91
|
|
|
response: 401, |
92
|
|
|
description: 'Unauthorized', |
93
|
|
|
content: new JsonContent( |
94
|
|
|
properties: [ |
95
|
|
|
new Property(property: 'code', type: 'integer'), |
96
|
|
|
new Property(property: 'message', type: 'string'), |
97
|
|
|
], |
98
|
|
|
type: 'object', |
99
|
|
|
example: [ |
100
|
|
|
'code' => 401, |
101
|
|
|
'message' => 'Bad credentials', |
102
|
|
|
], |
103
|
|
|
), |
104
|
|
|
)] |
105
|
|
|
#[OA\Tag(name: 'Authentication')] |
106
|
|
|
public function __invoke(): never |
107
|
|
|
{ |
108
|
1 |
|
$message = sprintf( |
109
|
1 |
|
'You need to send JSON body to obtain token eg. %s', |
110
|
1 |
|
JSON::encode([ |
111
|
1 |
|
'username' => 'username', |
112
|
1 |
|
'password' => 'password', |
113
|
1 |
|
]), |
114
|
1 |
|
); |
115
|
|
|
|
116
|
1 |
|
throw new HttpException(Response::HTTP_BAD_REQUEST, $message); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|