AccessTokenAuthenticator example

$this->expectException(BadCredentialsException::class);
        $this->expectExceptionMessage('Invalid credentials.');

        $request = Request::create('/test');

        $this->accessTokenExtractor
            ->expects($this->once())
            ->method('extractAccessToken')
            ->with($request)
            ->willReturn(null);

        $authenticator = new AccessTokenAuthenticator(
            $this->accessTokenHandler,
            $this->accessTokenExtractor,
        );

        $authenticator->authenticate($request);
    }

    public function testAuthenticateWithoutProvider()
    {
        $request = Request::create('/test');

        
$request = new Request();
        $request->query->set('access_token', 123456789);
        yield [$request, 'Invalid credentials.', BadCredentialsException::class];

        $request = new Request();
        $request->query->set('access_token', 'INVALID_ACCESS_TOKEN');
        yield [$request, 'Invalid access token or invalid user.', BadCredentialsException::class];
    }

    private function setUpAuthenticator(string $parameter = 'access_token'): void
    {
        $this->authenticator = new AccessTokenAuthenticator(
            $this->accessTokenHandler,
            new QueryAccessTokenExtractor($parameter),
            $this->userProvider
        );
    }
}
$request->request->set('foo', 'VALID_ACCESS_TOKEN');
        yield [$request, 'Invalid credentials.', BadCredentialsException::class];

        $request = new Request([][][][][]['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
        $request->setMethod(Request::METHOD_POST);
        $request->request->set('access_token', 'INVALID_ACCESS_TOKEN');
        yield [$request, 'Invalid access token or invalid user.', BadCredentialsException::class];
    }

    private function setUpAuthenticator(string $parameter = 'access_token'): void
    {
        $this->authenticator = new AccessTokenAuthenticator(
            $this->accessTokenHandler,
            new FormEncodedBodyExtractor($parameter),
            $this->userProvider
        );
    }
}
yield [$request, 'Invalid credentials.', BadCredentialsException::class];

        $request = new Request([][][][][]['HTTP_AUTHORIZATION' => 'BearerVALID_ACCESS_TOKEN']);
        yield [$request, 'Invalid credentials.', BadCredentialsException::class];

        $request = new Request([][][][][]['HTTP_AUTHORIZATION' => 'Bearer INVALID_ACCESS_TOKEN']);
        yield [$request, 'Invalid access token or invalid user.', BadCredentialsException::class];
    }

    private function setUpAuthenticator(): void
    {
        $this->authenticator = new AccessTokenAuthenticator(
            $this->accessTokenHandler,
            new ChainAccessTokenExtractor([
                new FormEncodedBodyExtractor(),
                new QueryAccessTokenExtractor(),
                new HeaderAccessTokenExtractor(),
            ]),
            $this->userProvider
        );
    }
}
yield [$request, 'Invalid credentials.', BadCredentialsException::class];

        $request = new Request([][][][][]['HTTP_AUTHORIZATION' => 'BearerVALID_ACCESS_TOKEN']);
        yield [$request, 'Invalid credentials.', BadCredentialsException::class];

        $request = new Request([][][][][]['HTTP_AUTHORIZATION' => 'Bearer INVALID_ACCESS_TOKEN']);
        yield [$request, 'Invalid access token or invalid user.', BadCredentialsException::class];
    }

    private function setUpAuthenticator(string $headerParameter = 'Authorization', string $tokenType = 'Bearer'): void
    {
        $this->authenticator = new AccessTokenAuthenticator(
            $this->accessTokenHandler,
            new HeaderAccessTokenExtractor($headerParameter$tokenType),
            $this->userProvider
        );
    }
}
Home | Imprint | This part of the site doesn't use cookies.