Skip to content

Commit

Permalink
Improve refresh token process
Browse files Browse the repository at this point in the history
  • Loading branch information
voltan committed Dec 12, 2024
1 parent a02b2d0 commit 99cd7a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
25 changes: 14 additions & 11 deletions src/Middleware/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,22 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
// Get token
$securityStream = $request->getAttribute('security_stream');
$refreshToken = $request->getHeaderLine('refresh-token');
$token = $request->getHeaderLine('token');

// Set refresh-token to token if its be on true module and handler
$type = 'access';
if (
!empty($refreshToken)
&& isset($routeParams['module'])
&& in_array($routeParams['module'], ['user', 'company'])
&& isset($routeParams['handler'])
&& $routeParams['handler'] == 'refresh'
) {
$type = 'refresh';
$token = $refreshToken;
}

// get route match
$routeMatch = $request->getAttribute('Laminas\Router\RouteMatch');
$routeParams = $routeMatch->getParams();
Expand Down Expand Up @@ -100,17 +114,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $this->errorHandler->handle($request);
}

// Set token type
$type = 'access';
if (
isset($routeParams['module'])
&& in_array($routeParams['module'], ['user', 'company'])
&& isset($routeParams['handler'])
&& $routeParams['handler'] == 'refresh'
) {
$type = 'refresh';
}

// Check a token type
if ($tokenParsed['type'] != $type) {
$request = $request->withAttribute('status', StatusCodeInterface::STATUS_UNAUTHORIZED);
Expand Down
27 changes: 13 additions & 14 deletions src/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ public function postLoginSuccess($account, $params): array
$account['roles'] = $this->roleService->getRoleAccount((int)$account['id']);
$account['roles_full'] = $this->roleService->canonizeAccountRole($account['roles']);

// Set company data and Get company details if company module loaded
$account['is_company_setup'] = false;
$account['company_id'] = $user['authorization']['company_id'] ?? 0;
$account['company_title'] = $user['authorization']['company']['title'] ?? '';
if ($this->hasCompanyService()) {
$company = $this->companyService->getCompanyDetails((int)$account['id']);
if (!empty($company)) {
$account['company_id'] = $company['company_id'];
$account['company_title'] = $company['company_title'];
$account['is_company_setup'] = true;
}
}

// Generate access token
$accessToken = $this->tokenService->encryptToken(
[
Expand Down Expand Up @@ -353,9 +366,6 @@ public function postLoginSuccess($account, $params): array
$account['multi_factor_verify'] = $multiFactorVerify;
$account['access_token'] = $accessToken['token'];
$account['refresh_token'] = $refreshToken['token'];
$account['is_company_setup'] = false;
$account['company_id'] = $user['authorization']['company_id'] ?? 0;
$account['company_title'] = $user['authorization']['company']['title'] ?? '';
$account['permission'] = [];
$account['token_payload'] = [
'iat' => $accessToken['payload']['iat'],
Expand All @@ -372,17 +382,6 @@ public function postLoginSuccess($account, $params): array
$account['permission'] = $this->permissionService->getPermissionRole($permissionParams);
}

// Get company details if company module loaded
if ($this->hasCompanyService()) {
$company = $this->companyService->getCompanyDetails((int)$account['id']);
if (!empty($company)) {
// Set company to account if exist
$account['company_id'] = $company['company_id'];
$account['company_title'] = $company['company_title'];
$account['is_company_setup'] = true;
}
}

// Check company setup
if (!$account['is_company_setup'] && isset($this->config['login']['get_company']) && (int)$this->config['login']['get_company'] === 1) {
$isCompanySetup = false;
Expand Down

0 comments on commit 99cd7a0

Please sign in to comment.