Completed
Pull Request — master (#84)
by Robbie
08:08
created

SAMLAuthenticator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 6
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_name() 0 4 1
A get_login_form() 0 4 1
A authenticate() 0 8 2
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Authenticators;
4
5
use SilverStripe\Control\Controller;
6
use Silverstripe\Control\Director;
7
use SilverStripe\Control\Session;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Security\Authenticator;
12
13
/**
14
 * Class SAMLAuthenticator
15
 *
16
 * Authenticates the user against a SAML IdP via a single sign-on process.
17
 * It will create a {@link Member} stub record with rudimentary fields (see {@link SAMLController::acs()})
18
 * if the Member record was not found.
19
 *
20
 * You can either use:
21
 * - just SAMLAuthenticator (which will trigger LDAP sync anyway, via LDAPMemberExtension::memberLoggedIn)
22
 * - just LDAPAuthenticator (syncs explicitly, but no single sign-on via IdP done)
23
 * - both, so people have multiple tabbed options in the login form.
24
 *
25
 * Both authenticators understand and collaborate through the GUID field on the Member.
26
 *
27
 * @package activedirectory
28
 */
29
class SAMLAuthenticator extends Authenticator
30
{
31
    /**
32
     * @var string
33
     */
34
    private $name = 'SAML';
0 ignored issues
show
Unused Code introduced by
The property $name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * @return string
38
     */
39
    public static function get_name()
40
    {
41
        return Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\SAMLAuthenticator', 'name');
42
    }
43
44
    /**
45
     * @param Controller $controller
46
     * @return SAMLLoginForm
47
     */
48
    public static function get_login_form(Controller $controller)
49
    {
50
        return new SAMLLoginForm($controller, 'LoginForm');
0 ignored issues
show
Documentation introduced by
$controller is of type object<SilverStripe\Control\Controller>, but the function expects a object<SilverStripe\Acti...henticators\Controller>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
    }
52
53
    /**
54
     * Sends the authentication process down the SAML rabbit hole. It will trigger
55
     * the IdP redirection via the 3rd party implementation, and if successful, the user
56
     * will be delivered to the SAMLController::acs.
57
     *
58
     * @param array $data
59
     * @param Form $form
60
     * @return bool|Member|void
61
     * @throws SS_HTTPResponse_Exception
62
     */
63
    public static function authenticate($data, Form $form = null)
64
    {
65
        // $data is not used - the form is just one button, with no fields.
66
        $auth = Injector::inst()->get('SilverStripe\\ActiveDirectory\\Helpers\\SAMLHelper')->getSAMLAuth();
67
        Session::set('BackURL', isset($data['BackURL']) ? $data['BackURL'] : null);
68
        Session::save();
69
        $auth->login(Director::absoluteBaseURL().'saml/');
70
    }
71
}
72