Passed
Pull Request — master (#1473)
by
unknown
35:03
created

IntrospectionResponse::parseParams()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 26
rs 8.6346
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
    protected 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
    protected ?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
    protected ?array $token = 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 setToken(array $token): void
41
    {
42
        $this->token = $token;
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->token !== null) {
52
            $params = array_merge(
53
                $params,
54
                $this->parseParams($this->tokenType, $this->token),
55
                $this->getExtraParams($this->tokenType, $this->token)
56
            );
57
        }
58
59
        $params = json_encode($params, flags: JSON_THROW_ON_ERROR);
60
61
        $response = $response
62
            ->withStatus(200)
63
            ->withHeader('pragma', 'no-cache')
64
            ->withHeader('cache-control', 'no-store')
65
            ->withHeader('content-type', 'application/json; charset=UTF-8');
66
67
        $response->getBody()->write($params);
68
69
        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...
70
    }
71
72
    /**
73
     * @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...
74
     * @param array<non-empty-string, mixed> $token
75
     *
76
     * @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...
77
     */
78
    protected function parseParams(string $tokenType, array $token): array
79
    {
80
        if ($tokenType === 'access_token') {
81
            return array_filter([
82
                'scope' => $token['scope'] ?? implode(' ', $token['scopes'] ?? []),
83
                'client_id' => $token['client_id'] ?? $token['aud'][0] ?? null,
84
                'username' => $token['username'] ?? null,
85
                'token_type' => 'Bearer',
86
                'exp' => isset($token['exp']) ? $this->convertTimestamp($token['exp']) : null,
87
                'iat' => isset($token['iat']) ? $this->convertTimestamp($token['iat']) : null,
88
                'nbf' => isset($token['nbf']) ? $this->convertTimestamp($token['nbf']) : null,
89
                'sub' => $token['sub'] ?? null,
90
                'aud' => $token['aud'] ?? null,
91
                'iss' => $token['iss'] ?? null,
92
                'jti' => $token['jti'] ?? null,
93
            ]);
94
        } elseif ($tokenType === 'refresh_token') {
95
            return array_filter([
96
                'scope' => implode(' ', $token['scopes'] ?? []),
97
                'client_id' => $token['client_id'] ?? null,
98
                'exp' => isset($token['expire_time']) ? $this->convertTimestamp($token['expire_time']) : null,
99
                'sub' => $token['user_id'] ?? null,
100
                'jti' => $token['refresh_token_id'] ?? null,
101
            ]);
102
        } else {
103
            return [];
104
        }
105
    }
106
107
    protected function convertTimestamp(int|float|string|DateTimeInterface $value): int
108
    {
109
        return match (true) {
110
            $value instanceof DateTimeInterface => $value->getTimestamp(),
111
            default => intval($value),
112
        };
113
    }
114
115
    /**
116
     * @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...
117
     * @param array<non-empty-string, mixed> $token
118
     *
119
     * @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...
120
     */
121
    protected function getExtraParams(string $tokenType, array $token): array
122
    {
123
        return [];
124
    }
125
}
126