Failed Conditions
Push — issue#666 ( f415d0...521a08 )
by Guilherme
12:02
created

AccessToken   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 123
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAccessToken() 0 21 2
B setAccessToken() 0 22 4
A setClientManager() 0 3 1
A setSubjectIdentifierService() 0 3 1
A getUser() 0 8 2
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 4
    public function __construct(EntityManager $EntityManager)
34
    {
35 4
        parent::__construct($EntityManager);
36 4
        $this->em = $EntityManager;
37 4
    }
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 $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 2
    public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null, $id_token = null)
99
    {
100 2
        if ($user_id === null || !$client = $this->clientManager->getClientById($client_id)) {
101 1
            return null;
102
        } else {
103 1
            $user = $this->getUser($client, $user_id);
104
        }
105
106
        // Create Access Token
107 1
        $accessToken = new \LoginCidadao\OAuthBundle\Entity\AccessToken();
108 1
        $accessToken->setToken($oauth_token);
109 1
        $accessToken->setClient($client);
110 1
        if ($user !== null) {
111 1
            $accessToken->setUser($user);
112
        }
113 1
        $accessToken->setExpiresAt($expires);
114 1
        $accessToken->setScope($scope);
115 1
        $accessToken->setIdToken($id_token);
116
117
        // Store Access Token and Authorization
118 1
        $this->em->persist($accessToken);
119 1
        $this->em->flush();
120 1
    }
121
122 2
    public function setSubjectIdentifierService(SubjectIdentifierService $subjectIdentifierService)
123
    {
124 2
        $this->subjectIdentifierService = $subjectIdentifierService;
125 2
    }
126
127 1
    public function setClientManager(ClientManager $clientManager)
128
    {
129 1
        $this->clientManager = $clientManager;
130 1
    }
131
132
    /**
133
     * @param ClientInterface $client
134
     * @param $user_id
135
     * @return PersonInterface|null|object
136
     */
137 1
    private function getUser(ClientInterface $client, $user_id)
138
    {
139 1
        $user = $this->subjectIdentifierService->getPerson($user_id, $client);
140 1
        if (!$user instanceof PersonInterface) {
141 1
            $user = $this->em->getRepository('LoginCidadaoCoreBundle:Person')->find($user_id);
142
        }
143
144 1
        return $user;
145
    }
146
}
147