Skip to content

Instantly share code, notes, and snippets.

@LordAro
Last active November 5, 2019 17:05
Show Gist options
  • Save LordAro/d499ca792112db086e458a5e877ed3ef to your computer and use it in GitHub Desktop.
Save LordAro/d499ca792112db086e458a5e877ed3ef to your computer and use it in GitHub Desktop.
<?php
use \MediaWiki\Session\SessionManager;
use \MediaWiki\Auth\AuthManager;
class URYAuthenticator extends PluggableAuth {
const API_KEY = '';
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 ) {
wfDebug("FOO - deauthenticate " . var_export($user, true));
}
public function saveExtraAttributes( $id ) {
wfDebug("FOO - save $id");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment