Skip to content

Commit 06fba3a

Browse files
committed
Merge branch 'master' of https://github.com/xtwoend/oauth-server
2 parents b2d880e + bd42da5 commit 06fba3a

10 files changed

+92
-19
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
],
1111
"minimum-stability": "dev",
1212
"require": {
13-
"hyperf/database": "^2.1",
14-
"hyperf/db-connection": "^2.1",
13+
"hyperf/database": "^2.2",
14+
"hyperf/db-connection": "^2.2",
1515
"league/oauth2-server": "^8.2",
1616
"phpseclib/phpseclib": "^3.0"
1717
},
@@ -25,7 +25,7 @@
2525
},
2626
"extra": {
2727
"branch-alias": {
28-
"dev-master": "1.0-dev"
28+
"dev-master": "1.1-dev"
2929
},
3030
"hyperf": {
3131
"config": "OAuthServer\\ConfigProvider"

publish/oauth.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
'scopes' => [
77
'public' => 'read all public resource'
88
],
9+
'use_otp_grant' => false,
910
'provider' => 'default', // connection provider
11+
'user_table' => 'users', // user table
1012
'find_by' => 'email' // username check
1113
];

readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## OAuth2 Server for Hyperf Framework
2+
3+
4+
5+
### installation
6+
7+
```
8+
composer require xtwoend/oauth-server
9+
```
10+
11+
12+
### Run on terminal
13+
14+
```
15+
php bin/hyperf.php vendor:publish xtwoend/oauth-server
16+
17+
php bin/hyperf.php migrate
18+
19+
php bin/hyperf.php oauth:key
20+
21+
```

src/AuthorizationServerFactory.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ public function __invoke()
5858
$tokenExpiresIn
5959
);
6060

61-
$server->enableGrantType(
62-
$this->makeOtpGrant(),
63-
$tokenExpiresIn
64-
);
61+
if($this->config->get('oauth.use_otp_grant', false)) {
62+
$server->enableGrantType(
63+
$this->makeOtpGrant(),
64+
$tokenExpiresIn
65+
);
66+
}
6567

