Skip to content

Commit 643afe8

Browse files
committed
Revert ./composer.json and remove useless comment and fix code style
1 parent 7059e38 commit 643afe8

23 files changed

+121
-315
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ composer require swoft/auth
1212

1313
## Document [wiki](https://github.com/aprchen/swoft-auth/wiki)
1414

15-
now
15+
Now
1616
- BasicAuth
1717
- BearerToken (JWT)
1818
- Acl
1919

20-
feature
21-
- oauth 2.0
20+
Future
21+
- Oauth 2.0
2222

2323

2424

2525
## Unit testing
2626

2727
```bash
28-
phpunit
28+
composer test
2929
```
3030

3131
## LICENSE

composer.json

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
11
{
2-
"name": "swoft/auth",
3-
"type": "library",
4-
"keywords": [
5-
"php",
6-
"swoole",
7-
"swoft",
8-
"auth",
9-
"auth-manager",
10-
"jwt",
11-
"acl"
12-
],
13-
"description": "auth compoment for swoft",
14-
"license": "Apache-2.0",
15-
"require": {
16-
"swoft/http-server": "^1.0",
17-
"firebase/php-jwt": "^5.0",
18-
"psr/simple-cache": "^1.0",
19-
"swoft/framework": "^1.0",
20-
"swoft/cache": "^1.0"
21-
},
22-
"autoload": {
23-
"psr-4": {
24-
"Swoft\\Auth\\": "src/"
2+
"name": "swoft/auth",
3+
"type": "library",
4+
"keywords": [
5+
"php",
6+
"swoole",
7+
"swoft",
8+
"auth",
9+
"auth-manager",
10+
"jwt",
11+
"acl"
12+
],
13+
"description": "Auth compoment for swoft",
14+
"license": "Apache-2.0",
15+
"require": {
16+
"swoft/http-server": "^1.0",
17+
"firebase/php-jwt": "^5.0",
18+
"psr/simple-cache": "^1.0",
19+
"swoft/framework": "^1.0",
20+
"swoft/cache": "^1.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Swoft\\Auth\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"SwoftTest\\Auth\\": "test/Cases"
30+
}
31+
},
32+
"repositories": [
33+
{
34+
"type": "composer",
35+
"url": "https://packagist.laravel-china.org"
36+
}
37+
],
38+
"require-dev": {
39+
"eaglewu/swoole-ide-helper": "dev-master",
40+
"phpunit/phpunit": "^5.7",
41+
"friendsofphp/php-cs-fixer": "^2.11"
42+
},
43+
"scripts": {
44+
"test": "./vendor/bin/phpunit -c phpunit.xml",
45+
"cs-fix": "./vendor/bin/php-cs-fixer fix $1"
2546
}
26-
},
27-
"autoload-dev": {
28-
"psr-4": {
29-
"SwoftTest\\Auth\\": "test/Cases"
30-
}
31-
},
32-
"repositories": [
33-
{
34-
"type": "composer",
35-
"url": "https://packagist.laravel-china.org"
36-
}
37-
],
38-
"require-dev": {
39-
"eaglewu/swoole-ide-helper": "dev-master",
40-
"phpunit/phpunit": "^5.7",
41-
"friendsofphp/php-cs-fixer": "^2.11"
42-
},
43-
"scripts": {
44-
"test": "./vendor/bin/phpunit -c phpunit.xml"
45-
}
4647
}

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</testsuites>
1616
<filter>
1717
<whitelist processUncoveredFilesFromWhitelist="true">
18-
<directory suffix=".php">./app</directory>
18+
<directory suffix=".php">./src</directory>
1919
</whitelist>
2020
</filter>
2121
</phpunit>

src/AuthManager.php

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
use Swoft\Core\RequestContext;
2626
use Swoft\Exception\RuntimeException;
2727

