Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using traditional_auth with API keys #1045

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
15 changes: 13 additions & 2 deletions src/Classes/ServiceAPI/MyRadio_APIKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace MyRadio\ServiceAPI;

use MyRadio\Iface\APICaller;
use MyRadio\MyRadioException;

/**
* The APIKey Class provies information and management of API Keys for the MyRadio
Expand Down Expand Up @@ -39,8 +40,6 @@ class MyRadio_APIKey extends ServiceAPI implements APICaller
protected function __construct($key)
{
$this->key = $key;
$revoked = self::$db->fetchColumn('SELECT revoked from myury.api_key WHERE key_string=$1', [$key]);
$this->revoked = ($revoked[0] == 't');
$this->permissions = array_map(
'intval',
self::$db->fetchColumn(
Expand All @@ -66,4 +65,16 @@ public function isRevoked()
{
return $this->revoked;
}

public static function factory($key)
{
$apiKey = new static($key);
$revoked = self::$db->fetchColumn('SELECT revoked from myury.api_key WHERE key_string=$1', [$key]);
if (count($revoked) === 0)
{
return null;
}
$apiKey->revoked = ($revoked[0] == 't');
return $apiKey;
}
}
17 changes: 12 additions & 5 deletions src/Controllers/traditional_auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@
//Load the basic MyRadio framework

use \MyRadio\MyRadio\URLUtils;
use MyRadio\Config;
use MyRadio\ServiceAPI\MyRadio_APIKey;
use MyRadio\ServiceAPI\MyRadio_Swagger2;

require_once __DIR__.'/root_cli.php';

if (defined('SHIBBOBLEH_ALLOW_API') && SHIBBOBLEH_ALLOW_API === true &&
(isset($_REQUEST['api_key']) || isset($_REQUEST['apiKey']))) {
$caller = MyRadio_Swagger2::getAPICaller();
$authed = $caller instanceof MyRadio_APIKey && !$caller->isRevoked();
} else {
$authed = isset($_SESSION['memberid']) && !$_SESSION['auth_use_locked'];
}

//Check the current authentication status of the user
if ((!isset($_SESSION['memberid']) or $_SESSION['auth_use_locked'])
&& (!defined('SHIBBOBLEH_ALLOW_READONLY') or SHIBBOBLEH_ALLOW_READONLY === false)
) {
if (!$authed && (!defined('SHIBBOBLEH_ALLOW_READONLY') or SHIBBOBLEH_ALLOW_READONLY === false)) {
//Authentication is required.
header('HTTP/1.1 403 Forbidden');
URLUtils::redirect('MyRadio', 'login', ['next' => $_SERVER['REQUEST_URI']]);
exit;
}
Expand All @@ -25,7 +33,6 @@
&& (defined('SHIBBOBLEH_REQUIRE_TIMESLOT') and SHIBBOBLEH_REQUIRE_TIMESLOT)
) {
//Timeslot needs configuring
header('HTTP/1.1 403 Forbidden');
URLUtils::redirect('MyRadio', 'timeslot', ['next' => $_SERVER['REQUEST_URI']]);
exit;
}