diff --git a/app/Auth/NostrSessionGuard.php b/app/Auth/NostrSessionGuard.php new file mode 100644 index 0000000..0d2fc13 --- /dev/null +++ b/app/Auth/NostrSessionGuard.php @@ -0,0 +1,41 @@ +updateSession($user->getAuthIdentifier()); + + $this->setUser($user); + + $this->fireLoginEvent($user, false); + } + + protected function updateSession($id): void + { + $this->session->put($this->getName(), $id); + $this->session->migrate(true); + } + + public function user(): ?Authenticatable + { + if ($this->user !== null) { + return $this->user; + } + + $id = $this->session->get($this->getName()); + + if ($id !== null) { + $this->user = $this->provider->retrieveById($id); + } + + return $this->user; + } +} diff --git a/app/Auth/NostrUser.php b/app/Auth/NostrUser.php new file mode 100644 index 0000000..79a61da --- /dev/null +++ b/app/Auth/NostrUser.php @@ -0,0 +1,64 @@ +pubkey = $pubkey; + $this->pleb = \App\Models\EinundzwanzigPleb::query() + ->where('pubkey', $pubkey) + ->first(); + } + + public function getAuthIdentifierName(): string + { + return 'pubkey'; + } + + public function getAuthIdentifier(): string + { + return $this->pubkey; + } + + public function getAuthPassword(): string + { + return ''; + } + + public function getRememberToken(): ?string + { + return null; + } + + public function setRememberToken($value): void + { + // + } + + public function getRememberTokenName(): ?string + { + return null; + } + + public function getAuthPasswordName(): string + { + return 'password'; + } + + public function getPubkey(): string + { + return $this->pubkey; + } + + public function getPleb(): ?object + { + return $this->pleb; + } +} diff --git a/app/Auth/NostrUserProvider.php b/app/Auth/NostrUserProvider.php new file mode 100644 index 0000000..3802804 --- /dev/null +++ b/app/Auth/NostrUserProvider.php @@ -0,0 +1,43 @@ +getPubkey() === ($credentials['pubkey'] ?? null); + } + + public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false): void + { + // + } +} diff --git a/app/Livewire/Traits/WithNostrAuth.php b/app/Livewire/Traits/WithNostrAuth.php new file mode 100644 index 0000000..cbb35af --- /dev/null +++ b/app/Livewire/Traits/WithNostrAuth.php @@ -0,0 +1,55 @@ +currentPubkey = $pubkey; + $this->currentPleb = \App\Models\EinundzwanzigPleb::query() + ->where('pubkey', $pubkey) + ->first(); + + if ($this->currentPleb && in_array($this->currentPleb->npub, config('einundzwanzig.config.current_board'), true)) { + $this->canEdit = true; + } + + $this->isAllowed = true; + } + + #[On('nostrLoggedOut')] + public function handleNostrLogout(): void + { + NostrAuth::logout(); + + $this->isAllowed = false; + $this->currentPubkey = null; + $this->currentPleb = null; + $this->canEdit = false; + } + + public function mountNostrAuth(): void + { + if ($user = NostrAuth::user()) { + $this->currentPubkey = $user->getPubkey(); + $this->currentPleb = $user->getPleb(); + $this->isAllowed = true; + + if ($this->currentPleb && in_array($this->currentPleb->npub, config('einundzwanzig.config.current_board'), true)) { + $this->canEdit = true; + } + } + } +} diff --git a/app/Providers/NostrAuthServiceProvider.php b/app/Providers/NostrAuthServiceProvider.php new file mode 100644 index 0000000..33256a4 --- /dev/null +++ b/app/Providers/NostrAuthServiceProvider.php @@ -0,0 +1,30 @@ +loginByPubkey($pubkey); + Session::regenerate(); + } + + /** + * Logout the current Nostr user + */ + public static function logout(): void + { + if (Auth::guard('nostr')->check()) { + Session::flush(); + } + } + + /** + * Get the currently authenticated Nostr user + */ + public static function user(): ?NostrUser + { + return Auth::guard('nostr')->user(); + } + + /** + * Check if a Nostr user is authenticated + */ + public static function check(): bool + { + return Auth::guard('nostr')->check(); + } + + /** + * Get the current pubkey (convenience method) + */ + public static function pubkey(): ?string + { + return self::user()?->getPubkey(); + } + + /** + * Get the current pleb (convenience method) + */ + public static function pleb(): ?object + { + return self::user()?->getPleb(); + } +} diff --git a/bootstrap/providers.php b/bootstrap/providers.php index 90915e8..8e4d42c 100644 --- a/bootstrap/providers.php +++ b/bootstrap/providers.php @@ -3,5 +3,6 @@ return [ App\Providers\AppServiceProvider::class, App\Providers\FolioServiceProvider::class, + App\Providers\NostrAuthServiceProvider::class, App\Providers\VoltServiceProvider::class, ]; diff --git a/config/auth.php b/config/auth.php index 0ba5d5d..c8aee63 100644 --- a/config/auth.php +++ b/config/auth.php @@ -40,6 +40,10 @@ return [ 'driver' => 'session', 'provider' => 'users', ], + 'nostr' => [ + 'driver' => 'nostr-session', + 'provider' => 'nostr', + ], ], /* @@ -64,6 +68,9 @@ return [ 'driver' => 'eloquent', 'model' => env('AUTH_MODEL', App\Models\User::class), ], + 'nostr' => [ + 'driver' => 'nostr', + ], // 'users' => [ // 'driver' => 'database', diff --git a/resources/views/components/layouts/app.blade.php b/resources/views/components/layouts/app.blade.php index 831170c..f884b3d 100644 --- a/resources/views/components/layouts/app.blade.php +++ b/resources/views/components/layouts/app.blade.php @@ -69,8 +69,16 @@ {{--@include('components.layouts.partials.notification-buttons')--}} - + @if(\App\Support\NostrAuth::check()) +
+ @csrf + + + @else + + @endif
diff --git a/resources/views/components/layouts/navigation/admin.blade.php b/resources/views/components/layouts/navigation/admin.blade.php index 176e5e9..4dc4fed 100644 --- a/resources/views/components/layouts/navigation/admin.blade.php +++ b/resources/views/components/layouts/navigation/admin.blade.php @@ -6,7 +6,7 @@ Admin-Bereich
- {{-- https://v.nostr.build/bomfuwLnOTIDrP4y.mp4 --}}