Completed
Push — issue#666 ( 6be2d0...f5ce0d )
by Guilherme
03:34
created

RemoteClaimController::validateRemoteClaimAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 26
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 47
rs 9.0303
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/remote-claims/translate", name="remote_claims_validate", defaults={"_format"="json"})
42
     * @REST\View(templateVar="oidc_config")
43
     */
44
    public function validateRemoteClaimAction(Request $request)
45
    {
46
        $format = $request->get('_format');
47
        if ($format != 'json') {
48
            throw new \RuntimeException("Unsupported format '{$format}'");
49
        }
50
51
        /** @var ClaimProviderInterface|ClientInterface $provider */
52
        $provider = $this->getClient();
53
54
        $accessToken = $request->get('claim_access_token');
55
56
        /** @var RemoteClaimManagerInterface $manager */
57
        $manager = $this->get('lc.remote_claims.manager');
58
59
        $remoteClaimAuthorization = $manager->getRemoteClaimAuthorizationByAccessToken($provider, $accessToken);
0 ignored issues
show
Bug introduced by
It seems like $provider defined by $this->getClient() on line 52 can also be of type object<LoginCidadao\OAut...\Model\ClientInterface>; however, LoginCidadao\RemoteClaim...rizationByAccessToken() does only seem to accept object<LoginCidadao\Remo...ClaimProviderInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
        if (!$remoteClaimAuthorization instanceof RemoteClaimAuthorizationInterface) {
61
            throw $this->createNotFoundException("Authorization not found");
62
        }
63
        $person = $remoteClaimAuthorization->getPerson();
64
        $client = $remoteClaimAuthorization->getClient();
65
66
        /** @var AuthorizationRepository $authorizationRepo */
67
        $authorizationRepo = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:Authorization');
68
69
        /** @var Authorization $authorization */
70
        $authorization = $authorizationRepo->findOneBy([
71
            'client' => $provider,
72
            'person' => $person,
73
        ]);
74
75
        /** @var SerializerInterface $serializer */
76
        $serializer = $this->get('jms_serializer');
77
        $personSerializationContext = $this->getSerializationContext($authorization->getScope());
78
        $serializedPerson = $serializer->serialize($person, $format, $personSerializationContext);
79
        $serializedClient = $serializer->serialize($client, $format, $this->getSerializationContext(['remote_claim']));
80
81
        $response = [
82
            'claim_name' => (string)$remoteClaimAuthorization->getClaimName(),
83
            'userinfo' => json_decode($serializedPerson, true),
84
            'relying_party' => json_decode($serializedClient, true),
85
        ];
86
87
        $view = $this->view($response);
88
89
        return $this->handleView($view);
90
    }
91
}
92