Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Auth;
8
9use MediaWiki\User\User;
10use StatusValue;
11
12/**
13 * Secondary providers act after input data is already associated with a MediaWiki account.
14 *
15 * For login, a secondary provider performs additional authentication steps
16 * after a PrimaryAuthenticationProvider has identified which MediaWiki user is
17 * trying to log in. For example, it might implement a password reset, request
18 * the second factor for two-factor auth, or prevent the login if the account is blocked.
19 *
20 * For account creation, a secondary provider performs optional extra steps after
21 * a PrimaryAuthenticationProvider has created the user; for example, it can collect
22 * further user information such as a biography.
23 *
24 * (For account linking, secondary providers are not involved.)
25 *
26 * This interface also provides methods for changing authentication data such
27 * as a second-factor token, and callbacks that are invoked after login / account creation
28 * succeeded or failed.
29 *
30 * @ingroup Auth
31 * @since 1.27
32 * @see https://www.mediawiki.org/wiki/Manual:SessionManager_and_AuthManager
33 */
34interface SecondaryAuthenticationProvider extends AuthenticationProvider {
35
36    /**
37     * Start an authentication flow
38     *
39     * Note that this may be called for a user even if
40     * beginSecondaryAccountCreation() was never called. The module should take
41     * the opportunity to do any necessary setup in that case.
42     *
43     * @param User $user User being authenticated. This may become a
44     *   "UserValue" in the future, or User may be refactored into such.
45     * @param AuthenticationRequest[] $reqs
46     * @return AuthenticationResponse Expected responses:
47     *  - PASS: The user is authenticated. Additional secondary providers may run.
48     *  - FAIL: The user is not authenticated. Fail the authentication process.
49     *  - ABSTAIN: Additional secondary providers may run.
50     *  - UI: Additional AuthenticationRequests are needed to complete the process.
51     *  - REDIRECT: Redirection to a third party is needed to complete the process.
52     */
53    public function beginSecondaryAuthentication( $user, array $reqs );
54
55    /**
56     * Continue an authentication flow
57     *
58     * @param User $user User being authenticated. This may become a "UserIdentity" in the future.
59     * @param AuthenticationRequest[] $reqs
60     * @return AuthenticationResponse Expected responses:
61     *  - PASS: The user is authenticated. Additional secondary providers may run.
62     *  - FAIL: The user is not authenticated. Fail the authentication process.
63     *  - ABSTAIN: Additional secondary providers may run.
64     *  - UI: Additional AuthenticationRequests are needed to complete the process.
65     *  - REDIRECT: Redirection to a third party is needed to complete the process.
66     */
67    public function continueSecondaryAuthentication( $user, array $reqs );
68
69    /**
70     * Post-login callback
71     *
72     * This will be called at the end of a login attempt. It will not be called for unfinished
73     * login attempts that fail by the session timing out.
74     *
75     * @param User|null $user User that was attempted to be logged in, if known.
76     *  This may become a "UserIdentity" in the future.
77     * @param AuthenticationResponse $response Authentication response that will be returned
78     *  (PASS or FAIL)
79     */
80    public function postAuthentication( $user, AuthenticationResponse $response );
81
82    /**
83     * Revoke the user's credentials
84     *
85     * This may cause the user to no longer exist for the provider, or the user
86     * may continue to exist in a "disabled" state.
87     *
88     * The intention is that the named account will never again be usable for
89     * normal login (i.e. there is no way to undo the revocation of access).
90     *
91     * @param string $username
92     */
93    public function providerRevokeAccessForUser( $username );
94
95    /**
96     * Determine whether a property can change
97     * @see AuthManager::allowsPropertyChange()
98     * @param string $property
99     * @return bool
100     */
101    public function providerAllowsPropertyChange( $property );
102
103    /**
104     * Validate a change of authentication data (e.g. passwords)
105     *
106     * Return StatusValue::newGood( 'ignored' ) if you don't support this
107     * AuthenticationRequest type.
108     *
109     * @param AuthenticationRequest $req
110     * @param bool $checkData If false, $req hasn't been loaded from the
111     *  submission so checks on user-submitted fields should be skipped.
112     *  $req->username is considered user-submitted for this purpose, even
113     *  if it cannot be changed via $req->loadFromSubmission.
114     * @return StatusValue
115     */
116    public function providerAllowsAuthenticationDataChange(
117        AuthenticationRequest $req, $checkData = true
118    );
119
120    /**
121     * Change or remove authentication data (e.g. passwords)
122     *
123     * If $req was returned for AuthManager::ACTION_CHANGE, the corresponding
124     * credentials should result in a successful login in the future.
125     *
126     * If $req was returned for AuthManager::ACTION_REMOVE, the corresponding
127     * credentials should no longer result in a successful login.
128     *
129     * It can be assumed that providerAllowsAuthenticationDataChange with $checkData === true
130     * was called before this, and passed. This method should never fail (other than throwing an
131     * exception).
132     */
133    public function providerChangeAuthenticationData( AuthenticationRequest $req );
134
135    /**
136     * Determine whether an account creation may begin
137     *
138     * Called from AuthManager::beginAccountCreation()
139     *
140     * @note No need to test if the account exists, AuthManager checks that
141     * @param User $user User being created (not added to the database yet).
142     *  This may become a "UserIdentity" in the future.
143     * @param User $creator User doing the creation. This may become a "UserIdentity" in the future.
144     * @param AuthenticationRequest[] $reqs
145     * @return StatusValue
146     */
147    public function testForAccountCreation( $user, $creator, array $reqs );
148
149    /**
150     * Start an account creation flow
151     *
152     * @note There is no guarantee this will be called in a successful account
153     *   creation process as the user can just abandon the process at any time
154     *   after the primary provider has issued a PASS and still have a valid
155     *   account. Be prepared to handle any database inconsistencies that result
156     *   from this or continueSecondaryAccountCreation() not being called.
157     * @param User $user User being created (has been added to the database).
158     *  This may become a "UserIdentity" in the future.
159     * @param User $creator User doing the creation. This may become a "UserIdentity" in the future.
160     * @param AuthenticationRequest[] $reqs
161     * @return AuthenticationResponse Expected responses:
162     *  - PASS: The user creation is ok. Additional secondary providers may run.
163     *  - ABSTAIN: Additional secondary providers may run.
164     *  - UI: Additional AuthenticationRequests are needed to complete the process.
165     *  - REDIRECT: Redirection to a third party is needed to complete the process.
166     */
167    public function beginSecondaryAccountCreation( $user, $creator, array $reqs );
168
169    /**
170     * Continue an authentication flow
171     *
172     * @param User $user User being created (has been added to the database).
173     *  This may become a "UserIdentity" in the future.
174     * @param User $creator User doing the creation. This may become a "UserIdentity" in the future.
175     * @param AuthenticationRequest[] $reqs
176     * @return AuthenticationResponse Expected responses:
177     *  - PASS: The user creation is ok. Additional secondary providers may run.
178     *  - ABSTAIN: Additional secondary providers may run.
179     *  - UI: Additional AuthenticationRequests are needed to complete the process.
180     *  - REDIRECT: Redirection to a third party is needed to complete the process.
181     */
182    public function continueSecondaryAccountCreation( $user, $creator, array $reqs );
183
184    /**
185     * Post-creation callback
186     *
187     * This will be called at the end of an account creation attempt. It will not be called if
188     * the account creation process results in a session timeout (possibly after a successful
189     * user creation, while a secondary provider is waiting for a response).
190     *
191     * @param User $user User that was attempted to be created.
192     *  This may become a "UserIdentity" in the future.
193     * @param User $creator User doing the creation. This may become a "UserIdentity" in the future.
194     * @param AuthenticationResponse $response Authentication response that will be returned
195     *   (PASS or FAIL)
196     */
197    public function postAccountCreation( $user, $creator, AuthenticationResponse $response );
198
199    /**
200     * Determine whether an account may be created
201     *
202     * @param User $user User being created (not added to the database yet).
203     *   This may become a "UserIdentity" in the future.
204     * @param bool|string $autocreate False if this is not an auto-creation, or
205     *  the source of the auto-creation passed to AuthManager::autoCreateUser().
206     * @phpcs:ignore Generic.Files.LineLength.TooLong
207     * @param array{flags?:int,creating?:bool,canAlwaysAutocreate?:bool,performer?:User|\MediaWiki\Permissions\Authority} $options
208     * Options for the account creation check.
209     *  - `flags`: Bitfield of IDBAccessObject::READ_* constants, default IDBAccessObject::READ_NORMAL.
210     *  - `creating`: If false (or missing), this call is only testing whether
211     *    a user could be created. If true, this (non-autocreation) call is for
212     *    actually creating an account and will be followed by a call to
213     *    testForAccountCreation(). In this case, the provider might return
214     *    StatusValue::newGood() here and let the later call to
215     *    testForAccountCreation() do a more thorough test.
216     *  - `canAlwaysAutocreate`: If true, the session provider is exempt from
217     *    autocreate user permission checks.
218     *  - `performer`: The performer of the action, used for user rights checking.
219     * @return StatusValue
220     */
221    public function testUserForCreation( $user, $autocreate, array $options = [] );
222
223    /**
224     * Post-auto-creation callback
225     *
226     * @param User $user User being created (has been added to the database now).
227     *  This may become a "UserIdentity" in the future.
228     * @param string $source The source of the auto-creation passed to
229     *  AuthManager::autoCreateUser().
230     */
231    public function autoCreatedAccount( $user, $source );
232
233}