require __DIR__ . '/../init_autoload.php'; $config = (require __DIR__ . '/config.php'); $clientInfo = new ClientInfo(); $clientInfo->fromArray($config['client_info']); $stateManager = new Manager(); $dispatcher = new Authorization\Dispatcher(); $dispatcher->setStateManager($stateManager); if (!isset($_GET['redirect'])) { $request = new Authorization\Request($clientInfo, 'code', 'openid profile email'); $uri = $dispatcher->createAuthorizationRequestUri($request); _dump($uri); printf("<pre>%s</pre><br>", $uri); printf("<a href=\"%s\">Login</a>", $uri); } else { try { $response = $dispatcher->getAuthorizationResponse(); printf("OK<br>Code: %s<br>State: %s<br>", $response->getCode(), $response->getState()); $tokenRequest = new Token\Request(); $tokenRequest->fromArray(array('client_info' => $clientInfo, 'code' => $response->getCode(), 'grant_type' => 'authorization_code')); $httpClient = _createHttpClient(); $tokenDispatcher = new Token\Dispatcher($httpClient); try { $tokenResponse = $tokenDispatcher->sendTokenRequest($tokenRequest); _dump($tokenResponse); printf("Access token: %s<br>", $tokenResponse->getAccessToken()); $userInfoRequest = new UserInfo\Request(); $userInfoRequest->setAccessToken($tokenResponse->getAccessToken()); $userInfoRequest->setClientInfo($clientInfo); $userInfoDispatcher = new UserInfo\Dispatcher($httpClient); try { $userInfoResponse = $userInfoDispatcher->sendUserInfoRequest($userInfoRequest);
public function testGetAuthorizationResponseOkWithState() { $code = '123'; $stateHash = 'abc'; $state = $this->createStateMock(); $dispatcher = new Dispatcher(); $httpRequest = $this->createHttpRequestMock(array('code' => $code, 'state' => $stateHash)); $response = $this->createResponseMock(); $responseFactory = $this->createResponseFactoryMock($code, $response, $stateHash); $dispatcher->setResponseFactory($responseFactory); $stateManager = $this->createStateManagerMock($stateHash); $dispatcher->setStateManager($stateManager); $this->assertSame($response, $dispatcher->getAuthorizationResponse($httpRequest)); $this->assertSame($httpRequest, $dispatcher->getLastHttpRequestFromServer()); $this->assertSame($response, $dispatcher->getLastResponse()); }