Passed
Push — master ( 35c320...7deb12 )
by MusikAnimal
04:42
created

DefaultController::loginAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 16
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the DefaultController class.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use MediaWiki\OAuthClient\Client;
9
use MediaWiki\OAuthClient\ClientConfig;
10
use MediaWiki\OAuthClient\Consumer;
11
use MediaWiki\OAuthClient\Exception;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Session\Session;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Xtools\ProjectRepository;
19
20
/**
21
 * The DefaultController handles the homepage, about pages, and user authentication.
22
 */
23
class DefaultController extends XtoolsController
24
{
25
    /** @var Client The Oauth HTTP client. */
26
    protected $oauthClient;
27
28
    /**
29
     * Required to be defined by XtoolsController, though here it is unused.
30
     * @return string
31
     * @codeCoverageIgnore
32
     */
33
    public function getIndexRoute()
34
    {
35
        return 'homepage';
36
    }
37
38
    /**
39
     * Display the homepage.
40
     * @Route("/", name="homepage")
41
     * @Route("/index.php", name="homepageIndexPhp")
42
     */
43 1
    public function indexAction()
44
    {
45
        // replace this example code with whatever you need
46 1
        return $this->render('default/index.html.twig', [
47 1
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
48 1
            'xtPage' => 'home',
49
        ]);
50
    }
51
52
    /**
53
     * Diplay XTools' about page.
54
     * @Route("/about", name="aboutPage")
55
     * @Route("/info.php", name="info")
56
     */
57 1
    public function aboutAction()
58
    {
59 1
        return $this->render('default/about.html.twig', [
60 1
            'xtPage' => 'about',
61
        ]);
62
    }
63
64
    /**
65
     * Display some configuration details, when in development mode.
66
     * @Route("/config", name="configPage")
67
     * @codeCoverageIgnore
68
     */
69
    public function configAction()
70
    {
71
72
        if ($this->container->getParameter('kernel.environment') !== 'dev') {
73
            throw new NotFoundHttpException();
74
        }
75
76
        $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()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $params = $this->container->/** @scrutinizer ignore-call */ getParameterBag()->all();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
78
        foreach ($params as $key => $value) {
79
            if (strpos($key, 'password') !== false) {
80
                $params[$key] = '<REDACTED>';
81
            }
82
        }
83
84
        // replace this example code with whatever you need
85
        return $this->render('default/config.html.twig', [
86
            'xtTitle' => 'Config',
87
            'xtPageTitle' => 'Config',
88
            'xtPage' => 'index',
89
            'dump' => print_r($params, true),
90
        ]);
91
    }
92
93
    /**
94
     * Redirect to the default project (or Meta) for Oauth authentication.
95
     * @Route("/login", name="login")
96
     * @return RedirectResponse
97
     * @throws Exception If initialization fails.
98
     */
99
    public function loginAction()
100
    {
101
        try {
102
            list( $next, $token ) = $this->getOauthClient()->initiate();
103
        } catch (Exception $oauthException) {
104
            throw $oauthException;
105
            // @TODO Make this work.
106
            //$this->addFlash('error', $oauthException->getMessage());
107
            //return $this->redirectToRoute('homepage');
108
        }
109
110
        // Save the request token to the session.
111
        /** @var Session $session */
112
        $session = $this->get('session');
113
        $session->set('oauth_request_token', $token);
114
        return new RedirectResponse($next);
115
    }
116
117
    /**
118
     * Receive authentication credentials back from the Oauth wiki.
119
     * @Route("/oauth_callback", name="oauth_callback")
120
     * @Route("/oauthredirector.php", name="old_oauth_callback")
121
     * @param Request $request The HTTP request.
122
     * @return RedirectResponse
123
     */
124 1
    public function oauthCallbackAction(Request $request)
125
    {
126
        // Give up if the required GET params don't exist.
127 1
        if (!$request->get('oauth_verifier')) {
128 1
            throw $this->createNotFoundException('No OAuth verifier given.');
129
        }
130
131
        /** @var Session $session */
132
        $session = $this->get('session');
133
134
        // Complete authentication.
135
        $client = $this->getOauthClient();
136
        $token = $session->get('oauth_request_token');
137
        $verifier = $request->get('oauth_verifier');
138
        $accessToken = $client->complete($token, $verifier);
139
140
        // Store access token, and remove request token.
141
        $session->set('oauth_access_token', $accessToken);
142
        $session->remove('oauth_request_token');
143
144
        // Store user identity.
145
        $ident = $client->identify($accessToken);
146
        $session->set('logged_in_user', $ident);
147
148
        // Send back to homepage.
149
        return $this->redirectToRoute('homepage');
150
    }
151
152
    /**
153
     * Get an OAuth client, configured to the default project.
154
     * (This shouldn't really be in this class, but oh well.)
155
     * @return Client
156
     * @codeCoverageIgnore
157
     */
158
    protected function getOauthClient()
159
    {
160
        if ($this->oauthClient instanceof Client) {
0 ignored issues
show
introduced by
$this->oauthClient is always a sub-type of MediaWiki\OAuthClient\Client.
Loading history...
161
            return $this->oauthClient;
162
        }
163
        $defaultProject = ProjectRepository::getProject(
164
            $this->getParameter('oauth_project'),
165
            $this->container
166
        );
167
        $endpoint = $defaultProject->getUrl(false)
168
                    . $defaultProject->getScript()
169
                    . '?title=Special:OAuth';
170
        $conf = new ClientConfig($endpoint);
171
        $consumerKey = $this->getParameter('oauth_key');
172
        $consumerSecret =  $this->getParameter('oauth_secret');
173
        $conf->setConsumer(new Consumer($consumerKey, $consumerSecret));
174
        $this->oauthClient = new Client($conf);
175
        // Callback URL is hardcoded in the consumer registration.
176
        $this->oauthClient->setCallback('oob');
177
        return $this->oauthClient;
178
    }
179
180
    /**
181
     * Log out the user and return to the homepage.
182
     * @Route("/logout", name="logout")
183
     */
184 1
    public function logoutAction()
185
    {
186 1
        $this->get('session')->invalidate();
187 1
        return $this->redirectToRoute('homepage');
188
    }
189
}
190