Failed Conditions
Push — ng ( 935f22...b3431d )
by Florent
04:01
created

theRequestHasNoScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\Scope\Tests;
15
16
use OAuth2Framework\Component\Server\Core\AccessToken\AccessToken;
17
use OAuth2Framework\Component\Server\Core\AccessToken\AccessTokenId;
18
use OAuth2Framework\Component\Server\Core\Client\Client;
19
use OAuth2Framework\Component\Server\Core\Client\ClientId;
20
use OAuth2Framework\Component\Server\Core\DataBag\DataBag;
21
use OAuth2Framework\Component\Server\Core\ResourceOwner\ResourceOwner;
22
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
23
use OAuth2Framework\Component\Server\Core\UserAccount\UserAccountId;
24
use OAuth2Framework\Component\Server\Scope\Policy\NoScopePolicy;
25
use OAuth2Framework\Component\Server\Scope\Policy\ScopePolicyManager;
26
use OAuth2Framework\Component\Server\Scope\Scope;
27
use OAuth2Framework\Component\Server\Scope\ScopeRepository;
28
use OAuth2Framework\Component\Server\Scope\TokenEndpointScopeExtension;
29
use OAuth2Framework\Component\Server\TokenEndpoint\GrantType;
30
use OAuth2Framework\Component\Server\TokenEndpoint\GrantTypeData;
31
use PHPUnit\Framework\TestCase;
32
use Psr\Http\Message\ServerRequestInterface;
33
34
/**
35
 * @group TokenEndpointScopeExtension
36
 */
37
final class TokenEndpointScopeExtensionTest extends TestCase
38
{
39
    /**
40
     * @test
41
     */
42
    public function theRequestHasNoScope()
43
    {
44
        $client = Client::createEmpty();
45
        $client = $client->create(
46
            ClientId::create('CLIENT_ID'),
47
            DataBag::create([]),
48
            UserAccountId::create('USER_ACCOUNT_ID')
49
        );
50
        $client->eraseMessages();
51
52
        $request = $this->prophesize(ServerRequestInterface::class);
53
        $request->getParsedBody()->willReturn([]);
54
        $grantTypeData = GrantTypeData::create($client);
55
        $grantType = $this->prophesize(GrantType::class);
56
        $next = function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData {
0 ignored issues
show
Unused Code introduced by
The parameter $grantType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
            return $grantTypeData;
58
        };
59
60
        $result = $this->getExtension()->beforeAccessTokenIssuance($request->reveal(), $grantTypeData, $grantType->reveal(), $next);
61
        self::assertSame($grantTypeData, $result);
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function theRequestedScopeIsNotSupported()
68
    {
69
        $client = Client::createEmpty();
70
        $client = $client->create(
71
            ClientId::create('CLIENT_ID'),
72
            DataBag::create([]),
73
            UserAccountId::create('USER_ACCOUNT_ID')
74
        );
75
        $client->eraseMessages();
76
77
        $request = $this->prophesize(ServerRequestInterface::class);
78
        $request->getParsedBody()->willReturn([
79
            'scope' => 'café',
80
        ]);
81
        $grantTypeData = GrantTypeData::create($client);
82
        $grantType = $this->prophesize(GrantType::class);
83
        $next = function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData {
0 ignored issues
show
Unused Code introduced by
The parameter $grantType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
            return $grantTypeData;
85
        };
86
87
        try {
88
            $this->getExtension()->beforeAccessTokenIssuance($request->reveal(), $grantTypeData, $grantType->reveal(), $next);
89
        } catch (OAuth2Exception $e) {
90
            self::assertEquals(400, $e->getCode());
91
            self::assertEquals([
92
                'error' => 'invalid_scope',
93
                'error_description' => 'An unsupported scope was requested. Available scope is/are: scope1 ,scope2.',
94
            ], $e->getData());
95
        }
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function theRequestedScopeIsValid()
102
    {
103
        $client = Client::createEmpty();
104
        $client = $client->create(
105
            ClientId::create('CLIENT_ID'),
106
            DataBag::create([]),
107
            UserAccountId::create('USER_ACCOUNT_ID')
108
        );
109
        $client->eraseMessages();
110
111
        $request = $this->prophesize(ServerRequestInterface::class);
112
        $request->getParsedBody()->willReturn([
113
            'scope' => 'scope2 scope1',
114
        ]);
115
        $grantTypeData = GrantTypeData::create($client);
116
        $grantType = $this->prophesize(GrantType::class);
117
        $next = function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData {
0 ignored issues
show
Unused Code introduced by
The parameter $grantType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
            return $grantTypeData;
119
        };
120
121
        $result = $this->getExtension()->beforeAccessTokenIssuance($request->reveal(), $grantTypeData, $grantType->reveal(), $next);
122
        self::assertNotSame($grantTypeData, $result);
123
        self::assertTrue($result->hasParameter('scope'));
124
        self::assertEquals('scope2 scope1', $result->getParameter('scope'));
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function after()
131
    {
132
        $client = Client::createEmpty();
133
        $client = $client->create(
134
            ClientId::create('CLIENT_ID'),
135
            DataBag::create([]),
136
            UserAccountId::create('USER_ACCOUNT_ID')
137
        );
138
        $client->eraseMessages();
139
        $accessToken = AccessToken::createEmpty();
140
        $accessToken = $accessToken->create(
141
            AccessTokenId::create('ACCESS_TOKEN_ID'),
142
            $client->getPublicId(),
143
            $client->getPublicId(),
144
            DataBag::create([]),
145
            DataBag::create([]),
146
            new \DateTimeImmutable('now +1 hour'),
147
            null
148
        );
149
        $accessToken->eraseMessages();
150
151
        $next = function (Client $client, ResourceOwner $resourceOwner, AccessToken $accessToken): array {
152
            return $accessToken->getResponseData();
153
        };
154
155
        $result = $this->getExtension()->afterAccessTokenIssuance($client, $client, $accessToken, $next);
156
        self::assertEquals(2, count($result));
157
    }
158
159
    /**
160
     * @var null|TokenEndpointScopeExtension
161
     */
162
    private $extension = null;
163
164
    /**
165
     * @return TokenEndpointScopeExtension
166
     */
167
    private function getExtension(): TokenEndpointScopeExtension
168
    {
169
        if (null === $this->extension) {
170
            $scope1 = $this->prophesize(Scope::class);
171
            $scope1->name()->willReturn('scope1');
172
            $scope1->__toString()->willReturn('scope1');
173
            $scope2 = $this->prophesize(Scope::class);
174
            $scope2->name()->willReturn('scope2');
175
            $scope2->__toString()->willReturn('scope2');
176
            $scopeRepository = $this->prophesize(ScopeRepository::class);
177
            $scopeRepository->all()->willReturn([
178
                $scope1->reveal(),
179
                $scope2->reveal(),
180
            ]);
181
182
            $scopePolicyManager = new ScopePolicyManager();
183
            $scopePolicyManager->add(new NoScopePolicy(), true);
184
185
            $this->extension = new TokenEndpointScopeExtension(
186
                $scopeRepository->reveal(),
187
                $scopePolicyManager
188
            );
189
        }
190
191
        return $this->extension;
192
    }
193
}
194