Passed
Pull Request — master (#1473)
by
unknown
45:32 queued 10:28
created

IntrospectionResponse::parseAccessTokenParams()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\OAuth2\Server\ResponseTypes;
6
7
use DateTimeInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class IntrospectionResponse implements IntrospectionResponseTypeInterface
11
{
12
    private bool $active = false;
13
14
    /**
15
     * @var non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
16
     */
17
    private ?string $tokenType = null;
18
19
    /**
20
     * @var array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
21
     */
22
    private ?array $tokenData = null;
23
24
    public function setActive(bool $active): void
25
    {
26
        $this->active = $active;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function setTokenType(string $tokenType): void
33
    {
34
        $this->tokenType = $tokenType;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function setTokenData(array $tokenData): void
41
    {
42
        $this->tokenData = $tokenData;
43
    }
44
45
    public function generateHttpResponse(ResponseInterface $response): ResponseInterface
46
    {
47
        $params = [
48
            'active' => $this->active,
49
        ];
50
51
        if ($this->active === true && $this->tokenType !== null && $this->tokenData !== null) {
52
            $params = array_merge(
53
                $params,
54
                $this->parseParams($this->tokenType, $this->tokenData),
55
                $this->getExtraParams($this->tokenType, $this->tokenData)
56
            );
57
        }
58
59
        $params = json_encode($params, flags: JSON_THROW_ON_ERROR);
60
61
        $response = $response
62
            ->withStatus(200)
63
            ->withHeader('cache-control', 'no-store')
64
            ->withHeader('content-type', 'application/json; charset=UTF-8');
65
66
        $response->getBody()->write($params);
67
68
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
69
    }
70
71
    /**
72
     * @param non-empty-string               $tokenType
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
73
     * @param array<non-empty-string, mixed> $tokenData
74
     *
75
     * @return array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
76
     */
77
    private function parseParams(string $tokenType, array $tokenData): array
78
    {
79
        return match ($tokenType) {
80
            'access_token' => $this->parseAccessTokenParams($tokenData),
81
            'refresh_token' => $this->parseRefreshTokenParams($tokenData),
82
            default => [],
83
        };
84
    }
85
86
    /**
87
     * @param array<non-empty-string, mixed> $tokenData
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
88
     *
89
     * @return array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
90
     */
91
    private function parseAccessTokenParams(array $tokenData): array
92
    {
93
        return array_filter([
94
            'scope' => $tokenData['scope'] ?? implode(' ', $tokenData['scopes'] ?? []),
95
            'client_id' => $tokenData['client_id'] ?? $tokenData['aud'][0] ?? null,
96
            'username' => $tokenData['username'] ?? null,
97
            'token_type' => 'Bearer',
98
            'exp' => isset($tokenData['exp']) ? $this->getTimestamp($tokenData['exp']) : null,
99
            'iat' => isset($tokenData['iat']) ? $this->getTimestamp($tokenData['iat']) : null,
100
            'nbf' => isset($tokenData['nbf']) ? $this->getTimestamp($tokenData['nbf']) : null,
101
            'sub' => $tokenData['sub'] ?? null,
102
            'aud' => $tokenData['aud'] ?? null,
103
            'iss' => $tokenData['iss'] ?? null,
104
            'jti' => $tokenData['jti'] ?? null,
105
        ], fn ($value) => !is_null($value));
106
    }
107
108
    /**
109
     * @param array<non-empty-string, mixed> $tokenData
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
110
     *
111
     * @return array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
112
     */
113
    private function parseRefreshTokenParams(array $tokenData): array
114
    {
115
        return array_filter([
116
            'scope' => implode(' ', $tokenData['scopes'] ?? []),
117
            'client_id' => $tokenData['client_id'] ?? null,
118
            'exp' => isset($tokenData['expire_time']) ? $this->getTimestamp($tokenData['expire_time']) : null,
119
            'sub' => $tokenData['user_id'] ?? null,
120
            'jti' => $tokenData['refresh_token_id'] ?? null,
121
        ], fn ($value) => !is_null($value));
122
    }
123
124
    private function getTimestamp(int|float|string|DateTimeInterface $value): int
125
    {
126
        return match (true) {
127
            $value instanceof DateTimeInterface => $value->getTimestamp(),
128
            default => intval($value),
129
        };
130
    }
131
132
    /**
133
     * @param non-empty-string               $tokenType
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
134
     * @param array<non-empty-string, mixed> $tokenData
135
     *
136
     * @return array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
137
     */
138
    protected function getExtraParams(string $tokenType, array $tokenData): array
139
    {
140
        return [];
141
    }
142
}
143