Skip to content

Commit

Permalink
added JwtGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyzhu committed Sep 12, 2020
1 parent 22465d9 commit 2e1cfd2
Show file tree
Hide file tree
Showing 15 changed files with 598 additions and 136 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Gate 可以通过注入 `HyperfExt\Auth\Contracts\Access\GateManagerInterface`

策略可以通过 `gen:policy` 命令来创建,例如 `gen:policy PostPolicy --model=App\\Model\\Post`。也可以在配置文件的 `policies` 中定义模型类和策略类的映射。

如需使用 JWT,请额外安装 [`hyperf-ext/jwt`](https://github.com/hyperf-ext/jwt) 组件。

## 安装

```shell script
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"suggest": {
"hyperf/session": "Required to use session guard.",
"hyperf-ext/cookie": "Required to use session guard."
"hyperf-ext/cookie": "Required to use session guard.",
"hyperf-ext/jwt": "Required to use JWT guard."
},
"config": {
"sort-packages": true
Expand Down
12 changes: 4 additions & 8 deletions publish/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
|
*/

'defaults' => [
'default' => [
'guard' => 'web',
'passwords' => 'users',
],
Expand All @@ -44,17 +44,13 @@
'web' => [
'driver' => \HyperfExt\Auth\Guards\SessionGuard::class,
'provider' => 'users',
'options' => [
'name' => 'session',
],
'options' => [],
],

'api' => [
'driver' => \HyperfExt\Auth\Guards\TokenGuard::class,
'driver' => \HyperfExt\Auth\Guards\JwtGuard::class,
'provider' => 'users',
'options' => [
'hash' => false,
],
'options' => [],
],
],

Expand Down
8 changes: 4 additions & 4 deletions src/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function guard(?string $name = null): GuardInterface
*/
public function getDefaultDriver(): string
{
return $this->config->get('auth.defaults.guard');
return $this->config->get('auth.default.guard');
}

/**
Expand All @@ -95,7 +95,7 @@ public function shouldUse(string $name): void
*/
public function setDefaultDriver(string $name)
{
$this->config->set('auth.defaults.guard', $name);
$this->config->set('auth.default.guard', $name);
}

/**
Expand Down Expand Up @@ -127,7 +127,7 @@ public function resolveUsersUsing(Closure $userResolver)
*/
public function createUserProvider(?string $provider = null): ?UserProviderInterface
{
$provider = $provider ?: $this->config->get('auth.defaults.provider', null);
$provider = $provider ?: $this->config->get('auth.default.provider', null);

$config = $this->config->get('auth.providers.' . $provider);

Expand Down Expand Up @@ -168,7 +168,7 @@ protected function resolve(string $name)
$provider = $this->createUserProvider($config['provider'] ?? null);
$options = $config['options'] ?? [];

return make($config['driver'], compact('provider', 'options'));
return make($config['driver'], compact('provider', 'name', 'options'));
}

protected function getUserResolverClosure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class GenPolicyCommand extends HyperfCommand
class GenAuthPolicyCommand extends HyperfCommand
{
/**
* @var \Hyperf\Contract\ConfigInterface
Expand All @@ -26,7 +26,7 @@ class GenPolicyCommand extends HyperfCommand

public function __construct(ConfigInterface $config)
{
parent::__construct('gen:policy');
parent::__construct('gen:auth-policy');
$this->config = $config;
}

Expand All @@ -49,7 +49,7 @@ public function handle()
$option = new PolicyOption();
$option
->setPath($this->getOption('path', 'app/Policy'))
->setGuard($this->getOption('guard'), $this->config->get('auth.defaults.guard', null))
->setGuard($this->getOption('guard', $this->config->get('auth.default.guard', null)))
->setModel($this->getOption('model'));
$this->createPolicy($name, $option);
}
Expand Down Expand Up @@ -138,7 +138,7 @@ protected function getNamespace(string $name): string
*/
protected function getUserProviderModel(?string $guard = null): ?string
{
$guard = $guard ?: $this->config->get('auth.defaults.guard');
$guard = $guard ?: $this->config->get('auth.default.guard');

return $this->config->get(
'auth.providers.' . $this->config->get('auth.guards.' . $guard . '.provider') . '.options.model'
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace HyperfExt\Auth;

use HyperfExt\Auth\Access\GateManager;
use HyperfExt\Auth\Commands\GenPolicyCommand;
use HyperfExt\Auth\Commands\GenAuthPolicyCommand;
use HyperfExt\Auth\Contracts\Access\GateManagerInterface;
use HyperfExt\Auth\Contracts\AuthManagerInterface;
use HyperfExt\Auth\Contracts\PasswordBrokerManagerInterface;
Expand All @@ -38,7 +38,7 @@ public function __invoke(): array
],
],
'commands' => [
GenPolicyCommand::class,
GenAuthPolicyCommand::class,
],
'publish' => [
[
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/AuthManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface AuthManagerInterface
/**
* Get a guard instance by name.
*
* @return \HyperfExt\Auth\Contracts\GuardInterface|\HyperfExt\Auth\Contracts\StatefulGuardInterface
* @return \HyperfExt\Auth\Contracts\GuardInterface|\HyperfExt\Auth\Contracts\StatefulGuardInterface|\HyperfExt\Auth\Contracts\StatelessGuardInterface
*/
public function guard(?string $name = null): GuardInterface;

Expand Down
9 changes: 6 additions & 3 deletions src/Contracts/StatefulGuardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ interface StatefulGuardInterface extends GuardInterface
{
/**
* Attempt to authenticate a user using the given credentials.
* @return bool|mixed
*/
public function attempt(array $credentials = [], bool $remember = false): bool;
public function attempt(array $credentials = [], bool $remember = false);

/**
* Log a user into the application without sessions or cookies.
Expand All @@ -26,8 +27,9 @@ public function once(array $credentials = []): bool;
* Log a user into the application.
*
* @param \HyperfExt\Auth\Contracts\AuthenticatableInterface $user
* @return mixed|void
*/
public function login(AuthenticatableInterface $user, bool $remember = false): void;
public function login(AuthenticatableInterface $user, bool $remember = false);

/**
* Log the given user ID into the application.
Expand All @@ -54,6 +56,7 @@ public function viaRemember(): bool;

/**
* Log the user out of the application.
* @return mixed|void
*/
public function logout(): void;
public function logout();
}
68 changes: 68 additions & 0 deletions src/Contracts/StatelessGuardInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);
/**
* This file is part of hyperf-ext/auth.
*
* @link https://github.com/hyperf-ext/auth
* @contact eric@zhu.email
* @license https://github.com/hyperf-ext/auth/blob/master/LICENSE
*/
namespace HyperfExt\Auth\Contracts;

interface StatelessGuardInterface extends GuardInterface
{
/**
* Attempt to authenticate the user using the given credentials and return the token.
*
* @return bool|mixed
*/
public function attempt(array $credentials = [], bool $login = true);

/**
* Log a user into the application without sessions or cookies.
*/
public function once(array $credentials = []): bool;

/**
* Log a user into the application, create a token for the user.
*
* @return mixed
*/
public function login(AuthenticatableInterface $user);

/**
* Log the given user ID into the application.
*
* @param mixed $id
*
* @return false|mixed
*/
public function loginUsingId($id);

/**
* Log the given user ID into the application without sessions or cookies.
*
* @param mixed $id
*/
public function onceUsingId($id): bool;

/**
* Log the user out of the application, thus invalidating the token.
*/
public function logout(bool $forceForever = false);

/**
* Refresh the token.
*
* @return mixed
*/
public function refresh(bool $forceForever = false);

/**
* Invalidate the token.
*
* @return mixed
*/
public function invalidate(bool $forceForever = false);
}
115 changes: 115 additions & 0 deletions src/EventHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);
/**
* This file is part of hyperf-ext/auth.
*
* @link https://github.com/hyperf-ext/auth
* @contact eric@zhu.email
* @license https://github.com/hyperf-ext/auth/blob/master/LICENSE
*/
namespace HyperfExt\Auth;

use HyperfExt\Auth\Contracts\AuthenticatableInterface;
use HyperfExt\Auth\Events\Attempting;
use HyperfExt\Auth\Events\Authenticated;
use HyperfExt\Auth\Events\CurrentDeviceLogout;
use HyperfExt\Auth\Events\Failed;
use HyperfExt\Auth\Events\Login;
use HyperfExt\Auth\Events\Logout;
use HyperfExt\Auth\Events\OtherDeviceLogout;
use HyperfExt\Auth\Events\Validated;

trait EventHelpers
{
/**
* Fire the attempt event with the arguments.
*/
protected function dispatchAttemptingEvent(array $credentials, bool $remember = false): void
{
$this->eventDispatcher->dispatch(new Attempting(
$this->name,
$credentials,
$remember
));
}

/**
* Fires the validated event if the dispatcher is set.
*/
protected function dispatchValidatedEvent(AuthenticatableInterface $user)
{
$this->eventDispatcher->dispatch(new Validated(
$this->name,
$user
));
}

/**
* Fire the login event if the dispatcher is set.
*/
protected function dispatchLoginEvent(AuthenticatableInterface $user, bool $remember = false): void
{
$this->eventDispatcher->dispatch(new Login(
$this->name,
$user,
$remember
));
}

/**
* Fire the authenticated event if the dispatcher is set.
*/
protected function dispatchAuthenticatedEvent(AuthenticatableInterface $user): void
{
$this->eventDispatcher->dispatch(new Authenticated(
$this->name,
$user
));
}

/**
* Fire the logout event if the dispatcher is set.
*/
protected function dispatchLogoutEvent(AuthenticatableInterface $user): void
{
$this->eventDispatcher->dispatch(new Logout(
$this->name,
$user
));
}

/**
* Fire the current device logout event if the dispatcher is set.
*/
protected function dispatchCurrentDeviceLogoutEvent(AuthenticatableInterface $user): void
{
$this->eventDispatcher->dispatch(new CurrentDeviceLogout(
$this->name,
$user
));
}

/**
* Fire the other device logout event if the dispatcher is set.
*/
protected function dispatchOtherDeviceLogoutEvent(AuthenticatableInterface $user): void
{
$this->eventDispatcher->dispatch(new OtherDeviceLogout(
$this->name,
$user
));
}

/**
* Fire the failed authentication attempt event with the given arguments.
*/
protected function dispatchFailedEvent(?AuthenticatableInterface $user, array $credentials): void
{
$this->eventDispatcher->dispatch(new Failed(
$this->name,
$user,
$credentials
));
}
}
6 changes: 3 additions & 3 deletions src/Events/Lockout.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
*/
namespace HyperfExt\Auth\Events;

use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;

class Lockout
{
/**
* The throttled request.
*
* @var \Hyperf\HttpServer\Contract\RequestInterface
* @var \Psr\Http\Message\ServerRequestInterface
*/
public $request;

/**
* Create a new event instance.
*/
public function __construct(RequestInterface $request)
public function __construct(ServerRequestInterface $request)
{
$this->request = $request;
}
Expand Down
Loading

0 comments on commit 2e1cfd2

Please sign in to comment.