28-
/**
29-
* Class AuthManager
30-
* @package Swoft\Auth
31-
*/
3228
class AuthManager implements AuthManagerInterface
3329
{
3430
/**
@@ -66,7 +62,7 @@ class AuthManager implements AuthManagerInterface
6662
*/
6763
private $tokenParser;
6864

69-
public function getSessionDuration()
65+
public function getSessionDuration(): int
7066
{
7167
return $this->sessionDuration;
7268
}
@@ -77,54 +73,41 @@ public function setSessionDuration($time)
7773
}
7874

7975
/**
80-
* @return AuthSession;
76+
* @return AuthSession|mixed
8177
*/
8278
public function getSession()
8379
{
8480
return RequestContext::getContextDataByKey(AuthConstants::AUTH_SESSION);
8581
}
8682

87-
/**
88-
* @param AuthSession $session
89-
*/
9083
public function setSession(AuthSession $session)
9184
{
9285
RequestContext::setContextData([AuthConstants::AUTH_SESSION => $session]);
9386
}
9487

9588
/**
96-
* @return bool
97-
*
9889
* Check if a user is currently logged in
9990
*/
100-
public function isLoggedIn()
91+
public function isLoggedIn(): bool
10192
{
10293
return $this->getSession() instanceof AuthSession;
10394
}
10495

