SmsLoginProvider::connect()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.0787

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 25
ccs 8
cts 14
cp 0.5714
rs 8.8571
cc 1
eloc 14
nc 1
nop 1
crap 1.0787
1
<?php
2
/**
3
 * This file is part of the Apiary SMS Login Provider package
4
 *
5
 * (c) Darren Mothersele <[email protected]>
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 Apiary\SmsLoginProvider;
12
13
use Silex\Application;
14
use Silex\ControllerProviderInterface;
15
use Silex\ServiceProviderInterface;
16
use Symfony\Component\Security\Core\Security;
17
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
18
use Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener;
19
use Symfony\Component\Security\Http\EntryPoint\FormAuthenticationEntryPoint;
20
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
21
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
22
use Symfony\Component\Security\Core\Exception\AuthenticationException;
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be exactly one blank line after the last USE statement, 2 were found though.
Loading history...
23
24
25
class SmsLoginProvider implements ServiceProviderInterface, ControllerProviderInterface
26
{
27
28 9
    public function connect(Application $app)
29
    {
30
31 9
        $controllers = $app['controllers_factory'];
32
33 9
        $controllers->get('/login', 'login.controller:loginAction')
34 9
          ->bind('sms.login');
35 9
        $controllers->post('/login', 'login.controller:verifyAction');
36
37
        // Convert Twilio exception to Auth exception
38
        $app->error(function (\Services_Twilio_RestException $e) use ($app) {
39
            $app['monolog']->addDebug('Converting Twilio Exception: ' . $e->getMessage());
40
            $app['session']->set(Security::AUTHENTICATION_ERROR,
41
              new BadCredentialsException('Invalid phone number'));
42
            return $app->redirect($app['url_generator']->generate('sms.login'));
43 9
        });
44
        // Store Auth exception and go back to login form
45
        $app->error(function (AuthenticationException $e) use ($app) {
46
            $app['session']->set(Security::AUTHENTICATION_ERROR, $e);
47
            return $app->redirect($app['url_generator']->generate('sms.login'));
48 9
        });
49
50 9
        return $controllers;
51
52
    }
53
54 9
    public function register(Application $app)
55
    {
56
        $app['login.controller'] = $app->share(function () use ($app) {
57
            // TODO: Pass in arguments for view template and message template
58 6
            return new SmsLoginController($app['sms.handler'], null, null);
59 9
        });
60
61
        $app['security.authentication_listener.factory.sms'] = $app->protect(function (
62
          $name,
63
          $options
64
        ) use ($app) {
65
66
            $app['security.entry_point.' . $name . '.sms'] = $app->share(function (
67
            ) use ($app, $options) {
68 9
                $loginPath = $app['url_generator']->generate('sms.login');
69 9
                $useForward = isset($options['use_forward']) ? $options['use_forward'] : false;
70 9
                return new FormAuthenticationEntryPoint($app,
71 9
                  $app['security.http_utils'], $loginPath, $useForward);
72 9
            });
73
74
            $app['security.authentication_provider.' . $name . '.sms'] = $app->share(function (
75
            ) use ($app, $name) {
76 9
                return new SmsAuthenticator($name, $app['session']->get('code'),
77 9
                  $app['monolog']);
78 9
            });
79
80
            $app['security.authentication_listener.' . $name . '.sms'] = $app->share(function (
81
            ) use ($app, $name, $options) {
82
83
                // Create fake route for login check
84 9
                $loginCheckPath = $app['url_generator']->generate('sms.login') . '/check';
85 9
                $options['check_path'] = $loginCheckPath;
86 9
                $app->match($loginCheckPath)->run(null)->bind(str_replace('/',
87 9
                  '_', ltrim($loginCheckPath, '/')));
88
89
                // Set default form item names, if not provided
90 9
                $options['username_parameter'] = empty($options['username_parameter']) ? 'mobile' : $options['username_parameter'];
91 9
                $options['password_parameter'] = empty($options['password_parameter']) ? 'code' : $options['password_parameter'];
92
93 9 View Code Duplication
                if (!isset($app['security.authentication.success_handler.' . $name])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
                    $app['security.authentication.success_handler.' . $name] = $app->share(function (
95
                    ) use ($name, $options, $app) {
96 9
                        $handler = new DefaultAuthenticationSuccessHandler($app['security.http_utils'],
97 9
                          $options);
98 9
                        $handler->setProviderKey($name);
99 9
                        return $handler;
100 9
                    });
101 9
                }
102
103 9 View Code Duplication
                if (!isset($app['security.authentication.failure_handler.' . $name])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104 9
                    $app['security.authentication.failure_handler.' . $name] = $app->share(function (
105
                    ) use ($name, $options, $app) {
106 9
                        return new DefaultAuthenticationFailureHandler($app,
107 9
                          $app['security.http_utils'], $options,
108 9
                          $app['logger']);
109 9
                    });
110 9
                }
111
112 9
                return new UsernamePasswordFormAuthenticationListener(
113 9
                  $app['security.token_storage'],
114 9
                  $app['security.authentication_manager'],
115 9
                  isset($app['security.session_strategy.' . $name]) ? $app['security.session_strategy.' . $name] : $app['security.session_strategy'],
116 9
                  $app['security.http_utils'],
117 9
                  $name,
118 9
                  $app['security.authentication.success_handler.' . $name],
119 9
                  $app['security.authentication.failure_handler.' . $name],
120 9
                  $options,
121 9
                  $app['logger'],
122 9
                  $app['dispatcher'],
123 9
                  isset($options['with_csrf']) && $options['with_csrf'] && isset($app['form.csrf_provider']) ? $app['form.csrf_provider'] : null
124 9
                );
125
126 9
            });
127
128
            return [
129
                // the authentication provider id
130 9
              'security.authentication_provider.' . $name . '.sms',
131
                // the authentication listener id
132 9
              'security.authentication_listener.' . $name . '.sms',
133
                // the entry point id
134 9
              'security.entry_point.' . $name . '.sms',
135
                // the position of the listener in the stack
136
              'form'
137 9
            ];
138 9
        });
139 9
    }
140
141 9
    public function boot(Application $app)
142
    {
143
144 9
    }
145
146
}
0 ignored issues
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...