OAuth2Authentication::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace kalanis\Restful\Security\Process;
4
5
6
use kalanis\Restful\Http\IInput;
7
use kalanis\Restful\Security\Exceptions\AuthenticationException;
8
use kalanis\OAuth2;
9
use kalanis\OAuth2\Storage\AccessTokens\AccessTokenFacade;
10
use kalanis\OAuth2\Storage\Exceptions\InvalidAccessTokenException;
11
use kalanis\OAuth2\Storage\ITokens;
12
13
14
/**
15
 * OAuth2Authentication
16
 * @package kalanis\Restful\Security\Process
17
 */
18 1
class OAuth2Authentication extends AuthenticationProcess
19
{
20
21 1
    public function __construct(
22
        private readonly AccessTokenFacade  $storage,
23
        private readonly OAuth2\Http\IInput $oauthInput,
24
    )
25
    {
26 1
    }
27
28
    /**
29
     * Authenticate request data
30
     * @param IInput $input
31
     * @throws AuthenticationException
32
     * @return bool
33
     */
34
    protected function authRequestData(IInput $input): bool
35
    {
36 1
        $token = $this->oauthInput->getAuthorization();
37 1
        if (!$token) {
38 1
            throw new AuthenticationException('Token was not found.');
39
        }
40 1
        return true;
41
    }
42
43
    /**
44
     * Authenticate request timeout
45
     * @param IInput $input
46
     * @throws AuthenticationException
47
     * @return bool
48
     */
49
    protected function authRequestTimeout(IInput $input): bool
50
    {
51
        try {
52 1
            $this->getAccessToken();
53 1
        } catch (InvalidAccessTokenException $e) {
54 1
            throw new AuthenticationException('Invalid or expired access token.', 0, $e);
55
        }
56 1
        return true;
57
    }
58
59
    /**
60
     * Get access token
61
     * @throws InvalidAccessTokenException
62
     * @return ITokens|null
63
     */
64
    public function getAccessToken(): ?ITokens
65
    {
66 1
        $token = $this->oauthInput->getAuthorization();
67 1
        return $token ? $this->storage->getEntity($token) : null;
68
    }
69
}
70