Skip to content

Commit 8075a68

Browse files
alistair3149claude
andauthored
Refuse to provision against a wiki that cannot issue OAuth2 tokens (#520)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 37719df commit 8075a68

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

docs/testing.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,15 @@ Extension:OAuth works.
167167
### 1. Prerequisites and the environment contract
168168

169169
You need a reachable MediaWiki container with **Extension:OAuth installed and
170-
OAuth2 enabled** (OAuth2 signing keys configured on the wiki), a known admin
171-
account, and a local build of this repo (`npm run build`). Any environment that
172-
satisfies the contract below works.
170+
OAuth2 enabled**, a known admin account, and a local build of this repo
171+
(`npm run build`). Any environment that satisfies the contract below works.
172+
173+
Installing Extension:OAuth is not enough to issue OAuth2 tokens. The wiki also
174+
needs `$wgOAuth2PrivateKey` and `$wgOAuth2PublicKey` set, the admin account needs
175+
an email address, and that account needs `mwoauthproposeconsumer` and
176+
`mwoauthmanageconsumer` — rights Extension:OAuth grants to no group by default.
177+
The provisioning script checks all three and prints the fix for whatever is
178+
missing, so run it first and let it tell you.
173179

174180
| Variable | Meaning | Source |
175181
|---|---|---|

scripts/provision-dev-wiki.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,61 @@ docker exec "$CONTAINER" test -f "$RUN_PHP" \
107107
docker exec "$CONTAINER" test -f "$OAUTH_SCRIPT" \
108108
|| die "Extension:OAuth not found at $MW_PATH/extensions/OAuth (install it and enable OAuth2)"
109109

110+
# Extension:OAuth being installed does not make the wiki able to issue OAuth2
111+
# tokens. Registration needs rights that Extension:OAuth grants to nobody by
112+
# default, and the token endpoint needs a signing keypair. Missing either, a
113+
# consumer registered here — or in the browser — is useless, so check before
114+
# provisioning rather than handing back credentials that cannot work.
115+
readiness_php='$u=User::newFromName("'"$ADMIN_USER"'");'
116+
readiness_php+='$p=MediaWiki\MediaWikiServices::getInstance()->getPermissionManager();'
117+
readiness_php+='echo "KEYS=".(((string)($GLOBALS["wgOAuth2PrivateKey"]??"")!==""'
118+
readiness_php+='&&(string)($GLOBALS["wgOAuth2PublicKey"]??"")!=="")?"yes":"no")'
119+
readiness_php+='." EMAIL=".($u->getEmail()!==""?"yes":"no")'
120+
readiness_php+='." PROPOSE=".($p->userHasRight($u,"mwoauthproposeconsumer")?"yes":"no")'
121+
readiness_php+='." APPROVE=".($p->userHasRight($u,"mwoauthmanageconsumer")?"yes":"no")."\n";'
122+
readiness="$(printf '%s' "$readiness_php" \
123+
| docker exec -i "$CONTAINER" php "$RUN_PHP" eval 2>/dev/null | tr -d '\r')"
124+
125+
case "$readiness" in
126+
*KEYS=*)
127+
missing=''
128+
case "$readiness" in *KEYS=no*) missing="${missing}keys " ;; esac
129+
case "$readiness" in *EMAIL=no*) missing="${missing}email " ;; esac
130+
case "$readiness" in *PROPOSE=no*) missing="${missing}propose " ;; esac
131+
case "$readiness" in *APPROVE=no*) missing="${missing}approve " ;; esac
132+
if [ -n "$missing" ]; then
133+
log "This wiki cannot issue OAuth2 tokens yet. Fix the items below, then re-run."
134+
log ""
135+
case "$missing" in *keys*)
136+
log " OAuth2 signing keys are not configured. Generate a keypair:"
137+
log " openssl genrsa -out oauth2.key 2048"
138+
log " openssl rsa -in oauth2.key -pubout -out oauth2.pub"
139+
log " and set \$wgOAuth2PrivateKey / \$wgOAuth2PublicKey in LocalSettings.php"
140+
log " to the key text (or to paths the web server user can read)."
141+
log "" ;;
142+
esac
143+
case "$missing" in *propose*|*approve*)
144+
log " ${ADMIN_USER} may not register consumers. Extension:OAuth grants these"
145+
log " to no group by default, so add to LocalSettings.php:"
146+
log " \$wgGroupPermissions['sysop']['mwoauthproposeconsumer'] = true;"
147+
log " \$wgGroupPermissions['sysop']['mwoauthmanageconsumer'] = true;"
148+
log "" ;;
149+
esac
150+
case "$missing" in *email*)
151+
log " ${ADMIN_USER} has no email address, which consumer registration requires."
152+
log " Set one at Special:Preferences (or Special:ChangeEmail) on the wiki."
153+
log "" ;;
154+
esac
155+
die "wiki not ready for OAuth2 (missing: ${missing% })"
156+
fi
157+
;;
158+
*)
159+
log "warning: could not determine whether this wiki can issue OAuth2 tokens."
160+
log "Continuing; if sign-in later fails, check the OAuth2 signing keys and the"
161+
log "mwoauthproposeconsumer right for ${ADMIN_USER}."
162+
;;
163+
esac
164+
110165
# --- register consumer (only if the stock CLI supports OAuth2) ---------------
111166
# The OAuth2 flags exist only in newer Extension:OAuth. The copy bundled with the
112167
# MediaWiki 1.43 LTS ships an OAuth1-only createOAuthConsumer.php, so the consumer

0 commit comments

Comments
 (0)