|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RestTemplate\Rest; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\RestServer\Exception\Error401Exception; |
|
6
|
|
|
|
|
7
|
|
|
class Login extends ServiceAbstractBase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Do login |
|
11
|
|
|
* |
|
12
|
|
|
* @SWG\Post( |
|
13
|
|
|
* path="/login", |
|
14
|
|
|
* operationId="post", |
|
15
|
|
|
* tags={"login"}, |
|
16
|
|
|
* @SWG\Parameter( |
|
17
|
|
|
* name="body", |
|
18
|
|
|
* in="body", |
|
19
|
|
|
* description="The login data", |
|
20
|
|
|
* required=true, |
|
21
|
|
|
* @SWG\Schema( |
|
22
|
|
|
* required={"username","password"}, |
|
23
|
|
|
* @SWG\Property(property="username", type="string", description="The username"), |
|
24
|
|
|
* @SWG\Property(property="password", type="string", description="The password"), |
|
25
|
|
|
* ) |
|
26
|
|
|
* ), |
|
27
|
|
|
* @SWG\Response( |
|
28
|
|
|
* response=200, |
|
29
|
|
|
* description="The object", |
|
30
|
|
|
* @SWG\Schema( |
|
31
|
|
|
* required={"token","properties"}, |
|
32
|
|
|
* @SWG\Property(property="token", type="string"), |
|
33
|
|
|
* @SWG\Property(property="properties", type="array") |
|
34
|
|
|
* ) |
|
35
|
|
|
* ), |
|
36
|
|
|
* @SWG\Response( |
|
37
|
|
|
* response=401, |
|
38
|
|
|
* description="Não autorizado", |
|
39
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
40
|
|
|
* ), |
|
41
|
|
|
* @SWG\Response( |
|
42
|
|
|
* response=500, |
|
43
|
|
|
* description="Erro Geral", |
|
44
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
45
|
|
|
* ) |
|
46
|
|
|
* ) |
|
47
|
|
|
*/ |
|
48
|
|
|
public function post() |
|
49
|
|
|
{ |
|
50
|
|
|
$json = json_decode($this->getRequest()->payload()); |
|
51
|
|
|
|
|
52
|
|
|
if ($json->username != 'admin' || $json->password != 'pwd') { |
|
53
|
|
|
throw new Error401Exception('Username or password is invalid'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$metadata = [ |
|
57
|
|
|
'role' => 'admin', |
|
58
|
|
|
'userid' => '1234567890ABCDEF0123456789', |
|
59
|
|
|
'name' => 'João Gilberto' |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
$token = $this->createToken($metadata); |
|
63
|
|
|
|
|
64
|
|
|
$this->getResponse()->write(['token' => $token]); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|