'/certificates/ca.crt', 'http_errors' => FALSE, 'timeout' => 10, ]); $clientFactory = new AtprotoClientFactory($transport, $httpFactory, $httpFactory); $anonymous = $clientFactory->create('https://pds.atproto-php.wotnak.dev'); $handle = 'oauth-user.test'; $password = 'correct horse battery staple'; $created = $anonymous->post('com.atproto.server.createAccount', [ 'handle' => $handle, 'email' => 'oauth-user@example.com', 'password' => $password, ]); $oauth = new OAuthClient( $transport, $httpFactory, $httpFactory, new DidResolver( $transport, $httpFactory, plcDirectoryUrl: 'https://plc.atproto-php.wotnak.dev', ), new HandleResolver($transport, $httpFactory), ); $config = new OAuthConfig( clientId: 'https://client.atproto-php.wotnak.dev/client-metadata.json', redirectUris: ['https://client.atproto-php.wotnak.dev/callback'], scopes: ['atproto'], clientName: 'atproto-php OAuth E2E', ); $authorization = $oauth->createAuthorizationRequest( $config, 'https://client.atproto-php.wotnak.dev/callback', 'https://pds.atproto-php.wotnak.dev', ); static::assertStringStartsWith('https://pds.atproto-php.wotnak.dev/oauth/authorize?', $authorization->authorizationUrl); $browser = new Client([ 'verify' => '/certificates/ca.crt', 'http_errors' => FALSE, 'timeout' => 10, 'cookies' => $cookies = new CookieJar(), 'allow_redirects' => FALSE, ]); $authorizationPage = $browser->get($authorization->authorizationUrl, [ 'headers' => [ 'Sec-Fetch-Mode' => 'navigate', 'Sec-Fetch-Site' => 'none', 'Sec-Fetch-Dest' => 'document', ], ]); static::assertSame(200, $authorizationPage->getStatusCode()); $csrfCookie = $cookies->getCookieByName('csrf-token'); static::assertNotNull($csrfCookie); $browserHeaders = [ 'Origin' => 'https://pds.atproto-php.wotnak.dev', 'Referer' => $authorization->authorizationUrl, 'Sec-Fetch-Mode' => 'same-origin', 'Sec-Fetch-Site' => 'same-origin', 'X-CSRF-Token' => $csrfCookie->getValue(), ]; $signInResponse = $browser->post( 'https://pds.atproto-php.wotnak.dev/@atproto/oauth-provider/~api/sign-in', [ 'headers' => $browserHeaders, 'json' => [ 'locale' => 'en', 'username' => $handle, 'password' => $password, 'remember' => FALSE, ], ], ); static::assertSame(200, $signInResponse->getStatusCode()); $signIn = json_decode((string) $signInResponse->getBody(), TRUE, 64, JSON_THROW_ON_ERROR); static::assertIsArray($signIn); static::assertSame($created['did'], $signIn['account']['did'] ?? NULL); static::assertIsString($signIn['ephemeralToken'] ?? NULL); $csrfCookie = $cookies->getCookieByName('csrf-token'); static::assertNotNull($csrfCookie); $browserHeaders['X-CSRF-Token'] = $csrfCookie->getValue(); $browserHeaders['Authorization'] = 'Bearer ' . $signIn['ephemeralToken']; $consentResponse = $browser->post( 'https://pds.atproto-php.wotnak.dev/@atproto/oauth-provider/~api/consent', [ 'headers' => $browserHeaders, 'json' => ['did' => $created['did']], ], ); static::assertSame(200, $consentResponse->getStatusCode()); $consent = json_decode((string) $consentResponse->getBody(), TRUE, 64, JSON_THROW_ON_ERROR); static::assertIsArray($consent); static::assertIsString($consent['url'] ?? NULL); parse_str((string) parse_url($consent['url'], PHP_URL_QUERY), $callback); static::assertSame($authorization->state->state, $callback['state'] ?? NULL); static::assertIsString($callback['code'] ?? NULL); static::assertIsString($callback['iss'] ?? NULL); $session = $oauth->exchangeAuthorizationCode( $authorization->state, $callback['code'], $callback['iss'], ); static::assertSame($created['did'], $session->did); static::assertSame('DPoP', $session->tokens->tokenType); static::assertNotNull($session->tokens->refreshToken); $authenticated = $clientFactory->createAuthenticated($session->authenticatedSession()); $resourceSession = $authenticated->get('com.atproto.server.getSession'); static::assertSame($created['did'], $resourceSession['did'] ?? NULL); $refreshed = $oauth->refreshSession($config, $session); static::assertSame($session->did, $refreshed->did); static::assertNotSame($session->tokens->accessToken, $refreshed->tokens->accessToken); static::assertNotSame($session->tokens->refreshToken, $refreshed->tokens->refreshToken); $refreshedClient = $clientFactory->createAuthenticated($refreshed->authenticatedSession()); $refreshedResourceSession = $refreshedClient->get('com.atproto.server.getSession'); static::assertSame($created['did'], $refreshedResourceSession['did'] ?? NULL); try { $oauth->refreshSession($config, $session); static::fail('Expected reuse of the rotated refresh token to fail.'); } catch (OAuthHttpException $exception) { static::assertSame('token', $exception->endpoint); static::assertSame(400, $exception->statusCode); static::assertSame('invalid_grant', $exception->oauthError); static::assertSame('refresh_token', $exception->grantType); static::assertTrue($exception->definitivelyRejectsRefreshToken()); } } }