1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
<?php
use \MediaWiki\Auth\AuthenticationRequest;
use \MediaWiki\Auth\ButtonAuthenticationRequest;
use \MediaWiki\Auth\AbstractPrimaryAuthenticationProvider;
use \MediaWiki\Auth\AuthManager;
use \MediaWiki\Auth\AuthenticationResponse;
class PluggableAuthPrimaryAuthenticationProvider extends
AbstractPrimaryAuthenticationProvider {
/**
* Start an authentication flow
* @inheritDoc
*/
public function beginPrimaryAuthentication( array $reqs ) {
$request = ButtonAuthenticationRequest::getRequestByName( $reqs,
'pluggableauthlogin' );
if ( !$request ) {
return AuthenticationResponse::newAbstain();
}
$extraLoginFields = [];
foreach ( $GLOBALS['wgPluggableAuth_ExtraLoginFields'] as $key => $value ) {
if ( isset( $request, $key ) ) {
$extraLoginFields[$key] = $request->$key;
}
}
$url = Title::newFromText( 'Special:PluggableAuthLogin' )->getFullURL();
$this->manager->setAuthenticationSessionData(
PluggableAuthLogin::RETURNTOURL_SESSION_KEY, $request->returnToUrl );
$this->manager->setAuthenticationSessionData(
PluggableAuthLogin::EXTRALOGINFIELDS_SESSION_KEY, $extraLoginFields );
// @codingStandardsIgnoreStart
if ( isset( $_GET['returnto'] ) ) {
$returnto = $_GET['returnto'];
} else {
$returnto = '';
}
$this->manager->setAuthenticationSessionData(
PluggableAuthLogin::RETURNTOPAGE_SESSION_KEY, $returnto );
if ( isset( $_GET['returntoquery'] ) ) {
$returntoquery = $_GET['returntoquery'];
} else {
$returntoquery = '';
}
// @codingStandardsIgnoreEnd
$this->manager->setAuthenticationSessionData(
PluggableAuthLogin::RETURNTOQUERY_SESSION_KEY, $returntoquery );
return AuthenticationResponse::newRedirect( [
new PluggableAuthContinueAuthenticationRequest()
], $url );
}
/**
* Continue an authentication flow
* @inheritDoc
*/
public function continuePrimaryAuthentication( array $reqs ) {
$request = AuthenticationRequest::getRequestByClass( $reqs,
PluggableAuthContinueAuthenticationRequest::class );
if ( !$request ) {
return AuthenticationResponse::newFail(
wfMessage( 'pluggableauth-authentication-workflow-failure' ) );
}
$error = $this->manager->getAuthenticationSessionData(
PluggableAuthLogin::ERROR_SESSION_KEY );
if ( !is_null( $error ) ) {
$this->manager->removeAuthenticationSessionData(
PluggableAuthLogin::ERROR_SESSION_KEY );
return AuthenticationResponse::newFail( new RawMessage( $error ) );
}
$username = $request->username;
$user = User::newFromName( $username );
if ( $user && $user->getId() !== 0 ) {
$this->updateUserRealnameAndEmail( $user );
Hooks::run( 'PluggableAuthPopulateGroups', [ $user ] );
}
return AuthenticationResponse::newPass( $username );
}
/**
* Determine whether a property can change
* @inheritDoc
*/
public function providerAllowsPropertyChange( $property ) {
return $GLOBALS['wgPluggableAuth_EnableLocalProperties'];
}
private function updateUserRealNameAndEmail( $user, $force = false ) {
$realname = $this->manager->getAuthenticationSessionData(
PluggableAuthLogin::REALNAME_SESSION_KEY );
$this->manager->removeAuthenticationSessionData(
PluggableAuthLogin::REALNAME_SESSION_KEY );
$email = $this->manager->getAuthenticationSessionData(
PluggableAuthLogin::EMAIL_SESSION_KEY );
$this->manager->removeAuthenticationSessionData(
PluggableAuthLogin::EMAIL_SESSION_KEY );
if ( $user->mRealName != $realname || $user->mEmail != $email ) {
if ( $GLOBALS['wgPluggableAuth_EnableLocalProperties'] && !$force ) {
wfDebugLog( 'PluggableAuth', 'Local properties enabled.' );
wfDebugLog( 'PluggableAuth', 'Did not save updated real name and email address.' );
} else {
wfDebugLog( 'PluggableAuth', 'Local properties disabled or has just been created.' );
$user->mRealName = $realname;
if ( $email && Sanitizer::validateEmail( $email ) ) {
$user->mEmail = $email;
$user->confirmEmail();
}
$user->saveSettings();
wfDebugLog( 'PluggableAuth', 'Saved updated real name and email address.' );
}
} else {
wfDebugLog( 'PluggableAuth', 'Real name and email address did not change.' );
}
}
/**
* @inheritDoc
*/
public function autoCreatedAccount( $user, $source ) {
$this->updateUserRealNameAndEmail( $user, true );
$pluggableauth = PluggableAuth::singleton();
if ( $pluggableauth ) {
$pluggableauth->saveExtraAttributes( $user->mId );
}
}
/**
* Test whether the named user exists
* @inheritDoc
*/
public function testUserExists( $username, $flags = User::READ_NORMAL ) {
return false;
}
/**
* Validate a change of authentication data (e.g. passwords)
* @inheritDoc
*/
public function providerAllowsAuthenticationDataChange(
AuthenticationRequest $req, $checkData = true ) {
return StatusValue::newGood( 'ignored' );
}
/**
* Fetch the account-creation type
* @inheritDoc
*/
public function accountCreationType() {
return self::TYPE_LINK;
}
/**
* Start an account creation flow
* @inheritDoc
*/
public function beginPrimaryAccountCreation( $user, $creator, array $reqs ) {
return AuthenticationResponse::newAbstain();
}
/**
* Change or remove authentication data (e.g. passwords)
* @inheritDoc
*/
public function providerChangeAuthenticationData( AuthenticationRequest $req ) {
}
/**
* @inheritDoc
*/
public function getAuthenticationRequests( $action, array $options ) {
switch ( $action ) {
case AuthManager::ACTION_LOGIN:
return [
new PluggableAuthBeginAuthenticationRequest()
];
default:
return [];
}
}
}
|