Completed
Push — master ( b7c59e...e15cd8 )
by Sam
04:11
created

DefaultController::oauthCallbackAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Exception;
6
use MediaWiki\OAuthClient\Client;
7
use MediaWiki\OAuthClient\ClientConfig;
8
use MediaWiki\OAuthClient\Consumer;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Session\Session;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
use Xtools\ProjectRepository;
16
17
class DefaultController extends Controller
18
{
19
20
    /** @var Client */
21
    protected $oauthClient;
22
23
    /**
24
     * @Route("/", name="homepage")
25
     * @Route("/index.php", name="homepageIndexPhp")
26
     */
27
    public function indexAction()
28
    {
29
        // replace this example code with whatever you need
30
        return $this->render('default/index.html.twig', [
31
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
32
            'xtPageTitle' => 'welcome',
33
            'xtPage' => "index",
34
        ]);
35
    }
36
37
    /**
38
     * @Route("/about", name="aboutPage")
39
     */
40
    public function aboutAction()
41
    {
42
43
        // replace this example code with whatever you need
44
        return $this->render('default/about.html.twig', array(
45
            'xtPage' => 'index',
46
        ));
47
    }
48
49
    /**
50
     * @Route("/config", name="configPage")
51
     */
52
    public function configAction()
53
    {
54
55
        if ($this->container->getParameter('kernel.environment') != "dev") {
56
            throw new NotFoundHttpException();
57
        }
58
59
        $params = $this->container->getParameterBag()->all();
0 ignored issues
show
Bug introduced by
The method getParameterBag() does not exist on Symfony\Component\Depend...tion\ContainerInterface. Did you maybe mean getParameter()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
61
        foreach ($params as $key => $value) {
62
            if (strpos($key, "password") !== false) {
63
                $params[$key] = "<REDACTED>";
64
            }
65
        }
66
67
        // replace this example code with whatever you need
68
        return $this->render('default/config.html.twig', [
69
            "xtTitle" => "Config",
70
            "xtPageTitle" => "Config",
71
            'xtPage' => "index",
72
            'dump' => print_r($params, true),
73
        ]);
74
    }
75
76
    /**
77
     * @Route("/login", name="login")
78
     */
79
    public function loginAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        try {
82
            list( $next, $token ) = $this->getOauthClient()->initiate();
83
        } catch (Exception $oauthException) {
84
            throw $oauthException;
85
            // @TODO Make this work.
86
            //$this->addFlash('error', $oauthException->getMessage());
1 ignored issue
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
87
            //return $this->redirectToRoute('homepage');
1 ignored issue
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
        }
89
90
        // Save the request token to the session.
91
        /** @var Session $session */
92
        $session = $this->get('session');
93
        $session->set('oauth_request_token', $token);
94
        return new RedirectResponse($next);
95
    }
96
97
    /**
98
     * @Route("/oauth_callback", name="oauth_callback")
99
     */
100
    public function oauthCallbackAction(Request $request)
101
    {
102
        // Give up if the required GET params don't exist.
103
        if (!$request->get('oauth_verifier')) {
104
            return $this->createNotFoundException();
105
        }
106
107
        /** @var Session $session */
108
        $session = $this->get('session');
109
110
        // Complete authentication.
111
        $client = $this->getOauthClient();
112
        $token = $session->get('oauth_request_token');
113
        $verifier = $request->get('oauth_verifier');
114
        $accessToken = $client->complete($token, $verifier);
115
116
        // Store access token, and remove request token.
117
        $session->set('oauth_access_token', $accessToken);
118
        $session->remove('oauth_request_token');
119
120
        // Store user identity.
121
        $ident = $client->identify($accessToken);
122
        $session->set('logged_in_user', $ident);
123
124
        // Send back to homepage.
125
        return $this->redirectToRoute('homepage');
126
    }
127
128
    /**
129
     * Get an OAuth client, configured to the default project.
130
     * (This shouldn't really be in this class, but oh well.)
131
     * @return Client
132
     */
133
    protected function getOauthClient()
134
    {
135
        if ($this->oauthClient instanceof Client) {
136
            return $this->oauthClient;
137
        }
138
        $defaultProject = ProjectRepository::getDefaultProject($this->container);
1 ignored issue
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
139
        $endpoint = $defaultProject->getUrl(false)
140
                    . $defaultProject->getScriptPath()
141
                    . '/index.php?title=Special:OAuth';
142
        $conf = new ClientConfig($endpoint);
143
        $consumerKey = $this->getParameter('oauth_key');
144
        $consumerSecret =  $this->getParameter('oauth_secret');
145
        $conf->setConsumer(new Consumer($consumerKey, $consumerSecret));
146
        $this->oauthClient = new Client($conf);
147
        // Callback URL is hardcoded in the consumer registration.
148
        $this->oauthClient->setCallback('oob');
149
        return $this->oauthClient;
150
    }
151
152
    /**
153
     * @Route("/logout", name="logout")
154
     */
155
    public function logoutAction()
156
    {
157
        $this->get('session')->invalidate();
158
        return $this->redirectToRoute('homepage');
159
    }
160
}
161