Completed
Pull Request — master (#116)
by Robbie
06:55
created

SAMLAuthenticator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 7
dl 0
loc 129
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get_name() 0 4 1
A get_login_form() 0 4 1
A authenticate() 0 8 2
A supportedServices() 0 4 1
A getLoginHandler() 0 4 1
A getLogOutHandler() 0 4 1
A getChangePasswordHandler() 0 4 1
A getLostPasswordHandler() 0 4 1
A checkPassword() 0 4 1
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Authenticators;
4
5
use SilverStripe\ActiveDirectory\Helpers\SAMLHelper;
6
use SilverStripe\Control\Controller;
7
use Silverstripe\Control\Director;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Control\Session;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Forms\Form;
13
use SilverStripe\ORM\ValidationResult;
14
use SilverStripe\Security\Authenticator;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Security\MemberAuthenticator\LoginHandler;
17
use SilverStripe\Security\MemberAuthenticator\LogoutHandler;
18
19
/**
20
 * Class SAMLAuthenticator
21
 *
22
 * Authenticates the user against a SAML IdP via a single sign-on process.
23
 * It will create a {@link Member} stub record with rudimentary fields (see {@link SAMLController::acs()})
24
 * if the Member record was not found.
25
 *
26
 * You can either use:
27
 * - just SAMLAuthenticator (which will trigger LDAP sync anyway, via LDAPMemberExtension::memberLoggedIn)
28
 * - just LDAPAuthenticator (syncs explicitly, but no single sign-on via IdP done)
29
 * - both, so people have multiple tabbed options in the login form.
30
 *
31
 * Both authenticators understand and collaborate through the GUID field on the Member.
32
 *
33
 * @package activedirectory
34
 */
35
class SAMLAuthenticator implements Authenticator
36
{
37
    /**
38
     * @var string
39
     */
40
    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...
41
42
    /**
43
     * @return string
44
     */
45
    public static function get_name()
46
    {
47
        return Config::inst()->get(self::class, 'name');
48
    }
49
50
    /**
51
     * @param Controller $controller
52
     * @return SAMLLoginForm
53
     */
54
    public static function get_login_form(Controller $controller)
55
    {
56
        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...
57
    }
58
59
    /**
60
     * Sends the authentication process down the SAML rabbit hole. It will trigger
61
     * the IdP redirection via the 3rd party implementation, and if successful, the user
62
     * will be delivered to the SAMLController::acs.
63
     *
64
     * @param array $data
65
     * @param HTTPRequest $request
66
     * @param ValidationResult|null $result
67
     * @return bool|Member|void
68
     * @internal param Form $form
69
     */
70
    public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null)
71
    {
72
        // $data is not used - the form is just one button, with no fields.
73
        $auth = Injector::inst()->get(SAMLHelper::class)->getSAMLAuth();
74
        $request->getSession()->set('BackURL', isset($data['BackURL']) ? $data['BackURL'] : null);
75
        $request->getSession()->save($request);
76
        $auth->login(Director::absoluteBaseURL().'saml/');
77
    }
78
79
    /**
80
     * Returns the services supported by this authenticator
81
     *
82
     * The number should be a bitwise-OR of 1 or more of the following constants:
83
     * Authenticator::LOGIN, Authenticator::LOGOUT, Authenticator::CHANGE_PASSWORD,
84
     * Authenticator::RESET_PASSWORD, or Authenticator::CMS_LOGIN
85
     *
86
     * @return int
87
     */
88
    public function supportedServices()
89
    {
90
        // TODO: Implement supportedServices() method.
91
    }
92
93
    /**
94
     * Return RequestHandler to manage the log-in process.
95
     *
96
     * The default URL of the RequestHandler should return the initial log-in form, any other
97
     * URL may be added for other steps & processing.
98
     *
99
     * URL-handling methods may return an array [ "Form" => (form-object) ] which can then
100
     * be merged into a default controller.
101
     *
102
     * @param string $link The base link to use for this RequestHandler
103
     * @return LoginHandler
104
     */
105
    public function getLoginHandler($link)
106
    {
107
        // TODO: Implement getLoginHandler() method.
108
    }
109
110
    /**
111
     * Return the RequestHandler to manage the log-out process.
112
     *
113
     * The default URL of the RequestHandler should log the user out immediately and destroy the session.
114
     *
115
     * @param string $link The base link to use for this RequestHandler
116
     * @return LogoutHandler
117
     */
118
    public function getLogOutHandler($link)
119
    {
120
        // TODO: Implement getLogOutHandler() method.
121
    }
122
123
    /**
124
     * Return RequestHandler to manage the change-password process.
125
     *
126
     * The default URL of the RequetHandler should return the initial change-password form,
127
     * any other URL may be added for other steps & processing.
128
     *
129
     * URL-handling methods may return an array [ "Form" => (form-object) ] which can then
130
     * be merged into a default controller.
131
     *
132
     * @param string $link The base link to use for this RequestHnadler
133
     */
134
    public function getChangePasswordHandler($link)
135
    {
136
        // TODO: Implement getChangePasswordHandler() method.
137
    }
138
139
    /**
140
     * @param string $link
141
     * @return mixed
142
     */
143
    public function getLostPasswordHandler($link)
144
    {
145
        // TODO: Implement getLostPasswordHandler() method.
146
    }
147
148
    /**
149
     * Check if the passed password matches the stored one (if the member is not locked out).
150
     *
151
     * Note, we don't return early, to prevent differences in timings to give away if a member
152
     * password is invalid.
153
     *
154
     * @param Member $member
155
     * @param string $password
156
     * @param ValidationResult $result
157
     * @return ValidationResult
158
     */
159
    public function checkPassword(Member $member, $password, ValidationResult &$result = null)
160
    {
161
        // TODO: Implement checkPassword() method.
162
    }
163
}
164