Laravel AT Protocol Client (alpha & unstable)
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Split token refresh into separate OAuth and legacy methods

+37 -2
+37 -2
src/Auth/TokenRefresher.php
··· 2 2 3 3 namespace SocialDept\AtpClient\Auth; 4 4 5 + use Illuminate\Support\Facades\Http; 5 6 use SocialDept\AtpClient\Data\AccessToken; 6 7 use SocialDept\AtpClient\Data\DPoPKey; 8 + use SocialDept\AtpClient\Enums\AuthType; 7 9 use SocialDept\AtpClient\Exceptions\AuthenticationException; 8 10 use SocialDept\AtpClient\Http\DPoPClient; 9 11 ··· 14 16 ) {} 15 17 16 18 /** 17 - * Refresh access token using refresh token 19 + * Refresh access token using refresh token. 18 20 * NOTE: Refresh tokens are single-use! 19 21 */ 20 22 public function refresh( 21 23 string $refreshToken, 22 24 string $pdsEndpoint, 23 25 DPoPKey $dpopKey, 24 - ?string $handle = null 26 + ?string $handle = null, 27 + AuthType $authType = AuthType::OAuth, 28 + ): AccessToken { 29 + return $authType === AuthType::Legacy 30 + ? $this->refreshLegacy($refreshToken, $pdsEndpoint, $handle) 31 + : $this->refreshOAuth($refreshToken, $pdsEndpoint, $dpopKey, $handle); 32 + } 33 + 34 + /** 35 + * Refresh OAuth session using /oauth/token endpoint with DPoP. 36 + */ 37 + protected function refreshOAuth( 38 + string $refreshToken, 39 + string $pdsEndpoint, 40 + DPoPKey $dpopKey, 41 + ?string $handle, 25 42 ): AccessToken { 26 43 $tokenUrl = $pdsEndpoint.'/oauth/token'; 27 44 ··· 31 48 'grant_type' => 'refresh_token', 32 49 'refresh_token' => $refreshToken, 33 50 ]); 51 + 52 + if ($response->failed()) { 53 + throw new AuthenticationException('Token refresh failed: '.$response->body()); 54 + } 55 + 56 + return AccessToken::fromResponse($response->json(), $handle, $pdsEndpoint); 57 + } 58 + 59 + /** 60 + * Refresh legacy session using /xrpc/com.atproto.server.refreshSession endpoint. 61 + */ 62 + protected function refreshLegacy( 63 + string $refreshToken, 64 + string $pdsEndpoint, 65 + ?string $handle, 66 + ): AccessToken { 67 + $response = Http::withHeader('Authorization', 'Bearer '.$refreshToken) 68 + ->post($pdsEndpoint.'/xrpc/com.atproto.server.refreshSession'); 34 69 35 70 if ($response->failed()) { 36 71 throw new AuthenticationException('Token refresh failed: '.$response->body());