ResourceOwnerPasswordCredentialsGrantType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A grant() 0 12 2
A __construct() 0 3 1
A associatedResponseTypes() 0 3 1
A prepareResponse() 0 2 1
A checkRequest() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\ResourceOwnerPasswordCredentialsGrant;
15
16
use OAuth2Framework\Component\Core\Message\OAuth2Error;
17
use OAuth2Framework\Component\Core\Util\RequestBodyParser;
18
use OAuth2Framework\Component\TokenEndpoint\GrantType;
19
use OAuth2Framework\Component\TokenEndpoint\GrantTypeData;
20
use Psr\Http\Message\ServerRequestInterface;
21
22
final class ResourceOwnerPasswordCredentialsGrantType implements GrantType
23
{
24
    /**
25
     * @var ResourceOwnerPasswordCredentialManager
26
     */
27
    private $resourceOwnerPasswordCredentialManager;
28
29
    public function __construct(ResourceOwnerPasswordCredentialManager $resourceOwnerPasswordCredentialManager)
30
    {
31
        $this->resourceOwnerPasswordCredentialManager = $resourceOwnerPasswordCredentialManager;
32
    }
33
34
    public function associatedResponseTypes(): array
35
    {
36
        return [];
37
    }
38
39
    public function name(): string
40
    {
41
        return 'password';
42
    }
43
44
    public function checkRequest(ServerRequestInterface $request): void
45
    {
46
        $parameters = RequestBodyParser::parseFormUrlEncoded($request);
47
        $requiredParameters = ['username', 'password'];
48
49
        $diff = array_diff($requiredParameters, array_keys($parameters));
50
        if (0 !== \count($diff)) {
51
            throw OAuth2Error::invalidRequest(\Safe\sprintf('Missing grant type parameter(s): %s.', implode(', ', $diff)));
52
        }
53
    }
54
55
    public function prepareResponse(ServerRequestInterface $request, GrantTypeData $grantTypeData): void
56
    {
57
    }
58
59
    public function grant(ServerRequestInterface $request, GrantTypeData $grantTypeData): void
60
    {
61
        $parameters = RequestBodyParser::parseFormUrlEncoded($request);
62
        $username = $parameters['username'];
63
        $password = $parameters['password'];
64
65
        $resourceOwnerId = $this->resourceOwnerPasswordCredentialManager->findResourceOwnerIdWithUsernameAndPassword($username, $password);
66
        if (null === $resourceOwnerId) {
67
            throw OAuth2Error::invalidGrant('Invalid username and password combination.');
68
        }
69
70
        $grantTypeData->setResourceOwnerId($resourceOwnerId);
71
    }
72
}
73