-
Notifications
You must be signed in to change notification settings - Fork 141
/
AccessTokenController.php
103 lines (86 loc) · 3.36 KB
/
AccessTokenController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Dusterio\LumenPassport\Http\Controllers;
use Laravel\Passport\Passport;
use Laravel\Passport\Token;
use Laminas\Diactoros\Response as Psr7Response;
use Psr\Http\Message\ServerRequestInterface;
use Dusterio\LumenPassport\LumenPassport;
/**
* Class AccessTokenController
* @package Dusterio\LumenPassport\Http\Controllers
*/
class AccessTokenController extends \Laravel\Passport\Http\Controllers\AccessTokenController
{
/**
* Authorize a client to access the user's account.
*
* @param ServerRequestInterface $request
* @return Response
*/
public function issueToken(ServerRequestInterface $request)
{
$response = $this->withErrorHandling(function () use ($request) {
$input = (array) $request->getParsedBody();
$clientId = isset($input['client_id']) ? $input['client_id'] : null;
// Overwrite password grant at the last minute to add support for customized TTLs
$this->server->enableGrantType(
$this->makePasswordGrant(), LumenPassport::tokensExpireIn(null, $clientId)
);
return $this->server->respondToAccessTokenRequest($request, new Psr7Response);
});
if ($response->getStatusCode() < 200 || $response->getStatusCode() > 299) {
return $response;
}
$payload = json_decode($response->getBody()->__toString(), true);
if (isset($payload['access_token'])) {
/* @deprecated the jwt property will be removed in a future Laravel Passport release */
$token = $this->jwt->parse($payload['access_token']);
if (method_exists($token, 'getClaim')) {
$tokenId = $token->getClaim('jti');
} else if (method_exists($token, 'claims')) {
$tokenId = $token->claims()->get('jti');
} else {
throw new \RuntimeException('This package is not compatible with the Laravel Passport version used');
}
$token = $this->tokens->find($tokenId);
if (!$token instanceof Token) {
return $response;
}
if ($token->client->firstParty() && LumenPassport::$allowMultipleTokens) {
// We keep previous tokens for password clients
} else {
$this->revokeOrDeleteAccessTokens($token, $tokenId);
}
}
return $response;
}
/**
* Create and configure a Password grant instance.
*
* @return \League\OAuth2\Server\Grant\PasswordGrant
*/
private function makePasswordGrant()
{
$grant = new \League\OAuth2\Server\Grant\PasswordGrant(
app()->make(\Laravel\Passport\Bridge\UserRepository::class),
app()->make(\Laravel\Passport\Bridge\RefreshTokenRepository::class)
);
$grant->setRefreshTokenTTL(Passport::refreshTokensExpireIn());
return $grant;
}
/**
* Revoke the user's other access tokens for the client.
*
* @param Token $token
* @param string $tokenId
* @return void
*/
protected function revokeOrDeleteAccessTokens(Token $token, $tokenId)
{
$query = Token::where('user_id', $token->user_id)->where('client_id', $token->client_id);
if ($tokenId) {
$query->where('id', '<>', $tokenId);
}
$query->update(['revoked' => true]);
}
}