mirror of
				https://github.com/elyby/oauth2-server.git
				synced 2025-05-31 14:12:07 +05:30 
			
		
		
		
	Implemented RFC7636. Fixes #574
This commit is contained in:
		@@ -27,7 +27,7 @@ use Zend\Diactoros\ServerRequest;
 | 
			
		||||
class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * CryptTrait stub
 | 
			
		||||
     * @var CryptTraitStub
 | 
			
		||||
     */
 | 
			
		||||
    protected $cryptStub;
 | 
			
		||||
 | 
			
		||||
@@ -136,6 +136,41 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
        $this->assertTrue($grant->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function testValidateAuthorizationRequestCodeChallenge()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            null,
 | 
			
		||||
            'php://input',
 | 
			
		||||
            $headers = [],
 | 
			
		||||
            $cookies = [],
 | 
			
		||||
            $queryParams = [
 | 
			
		||||
                'response_type'  => 'code',
 | 
			
		||||
                'client_id'      => 'foo',
 | 
			
		||||
                'redirect_uri'   => 'http://foo/bar',
 | 
			
		||||
                'code_challenge' => 'FOOBAR',
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue($grant->validateAuthorizationRequest($request) instanceof AuthorizationRequest);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @expectedException \League\OAuth2\Server\Exception\OAuthServerException
 | 
			
		||||
     * @expectedExceptionCode 3
 | 
			
		||||
@@ -272,6 +307,82 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
        $grant->validateAuthorizationRequest($request);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @expectedException \League\OAuth2\Server\Exception\OAuthServerException
 | 
			
		||||
     * @expectedExceptionCode 3
 | 
			
		||||
     */
 | 
			
		||||
    public function testValidateAuthorizationRequestMissingCodeChallenge()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            null,
 | 
			
		||||
            'php://input',
 | 
			
		||||
            $headers = [],
 | 
			
		||||
            $cookies = [],
 | 
			
		||||
            $queryParams = [
 | 
			
		||||
                'response_type' => 'code',
 | 
			
		||||
                'client_id'     => 'foo',
 | 
			
		||||
                'redirect_uri'  => 'http://foo/bar',
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $grant->validateAuthorizationRequest($request);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @expectedException \League\OAuth2\Server\Exception\OAuthServerException
 | 
			
		||||
     * @expectedExceptionCode 3
 | 
			
		||||
     */
 | 
			
		||||
    public function testValidateAuthorizationRequestInvalidCodeChallengeMethod()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            null,
 | 
			
		||||
            'php://input',
 | 
			
		||||
            $headers = [],
 | 
			
		||||
            $cookies = [],
 | 
			
		||||
            $queryParams = [
 | 
			
		||||
                'response_type'         => 'code',
 | 
			
		||||
                'client_id'             => 'foo',
 | 
			
		||||
                'redirect_uri'          => 'http://foo/bar',
 | 
			
		||||
                'code_challenge'        => 'foobar',
 | 
			
		||||
                'code_challenge_method' => 'foo',
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $grant->validateAuthorizationRequest($request);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testCompleteAuthorizationRequest()
 | 
			
		||||
    {
 | 
			
		||||
        $authRequest = new AuthorizationRequest();
 | 
			
		||||
@@ -390,6 +501,150 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
        $this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testRespondToAccessTokenRequestCodeChallengePlain()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setIdentifier('foo');
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
 | 
			
		||||
        $scopeEntity = new ScopeEntity();
 | 
			
		||||
        $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
 | 
			
		||||
        $scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
 | 
			
		||||
 | 
			
		||||
        $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
 | 
			
		||||
        $accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
 | 
			
		||||
 | 
			
		||||
        $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
        $grant->setScopeRepository($scopeRepositoryMock);
 | 
			
		||||
        $grant->setAccessTokenRepository($accessTokenRepositoryMock);
 | 
			
		||||
        $grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
 | 
			
		||||
        $grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
 | 
			
		||||
        $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            'POST',
 | 
			
		||||
            'php://input',
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [
 | 
			
		||||
                'grant_type'    => 'authorization_code',
 | 
			
		||||
                'client_id'     => 'foo',
 | 
			
		||||
                'redirect_uri'  => 'http://foo/bar',
 | 
			
		||||
                'code_verifier' => 'foobar',
 | 
			
		||||
                'code'          => $this->cryptStub->doEncrypt(
 | 
			
		||||
                    json_encode(
 | 
			
		||||
                        [
 | 
			
		||||
                            'auth_code_id'          => uniqid(),
 | 
			
		||||
                            'expire_time'           => time() + 3600,
 | 
			
		||||
                            'client_id'             => 'foo',
 | 
			
		||||
                            'user_id'               => 123,
 | 
			
		||||
                            'scopes'                => ['foo'],
 | 
			
		||||
                            'redirect_uri'          => 'http://foo/bar',
 | 
			
		||||
                            'code_challenge'        => 'foobar',
 | 
			
		||||
                            'code_challenge_method' => 'plain',
 | 
			
		||||
                        ]
 | 
			
		||||
                    )
 | 
			
		||||
                ),
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        /** @var StubResponseType $response */
 | 
			
		||||
        $response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
 | 
			
		||||
        $this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testRespondToAccessTokenRequestCodeChallengeS256()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setIdentifier('foo');
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
 | 
			
		||||
        $scopeEntity = new ScopeEntity();
 | 
			
		||||
        $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
 | 
			
		||||
        $scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
 | 
			
		||||
 | 
			
		||||
        $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
 | 
			
		||||
        $accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
 | 
			
		||||
 | 
			
		||||
        $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
        $grant->setScopeRepository($scopeRepositoryMock);
 | 
			
		||||
        $grant->setAccessTokenRepository($accessTokenRepositoryMock);
 | 
			
		||||
        $grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
 | 
			
		||||
        $grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
 | 
			
		||||
        $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            'POST',
 | 
			
		||||
            'php://input',
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [
 | 
			
		||||
                'grant_type'    => 'authorization_code',
 | 
			
		||||
                'client_id'     => 'foo',
 | 
			
		||||
                'redirect_uri'  => 'http://foo/bar',
 | 
			
		||||
                'code_verifier' => 'foobar',
 | 
			
		||||
                'code'          => $this->cryptStub->doEncrypt(
 | 
			
		||||
                    json_encode(
 | 
			
		||||
                        [
 | 
			
		||||
                            'auth_code_id'          => uniqid(),
 | 
			
		||||
                            'expire_time'           => time() + 3600,
 | 
			
		||||
                            'client_id'             => 'foo',
 | 
			
		||||
                            'user_id'               => 123,
 | 
			
		||||
                            'scopes'                => ['foo'],
 | 
			
		||||
                            'redirect_uri'          => 'http://foo/bar',
 | 
			
		||||
                            'code_challenge'        => urlencode(base64_encode(hash('sha256', 'foobar'))),
 | 
			
		||||
                            'code_challenge_method' => 'S256',
 | 
			
		||||
                        ]
 | 
			
		||||
                    )
 | 
			
		||||
                ),
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        /** @var StubResponseType $response */
 | 
			
		||||
        $response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue($response->getAccessToken() instanceof AccessTokenEntityInterface);
 | 
			
		||||
        $this->assertTrue($response->getRefreshToken() instanceof RefreshTokenEntityInterface);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @expectedException \League\OAuth2\Server\Exception\OAuthServerException
 | 
			
		||||
     * @expectedExceptionCode 3
 | 
			
		||||
@@ -710,4 +965,222 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
            $this->assertEquals($e->getHint(), 'Cannot decrypt the authorization code');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    public function testRespondToAccessTokenRequestBadCodeVerifierPlain()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setIdentifier('foo');
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
 | 
			
		||||
        $scopeEntity = new ScopeEntity();
 | 
			
		||||
        $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
 | 
			
		||||
        $scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
 | 
			
		||||
 | 
			
		||||
        $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
 | 
			
		||||
        $accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
 | 
			
		||||
 | 
			
		||||
        $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
        $grant->setAccessTokenRepository($accessTokenRepositoryMock);
 | 
			
		||||
        $grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
 | 
			
		||||
        $grant->setScopeRepository($scopeRepositoryMock);
 | 
			
		||||
        $grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
 | 
			
		||||
        $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            'POST',
 | 
			
		||||
            'php://input',
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [
 | 
			
		||||
                'grant_type'    => 'authorization_code',
 | 
			
		||||
                'client_id'     => 'foo',
 | 
			
		||||
                'redirect_uri'  => 'http://foo/bar',
 | 
			
		||||
                'code_verifier' => 'nope',
 | 
			
		||||
                'code'          => $this->cryptStub->doEncrypt(
 | 
			
		||||
                    json_encode(
 | 
			
		||||
                        [
 | 
			
		||||
                            'auth_code_id'          => uniqid(),
 | 
			
		||||
                            'expire_time'           => time() + 3600,
 | 
			
		||||
                            'client_id'             => 'foo',
 | 
			
		||||
                            'user_id'               => 123,
 | 
			
		||||
                            'scopes'                => ['foo'],
 | 
			
		||||
                            'redirect_uri'          => 'http://foo/bar',
 | 
			
		||||
                            'code_challenge'        => 'foobar',
 | 
			
		||||
                            'code_challenge_method' => 'plain',
 | 
			
		||||
                        ]
 | 
			
		||||
                    )
 | 
			
		||||
                ),
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            /* @var StubResponseType $response */
 | 
			
		||||
            $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
 | 
			
		||||
        } catch (OAuthServerException $e) {
 | 
			
		||||
            $this->assertEquals($e->getHint(), 'Failed to verify `code_verifier`.');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function testRespondToAccessTokenRequestBadCodeVerifierS256()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setIdentifier('foo');
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
 | 
			
		||||
        $scopeEntity = new ScopeEntity();
 | 
			
		||||
        $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
 | 
			
		||||
        $scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
 | 
			
		||||
 | 
			
		||||
        $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
 | 
			
		||||
        $accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
 | 
			
		||||
 | 
			
		||||
        $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
        $grant->setAccessTokenRepository($accessTokenRepositoryMock);
 | 
			
		||||
        $grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
 | 
			
		||||
        $grant->setScopeRepository($scopeRepositoryMock);
 | 
			
		||||
        $grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
 | 
			
		||||
        $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            'POST',
 | 
			
		||||
            'php://input',
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [
 | 
			
		||||
                'grant_type'    => 'authorization_code',
 | 
			
		||||
                'client_id'     => 'foo',
 | 
			
		||||
                'redirect_uri'  => 'http://foo/bar',
 | 
			
		||||
                'code_verifier' => 'nope',
 | 
			
		||||
                'code'          => $this->cryptStub->doEncrypt(
 | 
			
		||||
                    json_encode(
 | 
			
		||||
                        [
 | 
			
		||||
                            'auth_code_id'          => uniqid(),
 | 
			
		||||
                            'expire_time'           => time() + 3600,
 | 
			
		||||
                            'client_id'             => 'foo',
 | 
			
		||||
                            'user_id'               => 123,
 | 
			
		||||
                            'scopes'                => ['foo'],
 | 
			
		||||
                            'redirect_uri'          => 'http://foo/bar',
 | 
			
		||||
                            'code_challenge'        => 'foobar',
 | 
			
		||||
                            'code_challenge_method' => 'S256',
 | 
			
		||||
                        ]
 | 
			
		||||
                    )
 | 
			
		||||
                ),
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            /* @var StubResponseType $response */
 | 
			
		||||
            $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
 | 
			
		||||
        } catch (OAuthServerException $e) {
 | 
			
		||||
            $this->assertEquals($e->getHint(), 'Failed to verify `code_verifier`.');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testRespondToAccessTokenRequestMissingCodeVerifier()
 | 
			
		||||
    {
 | 
			
		||||
        $client = new ClientEntity();
 | 
			
		||||
        $client->setIdentifier('foo');
 | 
			
		||||
        $client->setRedirectUri('http://foo/bar');
 | 
			
		||||
        $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
 | 
			
		||||
        $clientRepositoryMock->method('getClientEntity')->willReturn($client);
 | 
			
		||||
 | 
			
		||||
        $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
 | 
			
		||||
        $scopeEntity = new ScopeEntity();
 | 
			
		||||
        $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
 | 
			
		||||
        $scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
 | 
			
		||||
 | 
			
		||||
        $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
 | 
			
		||||
        $accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf();
 | 
			
		||||
 | 
			
		||||
        $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf();
 | 
			
		||||
        $refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity());
 | 
			
		||||
 | 
			
		||||
        $grant = new AuthCodeGrant(
 | 
			
		||||
            $this->getMock(AuthCodeRepositoryInterface::class),
 | 
			
		||||
            $this->getMock(RefreshTokenRepositoryInterface::class),
 | 
			
		||||
            new \DateInterval('PT10M')
 | 
			
		||||
        );
 | 
			
		||||
        $grant->enableCodeExchangeProof();
 | 
			
		||||
        $grant->setClientRepository($clientRepositoryMock);
 | 
			
		||||
        $grant->setAccessTokenRepository($accessTokenRepositoryMock);
 | 
			
		||||
        $grant->setRefreshTokenRepository($refreshTokenRepositoryMock);
 | 
			
		||||
        $grant->setScopeRepository($scopeRepositoryMock);
 | 
			
		||||
        $grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));
 | 
			
		||||
        $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
 | 
			
		||||
 | 
			
		||||
        $request = new ServerRequest(
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            null,
 | 
			
		||||
            'POST',
 | 
			
		||||
            'php://input',
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [],
 | 
			
		||||
            [
 | 
			
		||||
                'grant_type'   => 'authorization_code',
 | 
			
		||||
                'client_id'    => 'foo',
 | 
			
		||||
                'redirect_uri' => 'http://foo/bar',
 | 
			
		||||
                'code'         => $this->cryptStub->doEncrypt(
 | 
			
		||||
                    json_encode(
 | 
			
		||||
                        [
 | 
			
		||||
                            'auth_code_id'          => uniqid(),
 | 
			
		||||
                            'expire_time'           => time() + 3600,
 | 
			
		||||
                            'client_id'             => 'foo',
 | 
			
		||||
                            'user_id'               => 123,
 | 
			
		||||
                            'scopes'                => ['foo'],
 | 
			
		||||
                            'redirect_uri'          => 'http://foo/bar',
 | 
			
		||||
                            'code_challenge'        => 'foobar',
 | 
			
		||||
                            'code_challenge_method' => 'plain',
 | 
			
		||||
                        ]
 | 
			
		||||
                    )
 | 
			
		||||
                ),
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            /* @var StubResponseType $response */
 | 
			
		||||
            $grant->respondToAccessTokenRequest($request, new StubResponseType(), new \DateInterval('PT10M'));
 | 
			
		||||
        } catch (OAuthServerException $e) {
 | 
			
		||||
            $this->assertEquals($e->getHint(), 'Check the `code_verifier` parameter');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user