|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Actions\StorageProvider; |
| 4 | + |
| 5 | +use App\Models\StorageProvider; |
| 6 | +use App\Models\User; |
| 7 | +use App\StorageProviders\Dropbox; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Support\Facades\Http; |
| 10 | +use Illuminate\Support\Facades\Validator; |
| 11 | +use Illuminate\Support\Str; |
| 12 | +use Illuminate\Validation\ValidationException; |
| 13 | +use RuntimeException; |
| 14 | + |
| 15 | +class ConnectDropbox |
| 16 | +{ |
| 17 | + private const string AUTHORIZE_URL = 'https://www.dropbox.com/oauth2/authorize'; |
| 18 | + |
| 19 | + private const string TOKEN_URL = 'https://api.dropbox.com/oauth2/token'; |
| 20 | + |
| 21 | + private const string SESSION_KEY = 'dropbox_oauth'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param array<string, mixed> $input |
| 25 | + * |
| 26 | + * @throws ValidationException |
| 27 | + */ |
| 28 | + public function redirectUrl(array $input): string |
| 29 | + { |
| 30 | + Validator::make($input, [ |
| 31 | + 'name' => ['required'], |
| 32 | + 'app_key' => ['required'], |
| 33 | + 'app_secret' => ['required'], |
| 34 | + ])->validate(); |
| 35 | + |
| 36 | + $state = Str::random(40); |
| 37 | + |
| 38 | + session()->put(self::SESSION_KEY, [ |
| 39 | + 'state' => $state, |
| 40 | + 'name' => $input['name'], |
| 41 | + 'app_key' => $input['app_key'], |
| 42 | + 'app_secret' => $input['app_secret'], |
| 43 | + 'global' => isset($input['global']) && $input['global'], |
| 44 | + ]); |
| 45 | + |
| 46 | + return self::AUTHORIZE_URL.'?'.http_build_query([ |
| 47 | + 'client_id' => $input['app_key'], |
| 48 | + 'response_type' => 'code', |
| 49 | + 'token_access_type' => 'offline', |
| 50 | + 'state' => $state, |
| 51 | + 'redirect_uri' => $this->redirectUri(), |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @throws ValidationException |
| 57 | + * @throws RuntimeException |
| 58 | + */ |
| 59 | + public function handleCallback(User $user, Request $request): StorageProvider |
| 60 | + { |
| 61 | + if ($request->filled('error')) { |
| 62 | + session()->forget(self::SESSION_KEY); |
| 63 | + |
| 64 | + throw ValidationException::withMessages([ |
| 65 | + 'provider' => __('Dropbox authorization was cancelled.'), |
| 66 | + ]); |
| 67 | + } |
| 68 | + |
| 69 | + /** @var array<string, mixed>|null $pending */ |
| 70 | + $pending = session()->pull(self::SESSION_KEY); |
| 71 | + |
| 72 | + if (! is_array($pending) || ! hash_equals((string) ($pending['state'] ?? ''), (string) $request->query('state'))) { |
| 73 | + throw ValidationException::withMessages([ |
| 74 | + 'provider' => __('Invalid Dropbox authorization state.'), |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + Validator::make($request->query(), [ |
| 79 | + 'code' => ['required', 'string'], |
| 80 | + ])->validate(); |
| 81 | + |
| 82 | + $refreshToken = $this->exchangeCode( |
| 83 | + (string) $request->query('code'), |
| 84 | + (string) $pending['app_key'], |
| 85 | + (string) $pending['app_secret'], |
| 86 | + ); |
| 87 | + |
| 88 | + return app(CreateStorageProvider::class)->create($user, [ |
| 89 | + 'provider' => Dropbox::id(), |
| 90 | + 'name' => $pending['name'], |
| 91 | + 'app_key' => $pending['app_key'], |
| 92 | + 'app_secret' => $pending['app_secret'], |
| 93 | + 'refresh_token' => $refreshToken, |
| 94 | + 'global' => $pending['global'], |
| 95 | + ]); |
| 96 | + } |
| 97 | + |
| 98 | + public function redirectUri(): string |
| 99 | + { |
| 100 | + return route('storage-providers.dropbox.callback'); |
| 101 | + } |
| 102 | + |
| 103 | + private function exchangeCode(string $code, string $appKey, string $appSecret): string |
| 104 | + { |
| 105 | + $res = Http::asForm()->post(self::TOKEN_URL, [ |
| 106 | + 'grant_type' => 'authorization_code', |
| 107 | + 'code' => $code, |
| 108 | + 'client_id' => $appKey, |
| 109 | + 'client_secret' => $appSecret, |
| 110 | + 'redirect_uri' => $this->redirectUri(), |
| 111 | + ]); |
| 112 | + |
| 113 | + if (! $res->successful()) { |
| 114 | + throw new RuntimeException("Failed to exchange Dropbox authorization code (HTTP {$res->status()})"); |
| 115 | + } |
| 116 | + |
| 117 | + $refreshToken = $res->json('refresh_token'); |
| 118 | + |
| 119 | + if (! is_string($refreshToken) || $refreshToken === '') { |
| 120 | + throw new RuntimeException('Dropbox did not return a refresh token. Ensure the app uses offline access.'); |
| 121 | + } |
| 122 | + |
| 123 | + return $refreshToken; |
| 124 | + } |
| 125 | +} |
0 commit comments