Passed
Pull Request — master (#32)
by
unknown
02:05
created

IdTokenResponse::isOpenIDRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * @author Steve Rhoades <[email protected]>
4
 * @license http://opensource.org/licenses/MIT MIT
5
 */
6
namespace OpenIDConnectServer;
7
8
use \DateTimeImmutable;
9
use OpenIDConnectServer\Repositories\IdentityProviderInterface;
10
use OpenIDConnectServer\Entities\ClaimSetInterface;
11
use League\OAuth2\Server\Entities\UserEntityInterface;
12
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
13
use League\OAuth2\Server\Entities\ScopeEntityInterface;
14
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
15
use Lcobucci\JWT\Configuration;
16
17
class IdTokenResponse extends BearerTokenResponse
18
{
19
    /**
20
     * @var IdentityProviderInterface
21
     */
22
    protected $identityProvider;
23
24
    /**
25
     * @var ClaimExtractor
26
     */
27
    protected $claimExtractor;
28
29
    /**
30
     * @var Configuration
31
     */
32
    private $config;
33
34
    public function __construct(
35
        IdentityProviderInterface $identityProvider,
36
        ClaimExtractor $claimExtractor,
37
        Configuration $config
38
    ) {
39
        $this->identityProvider = $identityProvider;
40
        $this->claimExtractor   = $claimExtractor;
41
        $this->config           = $config;
42
    }
43
44
    protected function getBuilder(AccessTokenEntityInterface $accessToken, UserEntityInterface $userEntity)
45
    {
46
        $dateTimeImmutableObject = new DateTimeImmutable();
47
48
        // Add required id_token claims
49
        $builder = $this->config
50
            ->builder()
51
            ->permittedFor($accessToken->getClient()->getIdentifier())
0 ignored issues
show
Bug introduced by
$accessToken->getClient()->getIdentifier() of type string is incompatible with the type Lcobucci\JWT\list expected by parameter $audiences of Lcobucci\JWT\Builder::permittedFor(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            ->permittedFor(/** @scrutinizer ignore-type */ $accessToken->getClient()->getIdentifier())
Loading history...
52
            ->issuedBy('https://' . $_SERVER['HTTP_HOST'])
53
            ->issuedAt($dateTimeImmutableObject)
54
            ->expiresAt($dateTimeImmutableObject->setTimestamp(
55
                $accessToken->getExpiryDateTime()->getTimestamp(),
56
            ))
57
            ->relatedTo($userEntity->getIdentifier());
58
59
        return $builder;
60
    }
61
62
    /**
63
     * @param AccessTokenEntityInterface $accessToken
64
     * @return array
65
     */
66
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
67
    {
68
        if (false === $this->isOpenIDRequest($accessToken->getScopes())) {
69
            return [];
70
        }
71
72
        /** @var UserEntityInterface $userEntity */
73
        $userEntity = $this->identityProvider->getUserEntityByIdentifier($accessToken->getUserIdentifier());
74
75
        if (false === is_a($userEntity, UserEntityInterface::class)) {
76
            throw new \RuntimeException('UserEntity must implement UserEntityInterface');
77
        } else if (false === is_a($userEntity, ClaimSetInterface::class)) {
78
            throw new \RuntimeException('UserEntity must implement ClaimSetInterface');
79
        }
80
81
        // Add required id_token claims
82
        $builder = $this->getBuilder($accessToken, $userEntity);
83
84
        // Need a claim factory here to reduce the number of claims by provided scope.
85
        $claims = $this->claimExtractor->extract($accessToken->getScopes(), $userEntity->getClaims());
86
87
        foreach ($claims as $claimName => $claimValue) {
88
            $builder = $builder->withClaim($claimName, $claimValue);
89
        }
90
91
        $token = $builder->getToken($this->config->signer(), $this->config->signingKey());
92
93
        return [
94
            'id_token' => $token->toString()
95
        ];
96
    }
97
98
    /**
99
     * @param ScopeEntityInterface[] $scopes
100
     * @return bool
101
     */
102
    private function isOpenIDRequest($scopes)
103
    {
104
        // Verify scope and make sure openid exists.
105
        $valid  = false;
106
107
        foreach ($scopes as $scope) {
108
            if ($scope->getIdentifier() === 'openid') {
109
                $valid = true;
110
                break;
111
            }
112
        }
113
114
        return $valid;
115
    }
116
117
}
118