Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-13 #14

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions src/Issuer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Issuer
*/
protected array $abilities = [];

protected bool $init = false;

protected bool $isRoot = false;

protected bool $isAdmin = false;
Expand Down Expand Up @@ -238,17 +240,21 @@ protected function abilities(Authenticatable $user): array
return $this->abilities;
}

/**
* @param array<string, mixed> $config
*/
public function init(Authenticatable $user, array $config): void
public function init(Authenticatable $user): self
{
if ($user instanceof HasApiTokens) {
if ($this->init) {
return $this;
}

$config = config('playground-auth');
$config = is_array($config) ? $config : [];

if ($user instanceof HasApiTokens || is_callable([$user, 'createToken'])) {
$this->hasSanctum = ! empty($config['sanctum']);
} else {
$this->hasSanctum = false;
}
// dump([
// dd([
// '__METHOD__' => __METHOD__,
// '$user' => $user->toArray(),
// '$config' => $config,
Expand Down Expand Up @@ -300,13 +306,17 @@ public function init(Authenticatable $user, array $config): void
$this->isGuest = false;
}

if (! empty($config['listed'])) {
if (empty($config['listed'])) {
$this->listed($user);
}

if (! $this->isGuest) {
$this->isGuest = ! ($this->isRoot || $this->isAdmin || $this->isManager || $this->isUser);
}

$this->init = true;

return $this;
}

public function listed(Authenticatable $user): void
Expand All @@ -328,23 +338,40 @@ public function listed(Authenticatable $user): void
}

/**
* @param Authenticatable&HasApiTokens $user
* @return array<string, ?string> Returns tokens for authorization.
*/
public function authorize(Authenticatable $user): array
{
$this->init($user);

if ($this->hasSanctum && $this->useSanctum) {
$tokens = $this->sanctum($user);
} else {
$tokens = [];
}

return $tokens;
}

/**
* @return array<string, ?string>
*/
public function sanctum(HasApiTokens $user): array
public function sanctum(Authenticatable $user): array
{
/**
* @var array<string, mixed> $config
*/
$config = config('playground-auth.token');

$this->init($user, $config);
$this->init($user);

$tokens = [];

if (! $this->hasSanctum) {
throw new \Exception(__('playground-auth::auth.sanctum.disabled'));
}
Log::debug(__('playground-auth::auth.sanctum.disabled'));

$tokens = [];
return $tokens;
}

$name = 'app';
if (! empty($config['name']) && is_string($config['name'])) {
Expand All @@ -357,10 +384,13 @@ public function sanctum(HasApiTokens $user): array
$expiresAt = Carbon::parse($config['expires']);
}

$tokens[$name] = $user->createToken(
$name,
$this->abilities($user)
)->plainTextToken;
if (is_callable([$user, 'createToken'])) {
$tokens[$name] = $user->createToken(
$name,
$this->abilities($user)
// $expiresAt
)->plainTextToken;
}

return $tokens;
}
Expand Down
Loading