mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-nostr.git
synced 2025-12-27 21:30:16 +00:00
🔒 Add Nostr authentication support with custom guard and user provider
🛠️ Integrate Nostr auth across relevant components and views 📦 Update config, routes, and service provider for Nostr auth
This commit is contained in:
41
app/Auth/NostrSessionGuard.php
Normal file
41
app/Auth/NostrSessionGuard.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use Illuminate\Auth\SessionGuard;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class NostrSessionGuard extends SessionGuard
|
||||
{
|
||||
public function loginByPubkey(string $pubkey): void
|
||||
{
|
||||
$user = new NostrUser($pubkey);
|
||||
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
64
app/Auth/NostrUser.php
Normal file
64
app/Auth/NostrUser.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class NostrUser implements Authenticatable
|
||||
{
|
||||
protected string $pubkey;
|
||||
protected ?object $pleb;
|
||||
|
||||
public function __construct(string $pubkey)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
43
app/Auth/NostrUserProvider.php
Normal file
43
app/Auth/NostrUserProvider.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
|
||||
class NostrUserProvider implements UserProvider
|
||||
{
|
||||
public function retrieveById($identifier): ?Authenticatable
|
||||
{
|
||||
return new NostrUser($identifier);
|
||||
}
|
||||
|
||||
public function retrieveByToken($identifier, $token): ?Authenticatable
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function updateRememberToken(Authenticatable $user, $token): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function retrieveByCredentials(array $credentials): ?Authenticatable
|
||||
{
|
||||
if (isset($credentials['pubkey'])) {
|
||||
return new NostrUser($credentials['pubkey']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function validateCredentials(Authenticatable $user, array $credentials): bool
|
||||
{
|
||||
return $user instanceof NostrUser && $user->getPubkey() === ($credentials['pubkey'] ?? null);
|
||||
}
|
||||
|
||||
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user