6668
$server->enableGrantType(
6769
$this->makeUserGrant(),

src/ConfigProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __invoke(): array
5252
'id' => 'config',
5353
'description' => 'The config for oauth.',
5454
'source' => __DIR__ . '/../publish/oauth.php',
55-
'destination' => BASE_PATH . '/config/oauth.php',
55+
'destination' => BASE_PATH . '/config/autoload/oauth.php',
5656
],
5757
[
5858
'id' => 'migration',
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace OAuthServer\Controller;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Hyperf\HttpServer\Annotation\Controller;
7+
use League\OAuth2\Server\AuthorizationServer;
8+
use OAuthServer\Repositories\TokenRepository;
9+
use Hyperf\HttpServer\Annotation\RequestMapping;
10+
use Hyperf\HttpServer\Contract\RequestInterface;
11+
use Hyperf\HttpServer\Contract\ResponseInterface;
12+
use OAuthServer\Exception\AuthenticationException;
13+
use League\OAuth2\Server\Exception\OAuthServerException;
14+
15+
16+
/**
17+
* @Controller()
18+
*/
19+
class TokenIssueController
20+
{
21+
protected $container;
22+
protected $server;
23+
protected $tokens;
24+
25+
public function __construct(
26+
ContainerInterface $container,
27+
AuthorizationServer $server,
28+
TokenRepository $tokens
29+
) {
30+
$this->container = $container;
31+
$this->server = $server;
32+
$this->tokens = $tokens;
33+
}
34+
35+
/**
36+
* @RequestMapping(path="/oauth/token", methods="post")
37+
*/
38+
public function issueToken(RequestInterface $request, ResponseInterface $response)
39+
{
40+
try {
41+
return $this->server->respondToAccessTokenRequest($request, $response);
42+
} catch (OAuthServerException $e) {
43+
return $e->generateHttpResponse($response);
44+
} catch (\Exception $e) {
45+
throw new AuthenticationException("Unauthorize: {$e->getMessage()}");
46+
}
47+
}
48+
}

src/Repositories/AccessTokenRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEnt
4040
'user_id' => $accessTokenEntity->getUserIdentifier(),
4141
'client_id' => $accessTokenEntity->getClient()->getIdentifier(),
4242
'scopes' => $this->formatScopesForStorage($accessTokenEntity->getScopes()),
43-
'revoked' => false,
43+
'revoked' => 0,
4444
'created_at' => new DateTime(),
4545
'updated_at' => new DateTime(),
4646
'expires_at' => $accessTokenEntity->getExpiryDateTime(),
@@ -58,7 +58,7 @@ public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEnt
5858
*/
5959
public function revokeAccessToken($tokenId)
6060
{
61-
Db::connection(config('oauth.provider', 'default'))->table('oauth_access_tokens')->where('id', $tokenId)->update(['revoked' => true]);
61+
Db::connection(config('oauth.provider', 'default'))->table('oauth_access_tokens')->where('id', $tokenId)->update(['revoked' => 1]);
6262
}
6363

6464
/**

src/Repositories/AuthCodeRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
2525
'user_id' => $authCodeEntity->getUserIdentifier(),
2626
'client_id' => $authCodeEntity->getClient()->getIdentifier(),
2727
'scopes' => $this->formatScopesForStorage($authCodeEntity->getScopes()),
28-
'revoked' => false,
28+
'revoked' => 0,
2929
'expires_at' => $authCodeEntity->getExpiryDateTime(),
3030
];
3131

@@ -37,7 +37,7 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
3737
*/
3838
public function revokeAuthCode($codeId)
3939
{
40-
Db::connection(config('oauth.provider', 'default'))->table('oauth_auth_codes')->where('id', $codeId)->update(['revoked' => true]);
40+
Db::connection(config('oauth.provider', 'default'))->table('oauth_auth_codes')->where('id', $codeId)->update(['revoked' => 1]);
4141
}
4242

4343
/**

src/Repositories/RefreshTokenRepository.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshToken
3333
Db::connection(config('oauth.provider', 'default'))->table('oauth_refresh_tokens')->insert([
3434
'id' => $id = $refreshTokenEntity->getIdentifier(),
3535
'access_token_id' => $accessTokenId = $refreshTokenEntity->getAccessToken()->getIdentifier(),
36-
'revoked' => false,
36+
'revoked' => 0,
3737
'expires_at' => $refreshTokenEntity->getExpiryDateTime(),
3838
]);
3939

@@ -45,16 +45,16 @@ public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshToken
4545
*/
4646
public function revokeRefreshToken($tokenId)
4747
{
48-
Db::connection(config('oauth.provider', 'default'))->table('oauth_refresh_tokens')->where('id', $tokenId)->update(['revoked' => true]);
48+
Db::connection(config('oauth.provider', 'default'))->table('oauth_refresh_tokens')->where('id', $tokenId)->update(['revoked' => 1]);
4949
}
5050

5151
/**
5252
* {@inheritdoc}
5353
*/
5454
public function isRefreshTokenRevoked($tokenId)
5555
{
56-
if ($token = Db::connection(config('oauth.provider', 'default'))->table('oauth_refresh_tokens')->where('id', $tokenId)) {
57-
return $token->revoked;
56+
if ($token = Db::connection(config('oauth.provider', 'default'))->table('oauth_refresh_tokens')->where('id', $tokenId)->first()) {
57+
return (bool) $token->revoked;
5858
}
5959

6060
return true;

src/Repositories/UserRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getUserEntityByUserCredentials(
3535

3636
$query = Db::connection($provider);
3737

38-
$user = $query->table('users')->where(config('oauth.find_by', 'email'), $username)->first();
38+
$user = $query->table(config('oauth.user_table', 'users'))->where(config('oauth.find_by', 'email'), $username)->first();
3939

4040
if (! $user) {
4141
return;
@@ -67,7 +67,7 @@ public function getUserEntityByOtp(
6767

6868
$query = Db::connection($provider);
6969

70-
$user = $query->table('users')->where('phone', $phone)->first();
70+
$user = $query->table(config('oauth.user_table', 'users'))->where('phone', $phone)->first();
7171

7272
if (! $user) {
7373
return;
@@ -89,7 +89,7 @@ public function getUserByProviderUserId($id, $client)
8989
}
9090

9191
$query = Db::connection($provider);
92-
$user = $query->table('users')->find($id);
92+
$user = $query->table(config('oauth.user_table', 'users'))->find($id);
9393

9494
unset($user->password);
9595

0 commit comments

Comments
 (0)