Created
February 25, 2019 00:20
-
-
Save LordAro/07c6205ab17c36affa9294a2cbb982fc to your computer and use it in GitHub Desktop.
PluggableAuth Mediawiki auth plugin for URY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "URYAuthenticator", | |
"version": "1.0.0", | |
"author": [ | |
"Lloyd Wallis", | |
"Charles Pigott" | |
], | |
"url": "http://ury.org.uk/", | |
"type": "other", | |
"config": { | |
"PluggableAuth_Class": "URYAuthenticator" | |
}, | |
"AutoloadClasses": { | |
"URYAuthenticator": "URYAuthenticatorPluggable.php" | |
}, | |
"MessagesDirs": { | |
"URYAuthentictor": ["i18n"] | |
}, | |
"requires": { | |
"MediaWiki": ">= 1.27.0", | |
"extensions": { | |
"PluggableAuth": ">= 5.1" | |
} | |
}, | |
"manifest_version": 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"@metadata": { | |
"authors": [ | |
"Charles Pigott" | |
] | |
}, | |
"no-user": "No user matching the given MyRadio credentials", | |
"myradio-error": "Unknown error in MyRadio backend" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \MediaWiki\Auth\AuthManager; | |
class URYAuthenticator extends PluggableAuth { | |
const API_KEY = '<snip>'; | |
const API_ROOT = 'https://ury.org.uk/api/'; | |
private function myradioAPIRequest($path, $params = [], $method = 'GET') { | |
$url = URYAuthenticator::API_ROOT . $path; | |
$params['api_key'] = URYAuthenticator::API_KEY; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
if ($result === false) { | |
return curl_error($ch); | |
} | |
return json_decode($result, true); | |
} | |
public function authenticate( &$id, &$username, &$realname, &$email, &$errorMessage ) { | |
$authManager = AuthManager::singleton(); | |
$extraLoginFields = $authManager->getAuthenticationSessionData(PluggableAuthLogin::EXTRALOGINFIELDS_SESSION_KEY); | |
$api = $this->myradioAPIRequest( | |
"v2/auth/testcredentials", | |
["user" => $extraLoginFields["username"], "pass" => $extraLoginFields["password"]], | |
"POST" | |
); | |
if (!isset($api["status"]) || $api["status"] !== "OK" || !isset($api["payload"])) { | |
$errorMessage = wfMessage("myradio-error"); | |
return false; | |
} | |
if ($api["payload"] === false) { | |
$errorMessage = wfMessage("no-user"); | |
return false; | |
} | |
$id = 0; | |
$dbr = wfGetDB( DB_REPLICA ); | |
$existing_user_id = $dbr->selectField('user', 'user_id', ['user_name' => $api["payload"]['memberid']]); | |
if ($existing_user_id !== false) { | |
$id = $existing_user_id; | |
} | |
$username = $api["payload"]['memberid']; | |
$realname = $api["payload"]["fname"] . " " . $api["payload"]["sname"]; | |
$email = $api["payload"]["public_email"]; | |
return true; | |
} | |
public function deauthenticate( User &$user ) { | |
// TODO: Something? | |
} | |
public function saveExtraAttributes( $id ) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment