Skip to content

Commit aa1efd3

Browse files
alistair3149claude
andcommitted
Ask for the member's address in a box of its own
The code route borrowed the login form's username box. A field several providers describe is labelled by the first of them, and this one is not it, so the address box was labelled "Username", placeholded "Enter your username" and autocompleted from the username the browser had saved. The messages meant to say otherwise, memberaccess-auth-email-label and -help, never reached the page. The button sending the code also sat above the button it is an alternative to, styled as the form's primary button beside the one that already was. Ask for the address in a field of this request's own, below the password form and behind a divider, with a button that is progressive without being primary. A box of its own means the request declares no username, so a code request names no account to MediaWiki and is counted against the client IP rather than against one address. Telling one address from another is left to the extension's own throttle, which is the tighter of the two. Declaring a username anyway would put a second answer on a form that already has the username box, and two requests naming different accounts is a conflict MediaWiki raises rather than resolves. Considered, omitted: * leading with the member route and folding the password form away, which suits a wiki whose logins are mostly members but hides the staff route * a heading over the section, which the divider and the field's own label already say * revisiting the wiki-wide badloginperuser captcha concession, which was made because the address sat in the field ConfirmEdit reads as the login subject and may no longer be needed now that it does not Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 0c08453 commit aa1efd3

8 files changed

Lines changed: 214 additions & 67 deletions

File tree

