Passed
Push — master ( 879ca4...d0a2dc )
by Robbie
03:13
created

SAMLAuthenticator::authenticate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace SilverStripe\SAML\Authenticators;
4
5
use SilverStripe\Control\Controller;
6
use Silverstripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\Session;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\ORM\ValidationResult;
13
use SilverStripe\SAML\Helpers\SAMLHelper;
14
use SilverStripe\Security\Authenticator;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
17
18
/**
19
 * Class SAMLAuthenticator
20
 *
21
 * Authenticates the user against a SAML IdP via a single sign-on process.
22
 * It will create a {@link Member} stub record with rudimentary fields (see {@link SAMLController::acs()})
23
 * if the Member record was not found.
24
 *
25
 * You can either use:
26
 * - just SAMLAuthenticator (which will trigger LDAP sync anyway, via LDAPMemberExtension::memberLoggedIn)
27
 * - just LDAPAuthenticator (syncs explicitly, but no single sign-on via IdP done)
28
 * - both, so people have multiple tabbed options in the login form.
29
 *
30
 * Both authenticators understand and collaborate through the GUID field on the Member.
31
 */
32
class SAMLAuthenticator extends MemberAuthenticator
33
{
34
    /**
35
     * @var string
36
     */
37
    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...
38
39
    /**
40
     * @return string
41
     */
42
    public static function get_name()
43
    {
44
        return Config::inst()->get(self::class, 'name');
45
    }
46
47
    /**
48
     * @param Controller $controller
49
     * @return SAMLLoginForm
50
     */
51
    public static function get_login_form(Controller $controller)
52
    {
53
        return new SAMLLoginForm($controller, 'LoginForm');
0 ignored issues
show
Bug introduced by
The call to SilverStripe\SAML\Authen...oginForm::__construct() has too few arguments starting with name. ( Ignorable by Annotation )

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

53
        return /** @scrutinizer ignore-call */ new SAMLLoginForm($controller, 'LoginForm');

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
54
    }
55
56
    /**
57
     * Sends the authentication process down the SAML rabbit hole. It will trigger
58
     * the IdP redirection via the 3rd party implementation, and if successful, the user
59
     * will be delivered to the SAMLController::acs.
60
     *
61
     * @param array $data
62
     * @param HTTPRequest $request
63
     * @param ValidationResult|null $result
64
     * @return bool|Member|void
65
     */
66
    public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null)
67
    {
68
        // $data is not used - the form is just one button, with no fields.
69
        $auth = Injector::inst()->get(SAMLHelper::class)->getSAMLAuth();
70
        $request->getSession()->set('BackURL', isset($data['BackURL']) ? $data['BackURL'] : null);
71
        $request->getSession()->save($request);
72
        $auth->login(Director::absoluteBaseURL().'saml/');
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function getLoginHandler($link)
79
    {
80
        return SAMLLoginHandler::create($link, $this);
0 ignored issues
show
Bug introduced by
$this of type SilverStripe\SAML\Authenticators\SAMLAuthenticator is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

80
        return SAMLLoginHandler::create($link, /** @scrutinizer ignore-type */ $this);
Loading history...
Bug Best Practice introduced by
The expression return SilverStripe\SAML...r::create($link, $this) also could return the type object which includes types incompatible with the return type mandated by SilverStripe\Security\Au...ator::getLoginHandler() of SilverStripe\Security\Me...henticator\LoginHandler. Consider adding a type-check to rule them out.
Loading history...
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function supportedServices()
87
    {
88
        return Authenticator::LOGIN | Authenticator::LOGOUT;
89
    }
90
}
91