105-
/**
106-
* @param $accountTypeName
107-
* @param array $data
108-
* @return AuthSession
109-
*/
11096
public function login(string $accountTypeName, array $data): AuthSession
11197
{
112-
if (!$account = $this->getAccountType($accountTypeName)) {
98+
if (! $account = $this->getAccountType($accountTypeName)) {
11399
throw new AuthException(ErrorCode::AUTH_INVALID_ACCOUNT_TYPE);
114100
}
115101
$result = $account->login($data);
116-
if (!$result instanceof AuthResult || $result->getIdentity() == '') {
102+
if (! $result instanceof AuthResult || $result->getIdentity() === '') {
117103
throw new AuthException(ErrorCode::AUTH_LOGIN_FAILED);
118104
}
119105
$session = $this->generateSession($accountTypeName, $result->getIdentity(), $result->getExtendedData());
120106
$this->setSession($session);
121107
if ($this->cacheEnable === true) {
122108
try {
123-
$this->getCacheClient()->set(
124-
$this->getCacheKey($session->getIdentity(), $session->getExtendedData()),
125-
$session->getToken(),
126-
$session->getExpirationTime()
127-
);
109+
$this->getCacheClient()
110+
->set($this->getCacheKey($session->getIdentity(), $session->getExtendedData()), $session->getToken(), $session->getExpirationTime());
128111
} catch (InvalidArgumentException $e) {
129112
$err = sprintf('%s Invalid Argument : %s', $session->getIdentity(), $e->getMessage());
130113
throw new AuthException(ErrorCode::POST_DATA_NOT_PROVIDED, $err);
@@ -133,7 +116,7 @@ public function login(string $accountTypeName, array $data): AuthSession
133116
return $session;
134117
}
135118

136-
protected function getCacheKey(string $identity, array $extendedData)
119+
protected function getCacheKey(string $identity, array $extendedData): string
137120
{
138121
if (empty($extendedData)) {
139122
return $this->prefix . $identity;
@@ -142,19 +125,12 @@ protected function getCacheKey(string $identity, array $extendedData)
142125
return $this->prefix . $identity . '.' . md5($str);
143126
}
144127

145-
/**
146-
* @param string $accountTypeName
147-
* @param string $identity
148-
* @param array $data
149-
* @return AuthSession
150-
*/
151-
public function generateSession(string $accountTypeName, string $identity, array $data = [])
128+
public function generateSession(string $accountTypeName, string $identity, array $data = []): AuthSession
152129
{
153130
$startTime = time();
154131
$exp = $startTime + (int)$this->sessionDuration;
155132
$session = new AuthSession();
156-
$session
157-
->setExtendedData($data)
133+
$session->setExtendedData($data)
158134
->setExpirationTime($exp)
159135
->setCreateTime($startTime)
160136
->setIdentity($identity)
@@ -171,45 +147,42 @@ public function generateSession(string $accountTypeName, string $identity, array
171147
*/
172148
public function getAccountType($name)
173149
{
174-
if (!App::hasBean($name)) {
150+
if (! App::hasBean($name)) {
175151
return null;
176152
}
177153
$account = App::getBean($name);
178-
if (!$account instanceof AccountTypeInterface) {
154+
if (! $account instanceof AccountTypeInterface) {
179155
return null;
180156
}
181157
return $account;
182158
}
183159

184160
/**
185-
* @return TokenParserInterface
161+
* @throws RuntimeException When TokenParser missing or error.
186162
*/
187163
public function getTokenParser(): TokenParserInterface
188164
{
189-
if (!$this->tokenParser instanceof TokenParserInterface) {
190-
if (!App::hasBean($this->tokenParserClass)) {
165+
if (! $this->tokenParser instanceof TokenParserInterface) {
166+
if (! App::hasBean($this->tokenParserClass)) {
191167
throw new RuntimeException('Can`t find tokenParserClass');
192168
}
193169
$tokenParser = App::getBean($this->tokenParserClass);
194-
if (!$tokenParser instanceof TokenParserInterface) {
170+
if (! $tokenParser instanceof TokenParserInterface) {
195171
throw new RuntimeException("TokenParser need implements Swoft\Auth\Mapping\TokenParserInterface ");
196172
}
197173
$this->tokenParser = $tokenParser;
198174
}
199175
return $this->tokenParser;
200176
}
201177

202-
/**
203-
* @return CacheInterface
204-
*/
205-
public function getCacheClient()
178+
public function getCacheClient(): CacheInterface
206179
{
207-
if (!$this->cache instanceof CacheInterface) {
208-
if (!App::hasBean($this->cacheClass)) {
180+
if (! $this->cache instanceof CacheInterface) {
181+
if (! App::hasBean($this->cacheClass)) {
209182
throw new RuntimeException('Can`t find cacheClass');
210183
}
211184
$cache = App::getBean($this->cacheClass);
212-
if (!$cache instanceof CacheInterface) {
185+
if (! $cache instanceof CacheInterface) {
213186
throw new RuntimeException('CacheClient need implements Psr\SimpleCache\CacheInterface');
214187
}
215188
$this->cache = $cache;
@@ -218,8 +191,6 @@ public function getCacheClient()
218191
}
219192

220193
/**
221-
* @param $token
222-
* @return bool
223194
* @throws AuthException
224195
*/
225196
public function authenticateToken(string $token): bool
@@ -231,26 +202,27 @@ public function authenticateToken(string $token): bool
231202
throw new AuthException(ErrorCode::AUTH_TOKEN_INVALID);
232203
}
233204

234-
if (!$session) {
205+
if (! $session) {
235206
return false;
236207
}
237208

238209
if ($session->getExpirationTime() < time()) {
239210
throw new AuthException(ErrorCode::AUTH_SESSION_EXPIRED);
240211
}
241212

242-
if (!$account = $this->getAccountType($session->getAccountTypeName())) {
213+
if (! $account = $this->getAccountType($session->getAccountTypeName())) {
243214
throw new AuthException(ErrorCode::AUTH_SESSION_INVALID);
244215
}
245216

246-
if (!$account->authenticate($session->getIdentity())) {
217+
if (! $account->authenticate($session->getIdentity())) {
247218
throw new AuthException(ErrorCode::AUTH_TOKEN_INVALID);
248219
}
249220

250221
if ($this->cacheEnable === true) {
251222
try {
252-
$cache = $this->getCacheClient()->get($this->getCacheKey($session->getIdentity(), $session->getExtendedData()));
253-
if (!$cache || $cache !== $token) {
223+
$cache = $this->getCacheClient()
224+
->get($this->getCacheKey($session->getIdentity(), $session->getExtendedData()));
225+
if (! $cache || $cache !== $token) {
254226
throw new AuthException(ErrorCode::AUTH_TOKEN_INVALID);
255227
}
256228
} catch (InvalidArgumentException $e) {

0 commit comments

Comments
 (0)