Completed
Pull Request — master (#874)
by Lukáš
04:56
created

AccessTokenTrait::getUserIdentifier()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
/**
3
 * @author      Alex Bilbie <[email protected]>
4
 * @copyright   Copyright (c) Alex Bilbie
5
 * @license     http://mit-license.org/
6
 *
7
 * @link        https://github.com/thephpleague/oauth2-server
8
 */
9
10
namespace League\OAuth2\Server\Entities\Traits;
11
12
use Lcobucci\JWT\Builder;
13
use Lcobucci\JWT\Signer\Key;
14
use Lcobucci\JWT\Signer\Rsa\Sha256;
15
use Lcobucci\JWT\Token;
16
use League\OAuth2\Server\CryptKey;
17
use League\OAuth2\Server\Entities\ClientEntityInterface;
18
use League\OAuth2\Server\Entities\ScopeEntityInterface;
19
20
trait AccessTokenTrait
21
{
22
    /**
23
     * Generate a JWT from the access token
24
     *
25
     * @param CryptKey $privateKey
26
     *
27
     * @return Token
28
     */
29 9
    public function convertToJWT(CryptKey $privateKey)
30
    {
31 9
        return (new Builder())
32 9
            ->setAudience($this->getClient()->getIdentifier())
33 9
            ->setId($this->getIdentifier(), true)
0 ignored issues
show
Bug introduced by
It seems like getIdentifier() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34 9
            ->setIssuedAt(time())
35 9
            ->setNotBefore(time())
36 9
            ->setExpiration($this->getExpiryDateTime()->getTimestamp())
37 9
            ->setSubject($this->getUserIdentifier())
38 9
            ->set('scopes', $this->getScopes())
39 9
            ->sign(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()))
40 9
            ->getToken();
41
    }
42
43
    /**
44
     * Generate a string representation from the access token
45
     */
46 9
    public function convertToAccessToken(CryptKey $privateKey): string
47
    {
48 9
        return (string) $this->convertToJWT($privateKey);
49
    }
50
51
    /**
52
     * @return ClientEntityInterface
53
     */
54
    abstract public function getClient();
55
56
    /**
57
     * @return \DateTime
58
     */
59
    abstract public function getExpiryDateTime();
60
61
    /**
62
     * @return string|int
63
     */
64
    abstract public function getUserIdentifier();
65
66
    /**
67
     * @return ScopeEntityInterface[]
68
     */
69
    abstract public function getScopes();
70
}
71