|
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\OAuthBundle\Model; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManager; |
|
14
|
|
|
use LoginCidadao\OAuthBundle\Entity\AccessToken; |
|
15
|
|
|
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken; |
|
16
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
18
|
|
|
|
|
19
|
|
|
class AccessTokenManager |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var EntityManager */ |
|
22
|
|
|
private $em; |
|
23
|
|
|
|
|
24
|
|
|
/** @var TokenStorageInterface */ |
|
25
|
|
|
private $tokenStorage; |
|
26
|
|
|
|
|
27
|
1 |
|
public function __construct(EntityManager $em, |
|
|
|
|
|
|
28
|
|
|
TokenStorageInterface $tokenStorage) |
|
|
|
|
|
|
29
|
|
|
{ |
|
|
|
|
|
|
30
|
1 |
|
$this->em = $em; |
|
31
|
1 |
|
$this->tokenStorage = $tokenStorage; |
|
32
|
1 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return boolean |
|
36
|
|
|
*/ |
|
37
|
|
|
public function hasToken() |
|
38
|
|
|
{ |
|
39
|
|
|
$securityToken = $this->tokenStorage->getToken(); |
|
40
|
|
|
return ($securityToken instanceof OAuthToken); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $token |
|
45
|
|
|
* @throws AccessDeniedException when no token is found in the current session |
|
46
|
|
|
* @return AccessToken |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getToken($token = null) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($token === null) { |
|
51
|
|
|
$securityToken = $this->tokenStorage->getToken(); |
|
52
|
|
|
if (!($securityToken instanceof OAuthToken)) { |
|
53
|
|
|
throw new AccessDeniedException("Couldn't find an AccessToken in the current session."); |
|
54
|
|
|
} |
|
55
|
|
|
$token = $securityToken->getToken(); |
|
56
|
|
|
} |
|
57
|
|
|
$repo = $this->em->getRepository('LoginCidadaoOAuthBundle:AccessToken'); |
|
58
|
|
|
return $repo->findOneByToken($token); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $token defaults to the current AccessToken |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getTokenScope($token = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$accessToken = $this->getToken($token); |
|
68
|
|
|
|
|
69
|
|
|
$scope = $accessToken->getScope(); |
|
70
|
|
|
|
|
71
|
|
|
if (is_string($scope)) { |
|
|
|
|
|
|
72
|
|
|
return explode(' ', $scope); |
|
73
|
|
|
} elseif (is_array($scope)) { |
|
74
|
|
|
return $scope; |
|
75
|
|
|
} else { |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $token |
|
82
|
|
|
* @return ClientInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getTokenClient($token = null) |
|
85
|
|
|
{ |
|
86
|
|
|
$accessToken = $this->getToken($token); |
|
87
|
|
|
|
|
88
|
|
|
return $accessToken->getClient(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|