Passed
Push — issue#785 ( 357acf...d512c5 )
by Guilherme
04:52
created

AccessToken::getAccessToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 2
rs 9.3142
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\Storage;
12
13
use LoginCidadao\CoreBundle\Model\PersonInterface;
14
use LoginCidadao\OAuthBundle\Entity\Client;
15
use LoginCidadao\OAuthBundle\Model\ClientInterface;
16
use LoginCidadao\OpenIDBundle\Manager\ClientManager;
17
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
18
use OAuth2\ServerBundle\Storage\AccessToken as BaseClass;
19
use OAuth2\Storage\AccessTokenInterface;
20
use Doctrine\ORM\EntityManager;
21
22
class AccessToken extends BaseClass implements AccessTokenInterface
23
{
24
    /** @var EntityManager */
25
    private $em;
26
27
    /** @var ClientManager */
28
    private $clientManager;
29
30
    /** @var SubjectIdentifierService */
31
    private $subjectIdentifierService;
32
33 5
    public function __construct(EntityManager $EntityManager)
34
    {
35 5
        parent::__construct($EntityManager);
36 5
        $this->em = $EntityManager;
37 5
    }
38
39
    /**
40
     * Look up the supplied oauth_token from storage.
41
     *
42
     * We need to retrieve access token data as we create and verify tokens.
43
     *
44
     * @param $oauth_token
45
     * oauth_token to be check with.
46
     *
47
     * @return array|null
48
     * An associative array as below, and return NULL if the supplied oauth_token
49
     * is invalid:
50
     * - client_id: Stored client identifier.
51
     * - expires: Stored expiration in unix timestamp.
52
     * - scope: (optional) Stored scope values in space-separated string.
53
     *
54
     * @ingroup oauth2_section_7
55
     */
56 2
    public function getAccessToken($oauth_token)
57
    {
58 2
        $accessToken = $this->em->getRepository('LoginCidadaoOAuthBundle:AccessToken')
59 2
            ->findOneBy(['token' => $oauth_token]);
60
61 2
        if (!$accessToken instanceof \LoginCidadao\OAuthBundle\Entity\AccessToken) {
62 1
            return null;
63
        }
64
65
        /** @var Client $client */
66 1
        $client = $accessToken->getClient();
67
68
        /** @var PersonInterface $person */
69 1
        $person = $accessToken->getUser();
70
71
        return [
72 1
            'client_id' => $client->getClientId(),
73 1
            'user_id' => $this->subjectIdentifierService->getSubjectIdentifier($person, $client->getMetadata()),
74 1
            'expires' => $accessToken->getExpiresAt(),
75 1
            'scope' => $accessToken->getScope(),
76 1
            'id_token' => $accessToken->getIdToken(),
77
        ];
78
    }
79
80
    /**
81
     * Store the supplied access token values to storage.
82
     *
83
     * We need to store access token data as we create and verify tokens.
84
     *
85
     * @param string $oauth_token
86
     * oauth_token to be stored.
87
     * @param string $client_id
88
     * Client identifier to be stored.
89
     * @param string|null $user_id
90
     * User identifier to be stored.
91
     * @param int $expires Expiration to be stored as a Unix timestamp.
92
     * @param string $scope (optional) Scopes to be stored in space-separated string.
93
     * @param null|string $id_token
94
     * @return null|void
95
     * @ingroup oauth2_section_4
96
     * @throws \Doctrine\ORM\OptimisticLockException
97
     */
98 3
    public function setAccessToken($oauth_token, $client_id, $user_id = null, $expires, $scope = null, $id_token = null)
99
    {
100 3
        $user = null;
101 3
        if (!$client = $this->clientManager->getClientById($client_id)) {
102 1
            return null;
103 2
        } elseif ($user_id !== null) {
104 1
            $user = $this->getUser($client, $user_id);
105
        }
106
107
        // Create Access Token
108 2
        $accessToken = new \LoginCidadao\OAuthBundle\Entity\AccessToken();
109 2
        $accessToken->setToken($oauth_token);
110 2
        $accessToken->setClient($client);
111 2
        if ($user !== null) {
112 1
            $accessToken->setUser($user);
113
        }
114 2
        $accessToken->setExpiresAt($expires);
115 2
        $accessToken->setScope($scope);
116 2
        $accessToken->setIdToken($id_token);
117
118
        // Store Access Token and Authorization
119 2
        $this->em->persist($accessToken);
120 2
        $this->em->flush();
121 2
    }
122
123 2
    public function setSubjectIdentifierService(SubjectIdentifierService $subjectIdentifierService)
124
    {
125 2
        $this->subjectIdentifierService = $subjectIdentifierService;
126 2
    }
127
128 3
    public function setClientManager(ClientManager $clientManager)
129
    {
130 3
        $this->clientManager = $clientManager;
131 3
    }
132
133
    /**
134
     * @param ClientInterface $client
135
     * @param $user_id
136
     * @return PersonInterface|null|object
137
     */
138 1
    private function getUser(ClientInterface $client, $user_id)
139
    {
140 1
        $user = $this->subjectIdentifierService->getPerson($user_id, $client);
141 1
        if (!$user instanceof PersonInterface) {
142 1
            $user = $this->em->getRepository('LoginCidadaoCoreBundle:Person')->find($user_id);
143
        }
144
145 1
        return $user;
146
    }
147
}
148