JWTFactory::getForAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
crap 1
1
<?php
2
3
4
namespace Mvdstam\Oauth2ServerLaravel\Factories;
5
6
7
use Carbon\Carbon;
8
use Lcobucci\JWT\Builder;
9
use Lcobucci\JWT\Signer\Key;
10
use Lcobucci\JWT\Signer\Rsa\Sha256;
11
use League\OAuth2\Server\CryptKey;
12
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
13
use Mvdstam\Oauth2ServerLaravel\Contracts\JWTFactoryInterface;
14
15
class JWTFactory implements JWTFactoryInterface
16
{
17
18
    /**
19
     * @inheritdoc
20
     */
21 11
    public function getForAccessToken(AccessTokenEntityInterface $accessToken, CryptKey $privateKey)
22
    {
23 11
        $now = Carbon::now();
24
25 11
        return (new Builder)
26 11
            ->setIssuer(url('/'))
0 ignored issues
show
Bug introduced by
It seems like url('/') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Lcobucci\JWT\Builder::setIssuer() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
27 11
            ->setIssuedAt($now->timestamp)
28 11
            ->setNotBefore($now->timestamp)
29 11
            ->setId($accessToken->getIdentifier())
30 11
            ->setExpiration($accessToken->getExpiryDateTime()->getTimestamp())
31 11
            ->setAudience($accessToken->getClient()->getIdentifier())
32 11
            ->setSubject($accessToken->getUserIdentifier())
33 11
            ->set('scopes', implode(',', $this->getScopesAsList($accessToken)))
34 11
            ->sign(new Sha256, new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()))
35 11
            ->getToken();
36
    }
37
38
39
    /**
40
     * @param AccessTokenEntityInterface $accessToken
41
     * @return string[]
42
     */
43 11
    public function getScopesAsList(AccessTokenEntityInterface $accessToken)
44
    {
45 11
        $scopes = [];
46 11
        foreach ($accessToken->getScopes() as $scope) {
47 4
            $scopes[] = $scope->getIdentifier();
48
        }
49
50 11
        return $scopes;
51
    }
52
53
}
54