-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceOwnerPasswordCredentialsGrantType.php
70 lines (56 loc) · 2.29 KB
/
ResourceOwnerPasswordCredentialsGrantType.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
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace OAuth2Framework\Component\ResourceOwnerPasswordCredentialsGrant;
use function Safe\sprintf;
use OAuth2Framework\Component\Core\Message\OAuth2Error;
use OAuth2Framework\Component\Core\Util\RequestBodyParser;
use OAuth2Framework\Component\TokenEndpoint\GrantType;
use OAuth2Framework\Component\TokenEndpoint\GrantTypeData;
use Psr\Http\Message\ServerRequestInterface;
final class ResourceOwnerPasswordCredentialsGrantType implements GrantType
{
private ResourceOwnerPasswordCredentialManager $resourceOwnerPasswordCredentialManager;
public function __construct(ResourceOwnerPasswordCredentialManager $resourceOwnerPasswordCredentialManager)
{
$this->resourceOwnerPasswordCredentialManager = $resourceOwnerPasswordCredentialManager;
}
public function associatedResponseTypes(): array
{
return [];
}
public function name(): string
{
return 'password';
}
public function checkRequest(ServerRequestInterface $request): void
{
$parameters = RequestBodyParser::parseFormUrlEncoded($request);
$requiredParameters = ['username', 'password'];
$diff = array_diff($requiredParameters, array_keys($parameters));
if (0 !== \count($diff)) {
throw OAuth2Error::invalidRequest(sprintf('Missing grant type parameter(s): %s.', implode(', ', $diff)));
}
}
public function prepareResponse(ServerRequestInterface $request, GrantTypeData $grantTypeData): void
{
}
public function grant(ServerRequestInterface $request, GrantTypeData $grantTypeData): void
{
$parameters = RequestBodyParser::parseFormUrlEncoded($request);
$username = $parameters['username'];
$password = $parameters['password'];
$resourceOwnerId = $this->resourceOwnerPasswordCredentialManager->findResourceOwnerIdWithUsernameAndPassword($username, $password);
if (null === $resourceOwnerId) {
throw OAuth2Error::invalidGrant('Invalid username and password combination.');
}
$grantTypeData->setResourceOwnerId($resourceOwnerId);
}
}