‎extension.json‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,28 @@
3737
},
3838
"api": {
3939
"factory": "ProfessionalWiki\\MemberAccess\\MemberAccessExtension::newUserListApiHookHandler"
40+
},
41+
"loginForm": {
42+
"class": "ProfessionalWiki\\MemberAccess\\EntryPoints\\LoginFormHandler"
4043
}
4144
},
4245
"Hooks": {
4346
"LoadExtensionSchemaUpdates": "schema",
4447
"PluggableAuthUserAuthorization": "sso",
4548
"UserLoggedIn": "login",
4649
"SpecialPasswordResetOnSubmit": "passwordReset",
47-
"ApiCheckCanExecute": "api"
50+
"ApiCheckCanExecute": "api",
51+
"AuthChangeFormFields": "loginForm",
52+
"BeforePageDisplay": "loginForm"
53+
},
54+
"ResourceFileModulePaths": {
55+
"localBasePath": "modules",
56+
"remoteExtPath": "MemberAccess/modules"
57+
},
58+
"ResourceModules": {
59+
"ext.memberAccess.loginForm": {
60+
"styles": [ "ext.memberAccess.loginForm.less" ]
61+
}
4862
},
4963
"callback": "ProfessionalWiki\\MemberAccess\\EntryPoints\\RegistrationHandler::onRegistration",
5064
"AuthManagerAutoConfig": {

‎i18n/en.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"memberaccess-description": "Members-only wiki access with email one-time-code login and an allowlist of addresses and domains",
88
"memberaccess-code-email-subject": "Your login code for {{SITENAME}}",
99
"memberaccess-code-email-body": "Your login code for {{SITENAME}} is:\n\n$1\n\nThe code is valid for $2 {{PLURAL:$2|minute|minutes}} and can be used once.\n\nIf you did not request a login code, you can ignore this message.",
10-
"memberaccess-auth-button-label": "Email me a login code",
10+
"memberaccess-auth-button-label": "Continue with email",
1111
"memberaccess-auth-button-help": "We send a one-time code to your email address. Nothing beyond your email address is requested.",
1212
"memberaccess-auth-email-label": "Email address",
1313
"memberaccess-auth-email-help": "The address you are a member with.",
14+
"memberaccess-auth-email-placeholder": "Enter your email",
15+
"memberaccess-auth-form-divider": "or",
1416
"memberaccess-auth-email-missing": "Enter your email address to get a login code.",
1517
"memberaccess-auth-email-invalid": "Enter a valid email address.",
1618
"memberaccess-auth-throttled": "Too many login codes have been requested. Try again later.",

‎i18n/qqq.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"memberaccess-description": "{{desc|name=MemberAccess|url=https://professional.wiki}}",
88
"memberaccess-code-email-subject": "Subject line of the email carrying a one-time login code.",
99
"memberaccess-code-email-body": "Plain text body of the email carrying a one-time login code.\n\nParameters:\n* $1 - the login code\n* $2 - the number of minutes the code stays valid",
10-
"memberaccess-auth-button-label": "Label of the button on the login form that asks for a login code to be mailed.",
10+
"memberaccess-auth-button-label": "Label of the button on the login form that asks for a login code to be mailed to the address above it.",
1111
"memberaccess-auth-button-help": "Help text below the button that asks for a login code to be mailed. \"Nothing beyond your email address is requested\" is a commitment: keep the meaning \"asked for\", and do not turn it into \"stored\".",
1212
"memberaccess-auth-email-label": "Label of the email address field.",
1313
"memberaccess-auth-email-help": "Help text for the email address field.",
14+
"memberaccess-auth-email-placeholder": "Placeholder shown in the email address field on the login form. Follows the wording of {{msg-mw|Userlogin-yourname-ph}} and {{msg-mw|Userlogin-yourpassword-ph}} above it.",
15+
"memberaccess-auth-form-divider": "Separator between the password login form and the email login section below it on the login form. Kept to a word or two.\n{{Identical|Or}}",
1416
"memberaccess-auth-email-missing": "Error shown when a login code is asked for without an email address.",
1517
"memberaccess-auth-email-invalid": "Error shown when the entered email address is malformed.",
1618
"memberaccess-auth-throttled": "Error shown when too many login codes have been asked for. The translation must not reveal whether the address is a member.",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Sets the member login route apart from the password form above it.
3+
*/
4+
5+
@import 'mediawiki.skin.variables.less';
6+
7+
.mw-memberaccess-divider {
8+
display: flex;
9+
align-items: center;
10+
margin: @spacing-100 0 @spacing-75;
11+
color: @color-subtle;
12+
}
13+
14+
.mw-memberaccess-divider::before,
15+
.mw-memberaccess-divider::after {
16+
border-top: @border-subtle;
17+
content: '';
18+
flex: 1;
19+
}
20+
21+
.mw-memberaccess-divider .oo-ui-fieldLayout-header,
22+
.mw-memberaccess-divider .cdx-label {
23+
display: none;
24+
}
25+
26+
.mw-memberaccess-divider .mw-htmlform-field-HTMLInfoField,
27+
.mw-memberaccess-divider .cdx-field__control {
28+
padding: 0 @spacing-50;
29+
}

‎src/EntryPoints/Auth/LoginCodeRequest.php‎

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,36 @@
66

77
use MediaWiki\Auth\ButtonAuthenticationRequest;
88
use MediaWiki\Message\Message;
9-
use ProfessionalWiki\MemberAccess\Application\NormalizedEmail;
109

1110
/**
1211
* The "email me a login code" button on the login form, with the address to send the code to.
1312
*
14-
* The address travels in the username field the login form already has, because a member's address
15-
* is their username here. That keeps one identity box on the form however the visitor goes on to
16-
* prove it, and it is how MediaWiki learns whose login this is: it looks for a request declaring a
17-
* username, and counts the attempt against that account's throttle rather than against one bucket
18-
* shared by everyone at the same client IP.
13+
* The address is asked for in a box of this request's own rather than in the login form's username
14+
* box. Sharing that box left the address labelled as a username, since a field every provider
15+
* describes is labelled by the first of them and this one is not it, and a member had to work out
16+
* that their address was what belonged there.
17+
*
18+
* A box of its own means this request declares no username, so MediaWiki learns no account from a
19+
* code request and counts the attempt against the client IP rather than against one address. The
20+
* extension's own throttle is what tells one address from another, and it is the tighter of the
21+
* two. {@see \ProfessionalWiki\MemberAccess\Application\RequestThrottle}
22+
*
23+
* Declaring one anyway would put a second answer on a form that already has the username box: two
24+
* requests naming different accounts is a conflict MediaWiki raises rather than resolves, and
25+
* nothing stops a visitor filling both boxes before pressing either button.
1926
*
2027
* The field is optional. Marked required it would be required of the whole login form, since a
21-
* shared field is mandatory only where every provider asking for it says so, and the box would then
22-
* have to be filled before any other login button could be pressed. An empty box is refused when
23-
* this button is pressed instead.
28+
* field is mandatory only where every provider asking for it says so, and the box would then have
29+
* to be filled before any other login button could be pressed. An empty box is refused when this
30+
* button is pressed instead.
2431
*/
2532
class LoginCodeRequest extends ButtonAuthenticationRequest {
2633

2734
public const BUTTON_NAME = 'memberaccessLogin';
35+
public const EMAIL_FIELD = 'memberaccessEmail';
2836

2937
public bool $memberaccessLogin = false;
30-
31-
private string $address = '';
38+
public ?string $memberaccessEmail = null;
3239

3340
public function __construct() {
3441
parent::__construct(
@@ -45,7 +52,7 @@ public function __construct() {
4552
public function getFieldInfo() {
4653
return array_merge(
4754
[
48-
'username' => [
55+
self::EMAIL_FIELD => [
4956
'type' => 'string',
5057
'label' => new Message( 'memberaccess-auth-email-label' ),
5158
'help' => new Message( 'memberaccess-auth-email-help' ),
@@ -57,34 +64,12 @@ public function getFieldInfo() {
5764
}
5865

5966
/**
60-
* @param array<string, mixed> $data
67+
* The address is passed on with no more than its surrounding space removed. What was typed is
68+
* still answered on, so that a mistyped address can be named as one, and the address a code is
69+
* sent to is the address that was asked for.
6170
*/
62-
public function loadFromSubmission( array $data ): bool {
63-
if ( !parent::loadFromSubmission( $data ) ) {
64-
return false;
65-
}
66-
67-
$this->address = is_string( $this->username ) ? trim( $this->username ) : '';
68-
69-
// A box holding no address names nobody. Left as submitted it would still read as an answer,
70-
// which MediaWiki normalises to nothing and counts against no throttle at all; emptied, the
71-
// attempt is counted against the client IP instead. What was typed is still answered on, so
72-
// that a mistyped address can be named as one.
73-
//
74-
// An address is passed on exactly as submitted, since a password request reading the same
75-
// box has to arrive at the same string or MediaWiki sees two conflicting logins. That leaves
76-
// an address MediaWiki cannot turn into a username, one holding a "#" for instance, counted
77-
// against nothing here; the extension's own per-address and per-IP limits, tighter than
78-
// core's, are what bound those.
79-
if ( NormalizedEmail::fromString( $this->address ) === null ) {
80-
$this->username = null;
81-
}
82-
83-
return true;
84-
}
85-
8671
public function address(): string {
87-
return $this->address;
72+
return is_string( $this->memberaccessEmail ) ? trim( $this->memberaccessEmail ) : '';
8873
}
8974

9075
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare( strict_types = 1 );
4+
5+
namespace ProfessionalWiki\MemberAccess\EntryPoints;
6+
7+
use MediaWiki\Auth\AuthenticationRequest;
8+
use MediaWiki\Auth\AuthManager;
9+
use MediaWiki\Output\Hook\BeforePageDisplayHook;
10+
use MediaWiki\Output\OutputPage;
11+
use MediaWiki\SpecialPage\Hook\AuthChangeFormFieldsHook;
12+
use ProfessionalWiki\MemberAccess\EntryPoints\Auth\LoginCodeRequest;
13+
use Skin;
14+
15+
/**
16+
* Sets the member route apart on the login form, so that the box asking for an address and the
17+
* button sending the code read as one way in rather than as loose additions to the password form.
18+
*
19+
* Without this the two routes interleave: the fields are laid out in the order the providers
20+
* describe them, which puts the code button above the button it is an alternative to. What each box
21+
* is for is then left to whoever is reading the form.
22+
*/
23+
class LoginFormHandler implements AuthChangeFormFieldsHook, BeforePageDisplayHook {
24+
25+
/**
26+
* Core gives the log in button weight 100 and the help link below it 200, so the member route
27+
* goes between the two: below the password form it is an alternative to, above the links that
28+
* close the form off.
29+
*/
30+
private const DIVIDER_WEIGHT = 105;
31+
private const EMAIL_WEIGHT = 106;
32+
private const BUTTON_WEIGHT = 107;
33+
34+
/**
35+
* @param AuthenticationRequest[] $requests
36+
* @param array<string, mixed> $fieldInfo
37+
* @param array<string, mixed> &$formDescriptor
38+
* @param string $action
39+
*/
40+
public function onAuthChangeFormFields( $requests, $fieldInfo, &$formDescriptor, $action ): void {
41+
if ( $action !== AuthManager::ACTION_LOGIN || !isset( $formDescriptor[LoginCodeRequest::EMAIL_FIELD] ) ) {
42+
return;
43+
}
44+
45+
$formDescriptor[LoginCodeRequest::EMAIL_FIELD] = array_merge(
46+
$this->fieldAt( $formDescriptor, LoginCodeRequest::EMAIL_FIELD ),
47+
[
48+
'placeholder-message' => 'memberaccess-auth-email-placeholder',
49+
'autocomplete' => 'email',
50+
'cssclass' => 'mw-memberaccess-email',
51+
'weight' => self::EMAIL_WEIGHT
52+
]
53+
);
54+
55+
// Progressive, but not the primary button: that one is the log in button above, and a form
56+
// offering two of them says neither is the way on.
57+
$formDescriptor[LoginCodeRequest::BUTTON_NAME] = array_merge(
58+
$this->fieldAt( $formDescriptor, LoginCodeRequest::BUTTON_NAME ),
59+
[
60+
'flags' => [ 'progressive' ],
61+
'weight' => self::BUTTON_WEIGHT
62+
]
63+
);
64+
65+
$formDescriptor['memberaccessDivider'] = [
66+
'type' => 'info',
67+
'cssclass' => 'mw-memberaccess-divider',
68+
'default' => wfMessage( 'memberaccess-auth-form-divider' )->text(),
69+
'weight' => self::DIVIDER_WEIGHT
70+
];
71+
}
72+
73+
/**
74+
* @param OutputPage $out
75+
* @param Skin $skin
76+
*/
77+
public function onBeforePageDisplay( $out, $skin ): void {
78+
if ( $out->getTitle()?->isSpecial( 'Userlogin' ) === true ) {
79+
$out->addModuleStyles( 'ext.memberAccess.loginForm' );
80+
}
81+
}
82+
83+
/**
84+
* @param array<string, mixed> $formDescriptor
85+
* @return array<string, mixed>
86+
*/
87+
private function fieldAt( array $formDescriptor, string $name ): array {
88+
$field = $formDescriptor[$name] ?? [];
89+
90+
return is_array( $field ) ? $field : [];
91+
}
92+
93+
}

‎tests/phpunit/Integration/Auth/CodeRequestSubmission.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private function submittedCodeRequest( string $address ): array {
2020
$requests = AuthenticationRequest::loadRequestsFromSubmission(
2121
[ new LoginCodeRequest() ],
2222
[
23-
'username' => $address,
23+
LoginCodeRequest::EMAIL_FIELD => $address,
2424
LoginCodeRequest::BUTTON_NAME => true
2525
]
2626
);

0 commit comments

Comments
 (0)