Skip to content

Commit 2e1cfd2

Browse files
committed
added JwtGuard
1 parent 22465d9 commit 2e1cfd2

15 files changed

+598
-136
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Gate 可以通过注入 `HyperfExt\Auth\Contracts\Access\GateManagerInterface`
2121

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

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

2628
```shell script

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
},
4949
"suggest": {
5050
"hyperf/session": "Required to use session guard.",
51-
"hyperf-ext/cookie": "Required to use session guard."
51+
"hyperf-ext/cookie": "Required to use session guard.",
52+
"hyperf-ext/jwt": "Required to use JWT guard."
5253
},
5354
"config": {
5455
"sort-packages": true

publish/auth.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
|
2121
*/
2222

23-
'defaults' => [
23+
'default' => [
2424
'guard' => 'web',
2525
'passwords' => 'users',
2626
],
@@ -44,17 +44,13 @@
4444
'web' => [
4545
'driver' => \HyperfExt\Auth\Guards\SessionGuard::class,
4646
'provider' => 'users',
47-
'options' => [
48-
'name' => 'session',
49-
],
47+
'options' => [],
5048
],
5149

5250
'api' => [
53-
'driver' => \HyperfExt\Auth\Guards\TokenGuard::class,
51+
'driver' => \HyperfExt\Auth\Guards\JwtGuard::class,
5452
'provider' => 'users',
55-
'options' => [
56-
'hash' => false,
57-
],
53+
'options' => [],
5854
],
5955
],
6056

src/AuthManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function guard(?string $name = null): GuardInterface
7575
*/
7676
public function getDefaultDriver(): string
7777
{
78-
return $this->config->get('auth.defaults.guard');
78+
return $this->config->get('auth.default.guard');
7979
}
8080

8181
/**
@@ -95,7 +95,7 @@ public function shouldUse(string $name): void
9595
*/
9696
public function setDefaultDriver(string $name)
9797
{
98-
$this->config->set('auth.defaults.guard', $name);
98+
$this->config->set('auth.default.guard', $name);
9999
}
100100

101101
/**
@@ -127,7 +127,7 @@ public function resolveUsersUsing(Closure $userResolver)
127127
*/
128128
public function createUserProvider(?string $provider = null): ?UserProviderInterface
129129
{
130-
$provider = $provider ?: $this->config->get('auth.defaults.provider', null);
130+
$provider = $provider ?: $this->config->get('auth.default.provider', null);
131131

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

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

171-
return make($config['driver'], compact('provider', 'options'));
171+
return make($config['driver'], compact('provider', 'name', 'options'));
172172
}
173173

174174
protected function getUserResolverClosure()

src/Commands/GenPolicyCommand.php renamed to src/Commands/GenAuthPolicyCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\Console\Input\InputArgument;
1818
use Symfony\Component\Console\Input\InputOption;
1919

20-
class GenPolicyCommand extends HyperfCommand
20+
class GenAuthPolicyCommand extends HyperfCommand
2121
{
2222
/**
2323
* @var \Hyperf\Contract\ConfigInterface
@@ -26,7 +26,7 @@ class GenPolicyCommand extends HyperfCommand
2626

2727
public function __construct(ConfigInterface $config)
2828
{
29-
parent::__construct('gen:policy');
29+
parent::__construct('gen:auth-policy');
3030
$this->config = $config;
3131
}
3232

@@ -49,7 +49,7 @@ public function handle()
4949
$option = new PolicyOption();
5050
$option
5151
->setPath($this->getOption('path', 'app/Policy'))
52-
->setGuard($this->getOption('guard'), $this->config->get('auth.defaults.guard', null))
52+
->setGuard($this->getOption('guard', $this->config->get('auth.default.guard', null)))
5353
->setModel($this->getOption('model'));
5454
$this->createPolicy($name, $option);
5555
}
@@ -138,7 +138,7 @@ protected function getNamespace(string $name): string
138138
*/
139139
protected function getUserProviderModel(?string $guard = null): ?string
140140
{
141-
$guard = $guard ?: $this->config->get('auth.defaults.guard');
141+
$guard = $guard ?: $this->config->get('auth.default.guard');
142142

143143
return $this->config->get(
144144
'auth.providers.' . $this->config->get('auth.guards.' . $guard . '.provider') . '.options.model'

src/ConfigProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace HyperfExt\Auth;
1212

1313
use HyperfExt\Auth\Access\GateManager;
14-
use HyperfExt\Auth\Commands\GenPolicyCommand;
14+
use HyperfExt\Auth\Commands\GenAuthPolicyCommand;
1515
use HyperfExt\Auth\Contracts\Access\GateManagerInterface;
1616
use HyperfExt\Auth\Contracts\AuthManagerInterface;
1717
use HyperfExt\Auth\Contracts\PasswordBrokerManagerInterface;
@@ -38,7 +38,7 @@ public function __invoke(): array
3838
],
3939
],
4040
'commands' => [
41-
GenPolicyCommand::class,
41+
GenAuthPolicyCommand::class,
4242
],
4343
'publish' => [
4444
[

src/Contracts/AuthManagerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface AuthManagerInterface
1515
/**
1616
* Get a guard instance by name.
1717
*
18-
* @return \HyperfExt\Auth\Contracts\GuardInterface|\HyperfExt\Auth\Contracts\StatefulGuardInterface
18+
* @return \HyperfExt\Auth\Contracts\GuardInterface|\HyperfExt\Auth\Contracts\StatefulGuardInterface|\HyperfExt\Auth\Contracts\StatelessGuardInterface
1919
*/
2020
public function guard(?string $name = null): GuardInterface;
2121

src/Contracts/StatefulGuardInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ interface StatefulGuardInterface extends GuardInterface
1414
{
1515
/**
1616
* Attempt to authenticate a user using the given credentials.
17+
* @return bool|mixed
1718
*/
18-
public function attempt(array $credentials = [], bool $remember = false): bool;
19+
public function attempt(array $credentials = [], bool $remember = false);
1920

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

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

5557
/**
5658
* Log the user out of the application.
59+
* @return mixed|void
5760
*/
58-
public function logout(): void;
61+
public function logout();
5962
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of hyperf-ext/auth.
6+
*
7+
* @link https://github.com/hyperf-ext/auth
8+
* @contact eric@zhu.email
9+
* @license https://github.com/hyperf-ext/auth/blob/master/LICENSE
10+
*/
11+
namespace HyperfExt\Auth\Contracts;
12+
13+
interface StatelessGuardInterface extends GuardInterface
14+
{
15+
/**
16+
* Attempt to authenticate the user using the given credentials and return the token.
17+
*
18+
* @return bool|mixed
19+
*/
20+
public function attempt(array $credentials = [], bool $login = true);
21+
22+
/**
23+
* Log a user into the application without sessions or cookies.
24+
*/
25+
public function once(array $credentials = []): bool;
26+
27+
/**
28+
* Log a user into the application, create a token for the user.
29+
*
30+
* @return mixed
31+
*/
32+
public function login(AuthenticatableInterface $user);
33+
34+
/**
35+
* Log the given user ID into the application.
36+
*
37+
* @param mixed $id
38+
*
39+
* @return false|mixed
40+
*/
41+
public function loginUsingId($id);
42+
43+
/**
44+
* Log the given user ID into the application without sessions or cookies.
45+
*
46+
* @param mixed $id
47+
*/
48+
public function onceUsingId($id): bool;
49+
50+
/**
51+
* Log the user out of the application, thus invalidating the token.
52+
*/
53+
public function logout(bool $forceForever = false);
54+
55+
/**
56+
* Refresh the token.
57+
*
58+
* @return mixed
59+
*/
60+
public function refresh(bool $forceForever = false);
61+
62+
/**
63+
* Invalidate the token.
64+
*
65+
* @return mixed
66+
*/
67+
public function invalidate(bool $forceForever = false);
68+
}

src/EventHelpers.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of hyperf-ext/auth.
6+
*
7+
* @link https://github.com/hyperf-ext/auth
8+
* @contact eric@zhu.email
9+
* @license https://github.com/hyperf-ext/auth/blob/master/LICENSE
10+
*/
11+
namespace HyperfExt\Auth;
12+
13+
use HyperfExt\Auth\Contracts\AuthenticatableInterface;
14+
use HyperfExt\Auth\Events\Attempting;
15+
use HyperfExt\Auth\Events\Authenticated;
16+
use HyperfExt\Auth\Events\CurrentDeviceLogout;
17+
use HyperfExt\Auth\Events\Failed;
18+
use HyperfExt\Auth\Events\Login;
19+
use HyperfExt\Auth\Events\Logout;
20+
use HyperfExt\Auth\Events\OtherDeviceLogout;
21+
use HyperfExt\Auth\Events\Validated;
22+
23+
trait EventHelpers
24+
{
25+
/**
26+
* Fire the attempt event with the arguments.
27+
*/
28+
protected function dispatchAttemptingEvent(array $credentials, bool $remember = false): void
29+
{
30+
$this->eventDispatcher->dispatch(new Attempting(
31+
$this->name,
32+
$credentials,
33+
$remember
34+
));
35+
}
36+
37+
/**
38+
* Fires the validated event if the dispatcher is set.
39+
*/
40+
protected function dispatchValidatedEvent(AuthenticatableInterface $user)
41+
{
42+
$this->eventDispatcher->dispatch(new Validated(
43+
$this->name,
44+
$user
45+
));
46+
}
47+
48+
/**
49+
* Fire the login event if the dispatcher is set.
50+
*/
51+
protected function dispatchLoginEvent(AuthenticatableInterface $user, bool $remember = false): void
52+
{
53+
$this->eventDispatcher->dispatch(new Login(
54+
$this->name,
55+
$user,
56+
$remember
57+
));
58+
}
59+
60+
/**
61+
* Fire the authenticated event if the dispatcher is set.
62+
*/
63+
protected function dispatchAuthenticatedEvent(AuthenticatableInterface $user): void
64+
{
65+
$this->eventDispatcher->dispatch(new Authenticated(
66+
$this->name,
67+
$user
68+
));
69+
}
70+
71+
/**
72+
* Fire the logout event if the dispatcher is set.
73+
*/
74+
protected function dispatchLogoutEvent(AuthenticatableInterface $user): void
75+
{
76+
$this->eventDispatcher->dispatch(new Logout(
77+
$this->name,
78+
$user
79+
));
80+
}
81+
82+
/**
83+
* Fire the current device logout event if the dispatcher is set.
84+
*/
85+
protected function dispatchCurrentDeviceLogoutEvent(AuthenticatableInterface $user): void
86+
{
87+
$this->eventDispatcher->dispatch(new CurrentDeviceLogout(
88+
$this->name,
89+
$user
90+
));
91+
}
92+
93+
/**
94+
* Fire the other device logout event if the dispatcher is set.
95+
*/
96+
protected function dispatchOtherDeviceLogoutEvent(AuthenticatableInterface $user): void
97+
{
98+
$this->eventDispatcher->dispatch(new OtherDeviceLogout(
99+
$this->name,
100+
$user
101+
));
102+
}
103+
104+
/**
105+
* Fire the failed authentication attempt event with the given arguments.
106+
*/
107+
protected function dispatchFailedEvent(?AuthenticatableInterface $user, array $credentials): void
108+
{
109+
$this->eventDispatcher->dispatch(new Failed(
110+
$this->name,
111+
$user,
112+
$credentials
113+
));
114+
}
115+
}

0 commit comments

Comments
 (0)