Failed Conditions
Push — issue#702_rs ( ed72a1...cdafcf )
by Guilherme
07:33
created

RemoteClaimController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B validateRemoteClaimAction() 0 46 3
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\RestBundle\Controller\Annotations as REST;
14
use JMS\Serializer\SerializationContext;
15
use JMS\Serializer\SerializerInterface;
16
use LoginCidadao\APIBundle\Controller\BaseController;
17
use LoginCidadao\CoreBundle\Entity\Authorization;
18
use LoginCidadao\CoreBundle\Entity\AuthorizationRepository;
19
use LoginCidadao\OAuthBundle\Model\ClientInterface;
20
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface;
21
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface;
22
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface;
23
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface;
24
use Symfony\Component\HttpFoundation\Request;
25
26
class RemoteClaimController extends BaseController
27
{
28
    /**
29
     * @REST\Post("/api/v{version}/remote-claims/translate",
30
     *     name="remote_claims_validate",
31
     *     defaults={"_format"="json"},
32
     *     requirements={"version": "\d+(.\d+)*"})
33
     * @REST\View(templateVar="oidc_config")
34
     */
35
    public function validateRemoteClaimAction(Request $request)
36
    {
37
        $format = $request->get('_format');
38
        if ($format != 'json') {
39
            throw new \RuntimeException("Unsupported format '{$format}'");
40
        }
41
42
        /** @var ClaimProviderInterface|ClientInterface $provider */
43
        $provider = $this->getClient();
44
45
        $accessToken = $request->get('claim_access_token');
46
47
        /** @var RemoteClaimManagerInterface $manager */
48
        $manager = $this->get('lc.remote_claims.manager');
49
50
        $remoteClaimAuthorization = $manager->getRemoteClaimAuthorizationByAccessToken($provider, $accessToken);
51
        if (!$remoteClaimAuthorization instanceof RemoteClaimAuthorizationInterface) {
52
            throw $this->createNotFoundException("Authorization not found");
53
        }
54
        $person = $remoteClaimAuthorization->getPerson();
55
        $client = $remoteClaimAuthorization->getClient();
56
57
        /** @var AuthorizationRepository $authorizationRepo */
58
        $authorizationRepo = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:Authorization');
59
60
        /** @var Authorization $authorization */
61
        $authorization = $authorizationRepo->findOneBy([
62
            'client' => $provider,
63
            'person' => $person,
64
        ]);
65
66
        /** @var SerializerInterface $serializer */
67
        $serializer = $this->get('jms_serializer');
68
        $personSerializationContext = $this->getJMSSerializationContext($authorization->getScope());
69
        $serializedPerson = $serializer->serialize($person, $format, $personSerializationContext);
70
        $serializedClient = $serializer->serialize($client, $format, $this->getJMSSerializationContext(['remote_claim']));
71
72
        $response = [
73
            'claim_name' => (string)$remoteClaimAuthorization->getClaimName(),
74
            'userinfo' => json_decode($serializedPerson, true),
75
            'relying_party' => json_decode($serializedClient, true),
76
        ];
77
78
        $view = $this->view($response);
79
80
        return $this->handleView($view);
81
    }
82
}
83