|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* OpauthLoginForm |
|
5
|
|
|
* The form presented to users for signing in with an Opauth strategy. |
|
6
|
|
|
* Not a form, rather a gateway that works by taking enabled strategies and |
|
7
|
|
|
* displaying a button to start the OAuth process with that strategy provider. |
|
8
|
|
|
* @author Will Morgan <@willmorgan> |
|
9
|
|
|
* @author Dan Hensby <@dhensby> |
|
10
|
|
|
* @copyright Copyright (c) 2013, Better Brief LLP |
|
11
|
|
|
*/ |
|
12
|
|
|
class OpauthLoginForm extends LoginForm { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
private |
|
15
|
|
|
/* |
|
16
|
|
|
* @var boolean |
|
17
|
|
|
*/ |
|
18
|
|
|
$_strategiesDefined = false; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
protected |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array config |
|
23
|
|
|
*/ |
|
24
|
|
|
$authenticator_class = 'OpauthAuthenticator'; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
private static |
|
27
|
|
|
$allowed_actions = array( |
|
|
|
|
|
|
28
|
|
|
'httpSubmission', |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($controller, $name) { |
|
32
|
|
|
parent::__construct($controller, $name, $this->getFields(), $this->getActions()); |
|
33
|
|
|
$this->configureBackURL(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Handle any backURL. Uses sessions as state gets lost through OAuth flow. |
|
38
|
|
|
* Use the same session key as MemberLoginForm for x-compat |
|
39
|
|
|
*/ |
|
40
|
|
|
public function configureBackURL() { |
|
41
|
|
|
if($backURL = $this->controller->request->requestVar('BackURL')) { |
|
42
|
|
|
Session::set('BackURL', $backURL); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Override httpSubmission so we definitely have strategy handlers. |
|
48
|
|
|
* This is because Form::httpSubmission is directly called. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function httpSubmission($request) { |
|
51
|
|
|
$this->defineStrategyHandlers(); |
|
52
|
|
|
return parent::httpSubmission($request); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Channel several unknown strategies in to one handler |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function defineStrategyHandlers() { |
|
59
|
|
|
if(!$this->_strategiesDefined) { |
|
60
|
|
|
foreach($this->getStrategies() as $strategyClass) { |
|
61
|
|
|
$strategyMethod = 'handleStrategy' . $strategyClass; |
|
62
|
|
|
$this->addWrapperMethod($strategyMethod, 'handleStrategy'); |
|
63
|
|
|
} |
|
64
|
|
|
$this->_strategiesDefined = true; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Ensure AuthenticationMethod is set to tell Security which form to process |
|
70
|
|
|
* Very important for multi authenticator form setups. |
|
71
|
|
|
* @return FieldList |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getFields() { |
|
74
|
|
|
return new FieldList( |
|
75
|
|
|
new HiddenField('AuthenticationMethod', null, $this->authenticator_class) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Provide an action button to be clicked per strategy |
|
81
|
|
|
* @return FieldList |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getActions() { |
|
84
|
|
|
$actions = new FieldList(); |
|
85
|
|
|
foreach($this->getStrategies() as $strategyClass) { |
|
86
|
|
|
$strategyMethod = 'handleStrategy' . $strategyClass; |
|
87
|
|
|
$fa = new FormAction($strategyMethod, $strategyClass); |
|
88
|
|
|
$fa->setUseButtonTag(true); |
|
89
|
|
|
$actions->push($fa); |
|
90
|
|
|
} |
|
91
|
|
|
return $actions; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return array All enabled strategies from config |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getStrategies() { |
|
98
|
|
|
return OpauthAuthenticator::get_enabled_strategies(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Global endpoint for handleStrategy - all strategy actions point here. |
|
103
|
|
|
* @throws LogicException This should not be directly called. |
|
104
|
|
|
* @throws InvalidArgumentException The strategy must be valid and existent |
|
105
|
|
|
* @param string $funcName The bound function name from addWrapperMethod |
|
106
|
|
|
* @param array $data Standard data param as part of form submission |
|
107
|
|
|
* @param OpauthLoginForm $form |
|
108
|
|
|
* @param SS_HTTPRequest $request |
|
109
|
|
|
* @return ViewableData |
|
110
|
|
|
*/ |
|
111
|
|
|
public function handleStrategy($funcName, $data, $form, $request) { |
|
|
|
|
|
|
112
|
|
|
if(func_num_args() < 4) { |
|
113
|
|
|
throw new LogicException('Must be called with a strategy handler'); |
|
114
|
|
|
} |
|
115
|
|
|
// Trim handleStrategy from the function name: |
|
116
|
|
|
$strategy = substr($funcName, strlen('handleStrategy')) . 'Strategy'; |
|
117
|
|
|
|
|
118
|
|
|
// Check the strategy is good |
|
119
|
|
|
if(!class_exists($strategy) || $strategy instanceof OpauthStrategy) { |
|
120
|
|
|
throw new InvalidArgumentException('Opauth strategy ' . $strategy . ' was not found or is not a valid strategy'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this->controller->redirect( |
|
124
|
|
|
Controller::join_links( |
|
125
|
|
|
OpauthController::get_path(), |
|
126
|
|
|
OpauthAuthenticator::get_strategy_segment($strategy) |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* The authenticator name, used in templates |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getAuthenticatorName() { |
|
136
|
|
|
return OpauthAuthenticator::get_name(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.