SAMLAuthenticator   A
last analyzed

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
 * Class SAMLAuthenticator
4
 *
5
 * Authenticates the user against a SAML IdP via a single sign-on process.
6
 * It will create a {@link Member} stub record with rudimentary fields (see {@link SAMLController::acs()})
7
 * if the Member record was not found.
8
 *
9
 * You can either use:
10
 * - just SAMLAuthenticator (which will trigger LDAP sync anyway, via LDAPMemberExtension::memberLoggedIn)
11
 * - just LDAPAuthenticator (syncs explicitly, but no single sign-on via IdP done)
12
 * - both, so people have multiple tabbed options in the login form.
13
 *
14
 * Both authenticators understand and collaborate through the GUID field on the Member.
15
 */
16
class SAMLAuthenticator extends Authenticator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * @var string
20
     */
21
    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...
22
23
    /**
24
     * @return string
25
     */
26
    public static function get_name()
27
    {
28
        return Config::inst()->get('SAMLAuthenticator', 'name');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Config::inst()->...uthenticator', 'name'); (array|integer|double|string|boolean) is incompatible with the return type of the parent method Authenticator::get_name of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
29
    }
30
31
    /**
32
     * @param Controller $controller
33
     * @return SAMLLoginForm
34
     */
35
    public static function get_login_form(Controller $controller)
36
    {
37
        return new SAMLLoginForm($controller, 'LoginForm');
38
    }
39
40
    /**
41
     * Sends the authentication process down the SAML rabbit hole. It will trigger
42
     * the IdP redirection via the 3rd party implementation, and if successful, the user
43
     * will be delivered to the SAMLController::acs.
44
     *
45
     * @param array $data
46
     * @param Form $form
47
     * @return bool|Member|void
48
     * @throws SS_HTTPResponse_Exception
49
     */
50
    public static function authenticate($data, Form $form = null)
51
    {
52
        // $data is not used - the form is just one button, with no fields.
53
        $auth = Injector::inst()->get('SAMLHelper')->getSAMLAuth();
54
        Session::set('BackURL', isset($data['BackURL']) ? $data['BackURL'] : null);
55
        Session::save();
56
        $auth->login(Director::absoluteBaseURL().'saml/');
57
    }
58
}
59