/
PluggableAuthLogin.php
160 lines (148 loc) · 5.94 KB
/
PluggableAuthLogin.php
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
<?php
/*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
namespace MediaWiki\Extension\PluggableAuth;
use MediaWiki\Auth\AuthManager;
use MediaWiki\Extension\PluggableAuth\Group\GroupProcessorRunner;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\User\UserIdentityValue;
use Message;
use Psr\Log\LoggerInterface;
use UnlistedSpecialPage;
use Wikimedia\Timestamp\ConvertibleTimestamp;
class PluggableAuthLogin extends UnlistedSpecialPage {
const RETURNTOURL_SESSION_KEY = 'PluggableAuthLoginReturnToUrl';
const EXTRALOGINFIELDS_SESSION_KEY = 'PluggableAuthLoginExtraLoginFields';
const AUTHENTICATIONPLUGINNAME_SESSION_KEY = 'PluggableAuthLoginAuthenticationPluginIndex';
const USERNAME_SESSION_KEY = 'PluggableAuthLoginUsername';
const REALNAME_SESSION_KEY = 'PluggableAuthLoginRealname';
const EMAIL_SESSION_KEY = 'PluggableAuthLoginEmail';
const ERROR_SESSION_KEY = 'PluggableAuthLoginError';
/**
* @var PluggableAuthFactory
*/
private $pluggableAuthFactory;
/**
* @var AuthManager
*/
private $authManager;
/**
* @var GroupProcessorRunner
*/
private $groupProcessorRunner;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var HookRunner
*/
private $hookRunner;
/**
* @param PluggableAuthFactory $pluggableAuthFactory
* @param AuthManager $authManager
* @param GroupProcessorRunner $groupProcessorRunner
*/
public function __construct( PluggableAuthFactory $pluggableAuthFactory, AuthManager $authManager,
GroupProcessorRunner $groupProcessorRunner ) {
parent::__construct( 'PluggableAuthLogin' );
$this->pluggableAuthFactory = $pluggableAuthFactory;
$this->authManager = $authManager;
$this->groupProcessorRunner = $groupProcessorRunner;
$this->logger = LoggerFactory::getInstance( 'PluggableAuth' );
}
/**
* Will be called automatically by `MediaWiki\SpecialPage\SpecialPageFactory::getPage`
* @inheritDoc
*/
public function setHookContainer( HookContainer $hookContainer ) {
parent::setHookContainer( $hookContainer );
$this->hookRunner = new HookRunner( $hookContainer );
}
/**
* @param string|null $subPage parameters (ignored)
*/
public function execute( $subPage ) {
$this->logger->debug( 'In execute()' );
$user = $this->getUser();
$pluggableauth = $this->pluggableAuthFactory->getInstance();
$error = null;
if ( $pluggableauth ) {
if ( $pluggableauth->authenticate( $id, $username, $realname, $email, $error ) ) {
if ( !$id ) {
$user->loadDefaults( $username );
if ( $realname !== null ) {
$user->setRealName( $realname );
}
$user->mName = $username;
$user->mEmail = $email;
$now = ConvertibleTimestamp::now( TS_UNIX );
$user->mEmailAuthenticated = $now;
$user->mTouched = $now;
$this->logger->debug( 'Authenticated new user: ' . $username );
// Group sync is done in `LocalUserCreated` hook
} else {
$user->mId = $id;
$user->loadFromId();
$this->logger->debug( 'Authenticated existing user: ' . $user->mName );
$userIdentity = new UserIdentityValue( $user->getId(), $user->getName() );
$this->groupProcessorRunner->run( $userIdentity, $pluggableauth );
}
$authorized = true;
$this->hookRunner->onPluggableAuthUserAuthorization( $user, $authorized );
if ( $authorized ) {
$this->authManager->setAuthenticationSessionData( self::USERNAME_SESSION_KEY, $username );
$this->authManager->setAuthenticationSessionData( self::REALNAME_SESSION_KEY, $realname );
$this->authManager->setAuthenticationSessionData( self::EMAIL_SESSION_KEY, $email );
$this->logger->debug( 'User is authorized.' );
} else {
$this->logger->debug( 'Authorization failure.' );
$error = ( new Message( 'pluggableauth-not-authorized', [ $username ] ) )->parse();
}
} else {
$this->logger->debug( 'Authentication failure.' );
if ( $error === null ) {
$error = ( new Message( 'pluggableauth-authentication-failure' ) )->text();
} else {
if ( !is_string( $error ) ) {
$error = strval( $error );
}
$this->logger->debug( 'ERROR: ' . $error );
}
}
} else {
$error = ( new Message( 'pluggableauth-authentication-plugin-failure' ) )->text();
}
if ( $error !== null ) {
$this->authManager->setAuthenticationSessionData( self::ERROR_SESSION_KEY, $error );
}
$returnToUrl = $this->authManager->getRequest()->getSessionData( self::RETURNTOURL_SESSION_KEY );
if ( $returnToUrl === null || strlen( $returnToUrl ) === 0 ) {
// This should never happen unless there is an issue in the authentication plugin, most
// likely resulting in session corruption. Since it is unclear if it is safe to continue,
// an error message is shown to the user and the authentication flow is terminated.
$this->logger->debug( 'ERROR: return to URL is null or empty' );
$this->getOutput()->wrapWikiMsg( "<div class='error'>\n$1\n</div>", 'pluggableauth-fatal-error' );
} else {
$this->getOutput()->redirect( $returnToUrl );
}
}
}