Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
LoginEndpoint
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Olz\Common\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6use Olz\Exceptions\AuthBlockedException;
7use Olz\Exceptions\InvalidCredentialsException;
8
9/**
10 * @extends OlzTypedEndpoint<
11 *   array{
12 *     usernameOrEmail: non-empty-string,
13 *     password: non-empty-string,
14 *   },
15 *   array{
16 *     status: 'AUTHENTICATED'|'INVALID_CREDENTIALS'|'BLOCKED',
17 *     numRemainingAttempts: ?int<0, max>,
18 *   }
19 * >
20 */
21class LoginEndpoint extends OlzTypedEndpoint {
22    protected function handle(mixed $input): mixed {
23        $username_or_email = trim($input['usernameOrEmail']);
24        $password = $input['password'];
25
26        try {
27            $user = $this->authUtils()->authenticate($username_or_email, $password);
28        } catch (AuthBlockedException $exc) {
29            return [
30                'status' => 'BLOCKED',
31                'numRemainingAttempts' => 0,
32            ];
33        } catch (InvalidCredentialsException $exc) {
34            return [
35                'status' => 'INVALID_CREDENTIALS',
36                'numRemainingAttempts' => $exc->getNumRemainingAttempts(),
37            ];
38        }
39
40        $now_datetime = new \DateTime($this->dateUtils()->getIsoNow());
41        $user->setLastLoginAt($now_datetime);
42        $this->entityManager()->flush();
43
44        $this->session()->resetConfigure([
45            'timeout' => 43200, // 12 hours
46        ]);
47
48        $this->authUtils()->setSessionUser($user);
49        $this->authUtils()->setSessionAuthUser($user);
50        $refresh_token = $this->authUtils()->issueRefreshToken($user);
51        $this->getResponse()->headers->setCookie(
52            $this->authUtils()->createRefreshTokenCookie($refresh_token)
53        );
54        return [
55            'status' => 'AUTHENTICATED',
56            'numRemainingAttempts' => null,
57        ];
58    }
59}