getMock(ClientRepositoryInterface::class), $this->getMock(AccessTokenRepositoryInterface::class), $this->getMock(ScopeRepositoryInterface::class), 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key', new StubResponseType() ); $server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M')); try { $server->respondToAccessTokenRequest(ServerRequestFactory::fromGlobals(), new Response); } catch (OAuthServerException $e) { $this->assertEquals('unsupported_grant_type', $e->getErrorType()); $this->assertEquals(400, $e->getHttpStatusCode()); } } public function testRespondToRequest() { $clientRepository = $this->getMock(ClientRepositoryInterface::class); $clientRepository->method('getClientEntity')->willReturn(new ClientEntity()); $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(); $scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0); $accessTokenRepositoryMock = $this->getMock(AccessTokenRepositoryInterface::class); $accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity()); $server = new Server( $clientRepository, $accessTokenRepositoryMock, $scopeRepositoryMock, 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key', new StubResponseType() ); $server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M')); $_POST['grant_type'] = 'client_credentials'; $_POST['client_id'] = 'foo'; $_POST['client_secret'] = 'bar'; $response = $server->respondToAccessTokenRequest(ServerRequestFactory::fromGlobals(), new Response); $this->assertEquals(200, $response->getStatusCode()); } public function testGetResponseType() { $clientRepository = $this->getMock(ClientRepositoryInterface::class); $server = new Server( $clientRepository, $this->getMock(AccessTokenRepositoryInterface::class), $this->getMock(ScopeRepositoryInterface::class), 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key' ); $abstractGrantReflection = new \ReflectionClass($server); $method = $abstractGrantReflection->getMethod('getResponseType'); $method->setAccessible(true); $this->assertTrue($method->invoke($server) instanceof BearerTokenResponse); } public function testValidateAuthenticatedRequest() { $clientRepository = $this->getMock(ClientRepositoryInterface::class); $server = new Server( $clientRepository, $this->getMock(AccessTokenRepositoryInterface::class), $this->getMock(ScopeRepositoryInterface::class), 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key' ); try { $server->validateAuthenticatedRequest(ServerRequestFactory::fromGlobals()); } catch (OAuthServerException $e) { $this->assertEquals('Missing "Authorization" header', $e->getHint()); } } public function testCompleteAuthorizationRequest() { $clientRepository = $this->getMock(ClientRepositoryInterface::class); $server = new Server( $clientRepository, $this->getMock(AccessTokenRepositoryInterface::class), $this->getMock(ScopeRepositoryInterface::class), 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key' ); $authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock(); $authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity()); $grant = new AuthCodeGrant( $authCodeRepository, $this->getMock(RefreshTokenRepositoryInterface::class), $this->getMock(UserRepositoryInterface::class), new \DateInterval('PT10M') ); $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/Stubs/private.key')); $grant->setPublicKey(new CryptKey('file://' . __DIR__ . '/Stubs/public.key')); $server->enableGrantType($grant); $authRequest = new AuthorizationRequest(); $authRequest->setAuthorizationApproved(true); $authRequest->setClient(new ClientEntity()); $authRequest->setGrantTypeId('authorization_code'); $authRequest->setUser(new UserEntity()); $this->assertTrue( $server->completeAuthorizationRequest($authRequest, new Response) instanceof ResponseInterface ); } public function testValidateAuthorizationRequest() { $client = new ClientEntity(); $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); $clientRepositoryMock->method('getClientEntity')->willReturn($client); $grant = new AuthCodeGrant( $this->getMock(AuthCodeRepositoryInterface::class), $this->getMock(RefreshTokenRepositoryInterface::class), $this->getMock(UserRepositoryInterface::class), new \DateInterval('PT10M') ); $grant->setClientRepository($clientRepositoryMock); $server = new Server( $clientRepositoryMock, $this->getMock(AccessTokenRepositoryInterface::class), $this->getMock(ScopeRepositoryInterface::class), 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key' ); $server->enableGrantType($grant); $request = new ServerRequest( [], [], null, null, 'php://input', $headers = [], $cookies = [], $queryParams = [ 'response_type' => 'code', 'client_id' => 'foo', ] ); $this->assertTrue($server->validateAuthorizationRequest($request) instanceof AuthorizationRequest); } /** * @expectedException \League\OAuth2\Server\Exception\OAuthServerException * @expectedExceptionCode 2 */ public function testValidateAuthorizationRequestUnregistered() { $server = new Server( $this->getMock(ClientRepositoryInterface::class), $this->getMock(AccessTokenRepositoryInterface::class), $this->getMock(ScopeRepositoryInterface::class), 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key' ); $request = new ServerRequest( [], [], null, null, 'php://input', $headers = [], $cookies = [], $queryParams = [ 'response_type' => 'code', 'client_id' => 'foo', ] ); $server->validateAuthorizationRequest($request); } }