GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0b21d1...86b995 )
by Christian
05:59
created

AuthController::storeCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
3
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\FacebookBundle\Controller;
11
12
use Core23\FacebookBundle\Connection\FacebookConnection;
13
use Facebook\Authentication\AccessToken;
14
use Facebook\Exceptions\FacebookSDKException;
15
use Facebook\GraphNodes\GraphUser;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpFoundation\Session\Session;
19
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20
21
final class AuthController extends Controller
22
{
23
    const SESSION_FB_ID      = '_CORE23_FACEBOOK_ID';
24
    const SESSION_FB_NAME    = '_CORE23_FACEBOOK_NAME';
25
    const SESSION_FB_TOKEN   = '_CORE23_FACEBOOK_TOKEN';
26
    const SESSION_FB_EXPIRES = '_CORE23_FACEBOOK_EXPIRES';
27
28
    /**
29
     * @return Response
30
     */
31
    public function authAction(): Response
32
    {
33
        $fb     = $this->getFacebookConnection();
34
        $helper = $fb->getRedirectLoginHelper();
35
36
        return $this->redirect($helper->getLoginUrl(
37
            $this->generateUrl('core23_facebook_check', array(), UrlGeneratorInterface::ABSOLUTE_URL),
38
            $this->getParameter('core23.facebook.api.permissions')
39
        ));
40
    }
41
42
    /**
43
     * @return Response
44
     */
45
    public function checkAction(): Response
46
    {
47
        $fb     = $this->getFacebookConnection();
48
        $helper = $fb->getRedirectLoginHelper();
49
50
        try {
51
            $token    = $helper->getAccessToken();
52
            $response = $fb->get('/me?fields=id,name', $token);
53
54
            $this->storeCredentials($token, $response->getGraphUser());
0 ignored issues
show
Bug introduced by
It seems like $token defined by $helper->getAccessToken() on line 51 can be null; however, Core23\FacebookBundle\Co...ler::storeCredentials() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
55
56
            return $this->redirectToRoute('core23_facebook_success');
57
        } catch (FacebookSDKException $exception) {
58
            $this->get('logger')->warning(sprintf('Facebook SDK Exception: %s', $exception->getMessage()));
59
        }
60
61
        return $this->redirectToRoute('core23_facebook_error');
62
    }
63
64
    /**
65
     * @return Response
66
     */
67
    public function errorAction(): Response
68
    {
69
        if ($this->isAuthenticated()) {
70
            return $this->redirectToRoute('core23_facebook_success');
71
        }
72
73
        if (null !== $this->getParameter('core23.facebook.auth_error.redirect_route')) {
74
            return $this->redirectToRoute($this->getParameter('core23.facebook.auth_error.redirect_route'), $this->getParameter('core23.facebook.auth_error.redirect_route_params'));
75
        }
76
77
        return $this->render('Core23FacebookBundle:Auth:error.html.twig');
78
    }
79
80
    /**
81
     * @return Response
82
     */
83
    public function successAction(): Response
84
    {
85
        if (!$this->isAuthenticated()) {
86
            return $this->redirectToRoute('core23_facebook_error');
87
        }
88
89
        if (null !== $this->getParameter('core23.facebook.auth_success.redirect_route')) {
90
            return $this->redirectToRoute($this->getParameter('core23.facebook.auth_success.redirect_route'), $this->getParameter('core23.facebook.auth_success.redirect_route_params'));
91
        }
92
93
        $session = $this->get('session');
94
95
        return $this->render('Core23FacebookBundle:Auth:success.html.twig', array(
96
            'name' => $session->get(static::SESSION_FB_NAME),
97
        ));
98
    }
99
100
    /**
101
     * @param AccessToken $token
102
     * @param GraphUser   $user
103
     */
104
    private function storeCredentials(AccessToken $token, GraphUser $user): void
105
    {
106
        $fbid = $user->getId();
107
        $name = $user->getName();
108
109
        /** @var Session $session */
110
        $session = $this->get('session');
111
        $session->set(static::SESSION_FB_ID, $fbid);
112
        $session->set(static::SESSION_FB_NAME, $name);
113
        $session->set(static::SESSION_FB_TOKEN, $token);
114
        $session->set(static::SESSION_FB_EXPIRES, $token->getExpiresAt());
115
    }
116
117
    /**
118
     * Returns the auth status.
119
     *
120
     * @return bool
121
     */
122
    private function isAuthenticated(): bool
123
    {
124
        return (bool) $this->get('session')->get(static::SESSION_FB_TOKEN);
125
    }
126
127
    /**
128
     * @return FacebookConnection
129
     */
130
    private function getFacebookConnection(): FacebookConnection
131
    {
132
        return $this->get('core23.facebook.connection');
133
    }
134
}
135