|
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\RemoteClaimsBundle\Controller; |
|
12
|
|
|
|
|
13
|
|
|
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken; |
|
14
|
|
|
use FOS\RestBundle\Controller\Annotations as REST; |
|
15
|
|
|
use JMS\Serializer\SerializationContext; |
|
16
|
|
|
use JMS\Serializer\SerializerInterface; |
|
17
|
|
|
use LoginCidadao\APIBundle\Controller\BaseController; |
|
18
|
|
|
use LoginCidadao\CoreBundle\Entity\Authorization; |
|
19
|
|
|
use LoginCidadao\CoreBundle\Entity\AuthorizationRepository; |
|
20
|
|
|
use LoginCidadao\CoreBundle\LongPolling\LongPollingUtils; |
|
21
|
|
|
use LoginCidadao\OAuthBundle\Model\AccessTokenManager; |
|
22
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
23
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface; |
|
24
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface; |
|
25
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface; |
|
26
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
28
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
29
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
30
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
31
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientUser; |
|
32
|
|
|
use LoginCidadao\APIBundle\Security\Audit\Annotation as Audit; |
|
33
|
|
|
use LoginCidadao\APIBundle\Entity\LogoutKey; |
|
34
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
35
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
36
|
|
|
|
|
37
|
|
|
class RemoteClaimController extends BaseController |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @REST\Get("/api/v1/validate-claim", name="remote_claims_validate", defaults={"_format"="json"}) |
|
42
|
|
|
* @REST\View(templateVar="oidc_config") |
|
43
|
|
|
*/ |
|
44
|
|
|
public function validateRemoteClaimAction(Request $request) |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var ClaimProviderInterface|ClientInterface $provider */ |
|
47
|
|
|
$provider = $this->getClient(); |
|
48
|
|
|
|
|
49
|
|
|
$accessToken = $request->get('claim_access_token'); |
|
50
|
|
|
|
|
51
|
|
|
/** @var RemoteClaimManagerInterface $manager */ |
|
52
|
|
|
$manager = $this->get('lc.remote_claims.manager'); |
|
53
|
|
|
|
|
54
|
|
|
$remoteClaimAuthorization = $manager->getRemoteClaimAuthorizationByAccessToken($provider, $accessToken); |
|
|
|
|
|
|
55
|
|
|
if (!$remoteClaimAuthorization instanceof RemoteClaimAuthorizationInterface) { |
|
56
|
|
|
throw $this->createNotFoundException("Authorization not found"); |
|
57
|
|
|
} |
|
58
|
|
|
$person = $remoteClaimAuthorization->getPerson(); |
|
59
|
|
|
$client = $remoteClaimAuthorization->getClient(); |
|
60
|
|
|
|
|
61
|
|
|
/** @var AuthorizationRepository $authorizationRepo */ |
|
62
|
|
|
$authorizationRepo = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:Authorization'); |
|
63
|
|
|
|
|
64
|
|
|
/** @var Authorization $authorization */ |
|
65
|
|
|
$authorization = $authorizationRepo->findOneBy([ |
|
66
|
|
|
'client' => $provider, |
|
67
|
|
|
'person' => $person, |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
/** @var SerializerInterface $serializer */ |
|
71
|
|
|
$serializer = $this->get('jms_serializer'); |
|
72
|
|
|
$serializedPerson = $serializer->serialize($person, $this->getSerializationContext($authorization->getScope())); |
|
|
|
|
|
|
73
|
|
|
$serializedClient = $serializer->serialize($client, $this->getSerializationContext(['remote_claim'])); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$response = [ |
|
76
|
|
|
'claim_name' => (string)$remoteClaimAuthorization->getClaimName(), |
|
77
|
|
|
'userinfo' => $serializedPerson, |
|
78
|
|
|
'relying_party' => $serializedClient, |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$view = $this->view($response); |
|
82
|
|
|
|
|
83
|
|
|
return $this->handleView($view); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.