Skip to content

Commit

Permalink
Merge pull request #6 from cluebotng/move-to-oauth
Browse files Browse the repository at this point in the history
Implement Wikimedia OAuth for access
  • Loading branch information
DamianZaremba authored Jul 24, 2021
2 parents e83a2ca + 5265464 commit 397a2fc
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 168 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"phpmd/phpmd" : "@stable"
},
"require": {
"monolog/monolog": "^1.17"
"monolog/monolog": "^1.17",
"mediawiki/oauthclient": "^1.0"
},
"config": {
"platform": {
Expand Down
61 changes: 60 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified composer.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion includes/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
error_reporting(E_ALL | E_STRICT);
session_start();

require_once 'vendor/autoload.php';
require_once 'includes/Page.php';
require_once 'web-settings.php';
require_once 'includes/dbFunctions.php';
require_once 'includes/recaptchalib.php';

foreach (glob('pages/*.page.php') as $page) {
require_once $page;
Expand Down
1 change: 0 additions & 1 deletion includes/index.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<link type='text/css' rel='stylesheet' href='index.css'>
<link type='text/css' rel='stylesheet' href='diff.css'>
<title>ClueBot NG Report Interface</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<div id="top">
Expand Down
17 changes: 0 additions & 17 deletions includes/recaptchalib.php

This file was deleted.

55 changes: 0 additions & 55 deletions pages/CreateAccount.page.php

This file was deleted.

13 changes: 0 additions & 13 deletions pages/Options.page.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ public function __construct()
$query = "UPDATE `users` SET `next_on_review` = '" . mysqli_real_escape_string($mysql, $next_on_review) . "'";
$_SESSION['next_on_review'] = ($next_on_review) ? true : false;

if (trim($_POST['email']) != "") {
$query .= ", `email` = '" . mysqli_real_escape_string($mysql, $_POST['email']) . "'";
$_SESSION['email'] = mysqli_real_escape_string($mysql, $_POST['email']);
}

if (trim($_POST['password']) != "") {
$query .= ", `password` = PASSWORD('" . mysqli_real_escape_string($mysql, $_POST['password']) . "')";
}

$query .= " WHERE `userid` = '" . mysqli_real_escape_string($mysql, $_SESSION['userid']) . "'";
mysqli_query($mysql, $query);

Expand All @@ -45,15 +36,11 @@ public function writeContent()
echo '<p>Saved!</p>';
}
echo '<form action="" method="post">';
echo '<h3>Change password</h3>';
echo '<p>(Leave blank to ignore change)</p>';
echo '<p>Password: <input type="text" id="password" name="password" value="" /></p>';

echo '<h3>General options</h3>';
echo '<p>Redirect on review: <input type="checkbox" id="next_on_review" name="next_on_review" value="Yes"';
echo ($_SESSION['next_on_review']) ? ' checked=checked' : '';
echo ' /></p>';
echo '<p>Email: <input type="text" id="email" name="email" value="' . $_SESSION['email'] . '" /></p>';

echo '<p><input id="submit" name="submit" type="submit" value="Save" /></p>';
echo '</form>';
Expand Down
100 changes: 73 additions & 27 deletions pages/SignIn.page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,94 @@

namespace ReportInterface;

use MediaWiki\OAuthClient\Client;
use MediaWiki\OAuthClient\ClientConfig;
use MediaWiki\OAuthClient\Consumer;
use MediaWiki\OAuthClient\Token;

class SignInPage extends Page
{
public function __construct()
private function lookupUser($username)
{
global $mysql;
if (isset($_POST['submit'])) {
$query = 'SELECT `userid`, `username`, `admin`, `superadmin`, `next_on_review`, `email` FROM `users` WHERE `username` = ';
$query .= '\'' . mysqli_real_escape_string($mysql, $_POST['username']) . '\' AND `password` = ';
$query .= 'PASSWORD(\'' . mysqli_real_escape_string($mysql, $_POST['password']) . '\')';
$row = mysqli_fetch_assoc(mysqli_query($mysql, $query));
if ($row) {
$_SESSION['userid'] = $row['userid'];
$_SESSION['next_on_review'] = $row['next_on_review'] ? true : false;
$_SESSION['email'] = $row['email'];
$_SESSION['username'] = $row['username'];
$_SESSION['admin'] = $row['admin'] ? true : false;
$_SESSION['sadmin'] = $row['superadmin'] ? true : false;
$query = 'SELECT `userid`, `username`, `admin`, `superadmin`, `next_on_review`, `email` FROM `users` WHERE `username` = ';
$query .= '\'' . mysqli_real_escape_string($mysql, $username) . '\'';
return mysqli_fetch_assoc(mysqli_query($mysql, $query));
}

header('Location: ?page=List');
die();
} else {
private function createUser($username)
{
global $mysql;
$query = 'INSERT INTO `users` (`username`,`admin`) VALUES (';
$query .= '\'' . mysqli_real_escape_string($mysql, $username) . '\',';
$query .= '0)';
mysqli_query($mysql, $query);
}

public function __construct()
{
global $oauthConsumerKey, $oauthConsumerSecret;
$conf = new ClientConfig('https://en.wikipedia.org/w/index.php?title=Special:OAuth');
$conf->setConsumer(new Consumer($oauthConsumerKey, $oauthConsumerSecret));
$client = new Client($conf);

if (isset($_GET['oauth_verifier'])) {
// Callback URL - verify
$requestToken = new Token($_SESSION['request_key'], $_SESSION['request_secret']);
$accessToken = $client->complete($requestToken, $_GET['oauth_verifier']);
$identity = $client->identify($accessToken);

// We are done with these
unset($_SESSION['request_key']);
unset($_SESSION['request_secret']);

if (!$identity) {
header('Location: ?page=Sign+In');
die();
}

if ($identity->blocked) {
print('Access blocked');
die();
}

// This is a bit odd, but basically we lazy create users
$user = $this->lookupUser($identity->username);
if (!$user) {
$this->createUser($identity->username);
$user = $this->lookupUser($identity->username);
}

// If we managed to do the dance above, then we are logged in
if ($user) {
$_SESSION['userid'] = $user['userid'];
$_SESSION['next_on_review'] = $user['next_on_review'] ? true : false;
$_SESSION['username'] = $user['username'];
$_SESSION['admin'] = $user['admin'] ? true : false;
$_SESSION['sadmin'] = $user['superadmin'] ? true : false;

header('Location: ?page=List');
die();
}

// Else go through the process again
header('Location: ?page=Sign+In');
die();
} else {
// SignIn URl - redirect
list($authUrl, $token) = $client->initiate();
$_SESSION['request_key'] = $token->key;
$_SESSION['request_secret'] = $token->secret;

header('Location: ' . $authUrl);
die();
}
}

public function writeHeader()
{
echo 'Sign In';
}

public function writeContent()
{
echo '<form method="post">';
echo '<table>';
echo '<tr><th>Username:</th><td><input type="text" name="username" /></td></tr>';
echo '<tr><th>Password:</th><td><input type="password" name="password" /></td></tr>';
echo '<tr><td colspan=2><input type="submit" name="submit" value="Sign In" /></td></tr>';
echo '</table>';
echo '</form>';
}
}

if (!isset($_SESSION['username'])) {
Expand Down
2 changes: 1 addition & 1 deletion pages/SignOut.page.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SignOutPage extends Page
public function __construct()
{
session_destroy();
header('Location: ?page=Sign+In');
header('Location: ?page=List');
die();
}

Expand Down
24 changes: 4 additions & 20 deletions pages/View.page.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class ViewPage extends Page
private $row;
private $id;
private $data;
private $bad_captca;
private $bad_comment;

public function __construct()
{
Expand All @@ -22,25 +20,11 @@ public function __construct()
die();
}

if (isset($_POST['submit'])) {
if (isset($_POST['submit']) && isset($_SESSION['username'])) {
if (trim($_POST['comment']) != '') {
$this->bad_captca = false;
if (!isset($_SESSION['username'])) {
if (!recaptca_is_valid()) {
$this->bad_captca = true;
}
}

$this->bad_comment = false;
if (strpos($_POST['comment'], 'http://') !== false) {
$this->bad_comment = true;
}

if ($this->bad_captca === false && $this->bad_comment === false) {
createComment($this->id, $_POST['user'], $_POST['comment']);
header('Location: ?page=View&id=' . $this->id);
die();
}
createComment($this->id, $_SESSION['username'], $_POST['comment']);
header('Location: ?page=View&id=' . $this->id);
die();
}
}

Expand Down
Loading

0 comments on commit 397a2fc

Please sign in to comment.