Failed Conditions
Push — issue#702 ( 91bd46...0b5bf0 )
by Guilherme
19:37 queued 12:15
created

RemoteClaimController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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