-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAgentAuthController.php
More file actions
155 lines (136 loc) · 4.54 KB
/
Copy pathAgentAuthController.php
File metadata and controls
155 lines (136 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace App\Http\Controllers;
use App\Models\Agent;
use App\Models\MainSettings;
use App\Services\AgentService;
use App\Services\MainSettingsService;
use App\Utils\DemoCompany;
use Dedoc\Scramble\Attributes\BodyParameter;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Tymon\JWTAuth\JWTGuard;
#[Group('AgentApp - Auth', 'Responsible for AgentAPP-API-Call authentications.', weight: 20)]
class AgentAuthController extends Controller {
/**
* Create a new AuthController instance.
*/
public function __construct(
private AgentService $agentService,
private MainSettingsService $mainSettingsService,
) {}
/**
* Get the JWT authentication guard.
*/
protected function guard(): JWTGuard {
/** @var JWTGuard $guard */
$guard = auth()->guard('agent_api');
return $guard;
}
/**
* Agent login.
*
* Login a user of the Agent App and get JWT token via given credentials.
*
* @bodyParam email string required
* @bodyParam password string required
*/
#[BodyParameter('email', type: 'string', format: 'email', example: DemoCompany::DEMO_COMPANY_AGENT_EMAIL)]
#[BodyParameter('password', type: 'string', format: 'password', example: DemoCompany::DEMO_COMPANY_PASSWORD)]
public function login(Request $request): JsonResponse {
$credentials = $request->only(['email', 'password']);
if (!$token = $this->guard()->setTTL(525600)->attempt($credentials)) {
return response()->json(['data' => ['message' => 'Unauthorized', 'status' => 401]], 401);
}
// if the Agent app sends us a agent-device-id in the header
// we update the agent in db
$deviceId = $request->header('device-id');
if ($deviceId) {
$agentId = $this->guard()->user()->id;
$agent = $this->agentService->getById($agentId);
$this->agentService->updateDevice($agent, $deviceId);
}
return $this->respondWithToken($token);
}
/**
* Get the authenticated Agent.
*/
public function me(): JsonResponse {
$agent = auth('agent_api')->user();
if (method_exists($agent, 'getRoleNames')) {
/** @var Agent $agent */
$roles = $agent->getRoleNames()->toArray();
} else {
$roles = [];
}
if (method_exists($agent, 'getAllPermissions')) {
/** @var Agent $agent */
$permissions = $agent->getAllPermissions()->pluck('name')->toArray();
} else {
$permissions = [];
}
return response()->json([
/* @var Agent */
'agent' => $agent,
'roles' => $roles,
'permissions' => $permissions,
'settings' => $this->mainSettings(),
]);
}
/**
* Agent logout.
*
* Logout the AgentApp user and invalidate the JWT token.
*/
public function logout(): JsonResponse {
auth('agent_api')->logout();
return response()->json(['message' => 'Successfully logged out']);
}
/**
* Refresh Agent token.
*
* A valid JWT token has to be sent to refresh the token.
*
* @return JsonResponse
*/
public function refresh() {
return $this->respondWithToken($this->guard()->refresh());
}
/**
* Get the token array structure.
*
* @param string $token
*
* @return JsonResponse
*/
protected function respondWithToken($token) {
/** @var Agent $agent */
$agent = $this->guard()->user();
$roles = $agent->getRoleNames()->toArray();
$permissions = $agent->getAllPermissions()->pluck('name')->toArray();
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => $this->guard()->factory()->getTTL() * 60,
'agent' => $agent,
'roles' => $roles,
'permissions' => $permissions,
'settings' => $this->mainSettings(),
]);
}
/**
* @return array<string, mixed>|null
*/
private function mainSettings(): ?array {
$settings = $this->mainSettingsService->getAll()->first();
if (!$settings instanceof MainSettings) {
return null;
}
return [
'currency' => $settings->currency,
'country' => $settings->country,
'language' => $settings->language,
'company_name' => $settings->company_name,
];
}
}