|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\SAML\Authenticators; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
|
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
7
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
8
|
|
|
use SilverStripe\Control\HTTPResponse_Exception; |
|
9
|
|
|
use SilverStripe\Control\RequestHandler; |
|
10
|
|
|
use SilverStripe\SAML\Helpers\SAMLHelper; |
|
11
|
|
|
|
|
12
|
|
|
class SAMLLoginHandler extends RequestHandler |
|
13
|
|
|
{ |
|
14
|
|
|
private static $url_handlers = [ |
|
|
|
|
|
|
15
|
|
|
'' => 'login', |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
19
|
|
|
'login', |
|
20
|
|
|
'LoginForm' |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
private static $dependencies = [ |
|
|
|
|
|
|
24
|
|
|
'helper' => '%$' . SAMLHelper::class |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var SAMLAuthenticator |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $authenticator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var SAMLHelper |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $helper; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Link to this handler |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $link = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $link The URL to recreate this request handler |
|
46
|
|
|
* @param SAMLAuthenticator $authenticator The authenticator to use |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($link, SAMLAuthenticator $authenticator) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->link = $link; |
|
51
|
|
|
$this->authenticator = $authenticator; |
|
52
|
|
|
parent::__construct(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* URL handler for the log-in screen |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function login() |
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
'Form' => $this->loginForm(), |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function loginForm() |
|
68
|
|
|
{ |
|
69
|
|
|
return SAMLLoginForm::create( |
|
70
|
|
|
$this, |
|
71
|
|
|
get_class($this->authenticator), |
|
72
|
|
|
'LoginForm' |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return a link to this request handler. |
|
78
|
|
|
* The link returned is supplied in the constructor |
|
79
|
|
|
* |
|
80
|
|
|
* @param null|string $action |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function Link($action = null) |
|
84
|
|
|
{ |
|
85
|
|
|
$link = Controller::join_links($this->link, $action); |
|
86
|
|
|
$this->extend('updateLink', $link, $action); |
|
87
|
|
|
return $link; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Login form handler method |
|
92
|
|
|
* |
|
93
|
|
|
* This method is called when the user finishes the login flow |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $data Submitted data |
|
96
|
|
|
* @param SAMLLoginForm $form |
|
97
|
|
|
* @param HTTPRequest $request |
|
98
|
|
|
* @return void This method never returns anything - it just redirects the user to the IdP |
|
99
|
|
|
* @throws HTTPResponse_Exception |
|
100
|
|
|
*/ |
|
101
|
|
|
public function doLogin($data, SAMLLoginForm $form, HTTPRequest $request) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$backURL = (isset($data['BackURL']) ? $data['BackURL'] : null); |
|
104
|
|
|
$this->helper->redirect($this, $request, $backURL); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return SAMLHelper |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getHelper() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->helper; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sets the SAMLHelper that this login handler should use to redirect users to the IdP |
|
117
|
|
|
* |
|
118
|
|
|
* @param SAMLHelper $helper |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setHelper(SAMLHelper $helper) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->helper = $helper; |
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|