names() as $name) { $sharer = $sharers->for($name); $connection = $sharer->connection(); $platforms[] = [ 'platform' => $name, 'label' => $sharer->label(), 'can_connect' => $sharer->canConnect(), 'missing' => $sharer->canConnect() ? null : $sharer->missing(), 'connected' => $connection !== null, 'account' => $sharer->accountName(), 'expires_at' => $connection?->expires_at?->toIso8601String(), 'callback_url' => route(Names::route('sharing.callback'), $name), ]; } $notice = (string) $request->query('notice'); return response()->json([ 'live' => PostSharing::isLive(), 'platforms' => $platforms, 'notice' => in_array($notice, self::NOTICES, true) ? $notice : null, 'shares' => PostShare::query()->with('post')->where('status', '!=', PostShare::PENDING)->latest('id')->limit(30)->get() ->map(fn (PostShare $share) => [ 'id' => $share->id, 'post_id' => $share->post_id, 'post_title' => $share->post?->title, 'platform' => $share->platform, 'status' => $share->status, 'dry_run' => (bool) $share->dry_run, 'url' => $share->external_url, 'error' => $share->error, 'at' => ($share->shared_at ?? $share->updated_at)?->toIso8601String(), ])->values(), ]); } /** Sends the owner to the platform to give permission. */ public function connect(Request $request, Sharers $sharers, string $platform): RedirectResponse { $sharer = $sharers->for($platform); if (! $sharer->canConnect()) { return $this->back('failed'); } // The platform sends this value back; it proves the answer belongs to this login session. $state = Str::random(40); // A second secret that never leaves this site until the code is traded in (PKCE). $verifier = Str::random(64); $request->session()->put(self::STATE_KEY.$platform, $state); $request->session()->put(self::VERIFIER_KEY.$platform, $verifier); return redirect()->away($sharer->authorizationUrl($state, route(Names::route('sharing.callback'), $platform), $verifier)); } /** Where the platform sends the owner back to, with a code or with an error. */ public function callback(Request $request, Sharers $sharers, string $platform): RedirectResponse { $sharer = $sharers->for($platform); $expected = $request->session()->pull(self::STATE_KEY.$platform); $verifier = $request->session()->pull(self::VERIFIER_KEY.$platform); $state = $request->query('state'); if (! is_string($expected) || ! is_string($state) || ! hash_equals($expected, $state)) { return $this->back('state'); } if ($request->query('error') !== null) { return $this->back('denied'); } $code = $request->query('code'); if (! is_string($code) || $code === '') { return $this->back('failed'); } try { $sharer->connect($code, route(Names::route('sharing.callback'), $platform), is_string($verifier) ? $verifier : ''); } catch (SharingException) { return $this->back('failed'); } return $this->back('connected'); } public function disconnect(Sharers $sharers, string $platform): JsonResponse { $sharers->for($platform)->disconnect(); return response()->json(['ok' => true]); } /** How a post stands on every platform, for the panel on its screen. */ public function post(Post $post, PostSharing $sharing): JsonResponse { return response()->json(['live' => PostSharing::isLive(), 'platforms' => $sharing->overview($post)]); } public function share(Request $request, Post $post, Sharers $sharers, PostSharing $sharing): JsonResponse { $request->validate(['platform' => ['required', 'string', 'max:20']]); $platform = $request->string('platform')->toString(); abort_unless($sharers->has($platform), 422, __('This platform is not known.')); try { $share = $sharing->shareNow($post, $platform); } catch (SharingException $e) { return response()->json(['message' => $e->getMessage()], 422); } if ($share->status === PostShare::FAILED) { return response()->json(['message' => (string) $share->error], 422); } return response()->json(['ok' => true, 'dry_run' => (bool) $share->dry_run, 'url' => $share->external_url], 201); } private function back(string $notice): RedirectResponse { return redirect(route(Names::route('sharing.index'), ['notice' => $notice])); } }