Refine OAuth flow

This commit is contained in:
Michael 2021-05-13 11:26:56 +00:00
parent 78a85df86b
commit 7975bc244d
8 changed files with 134 additions and 32 deletions

View file

@ -25,6 +25,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Util\Network;
/**
* Apps class to register new OAuth clients
@ -37,6 +38,17 @@ class Apps extends BaseApi
*/
public static function post(array $parameters = [])
{
// Workaround for AndStatus, see issue https://github.com/andstatus/andstatus/issues/538
if (empty($_REQUEST['client_name']) || empty($_REQUEST['redirect_uris'])) {
$postdata = Network::postdata();
if (!empty($postdata)) {
$_REQUEST = json_decode($postdata, true);
if (empty($_REQUEST)) {
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters'));
}
}
}
$name = $_REQUEST['client_name'] ?? '';
$redirect = $_REQUEST['redirect_uris'] ?? '';
$scopes = $_REQUEST['scopes'] ?? '';

View file

@ -21,6 +21,7 @@
namespace Friendica\Module;
use Exception;
use Friendica\BaseModule;
use Friendica\Core\Logger;
use Friendica\Core\System;
@ -206,19 +207,13 @@ class BaseApi extends BaseModule
/**
* Get the application record via the proved request header fields
*
* @param string $client_id
* @param string $client_secret
* @param string $redirect_uri
* @return array application record
*/
public static function getApplication()
public static function getApplication(string $client_id, string $client_secret, string $redirect_uri)
{
$redirect_uri = $_REQUEST['redirect_uri'] ?? '';
$client_id = $_REQUEST['client_id'] ?? '';
$client_secret = $_REQUEST['client_secret'] ?? '';
if ((empty($redirect_uri) && empty($client_secret)) || empty($client_id)) {
Logger::warning('Incomplete request', ['request' => $_REQUEST]);
return [];
}
$condition = ['client_id' => $client_id];
if (!empty($client_secret)) {
$condition['client_secret'] = $client_secret;
@ -262,15 +257,18 @@ class BaseApi extends BaseModule
/**
* Create and fetch an token for the application and user
*
* @param array $application
* @param array $application
* @param integer $uid
* @param string $scope
* @return array application record
*/
public static function createTokenForUser(array $application, int $uid)
public static function createTokenForUser(array $application, int $uid, string $scope)
{
$code = bin2hex(random_bytes(32));
$access_token = bin2hex(random_bytes(32));
// @todo store the scope
$fields = ['application-id' => $application['id'], 'uid' => $uid, 'code' => $code, 'access_token' => $access_token, 'created_at' => DateTimeFormat::utcNow(DateTimeFormat::MYSQL)];
if (!DBA::insert('application-token', $fields, Database::INSERT_UPDATE)) {
return [];

View file

@ -27,6 +27,7 @@ use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/spec/oauth/
* @see https://aaronparecki.com/oauth-2-simplified/
*/
class Authorize extends BaseApi
{
@ -37,16 +38,29 @@ class Authorize extends BaseApi
public static function rawContent(array $parameters = [])
{
$response_type = $_REQUEST['response_type'] ?? '';
$client_id = $_REQUEST['client_id'] ?? '';
$client_secret = $_REQUEST['client_secret'] ?? ''; // Isn't normally provided. We will use it if present.
$redirect_uri = $_REQUEST['redirect_uri'] ?? '';
$scope = $_REQUEST['scope'] ?? '';
$state = $_REQUEST['state'] ?? '';
if ($response_type != 'code') {
Logger::warning('Wrong or missing response type', ['response_type' => $response_type]);
DI::mstdnError()->UnprocessableEntity();
Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]);
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing response type'));
}
$application = self::getApplication();
if (empty($client_id) || empty($redirect_uri)) {
Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data'));
}
$application = self::getApplication($client_id, $client_secret, $redirect_uri);
if (empty($application)) {
DI::mstdnError()->UnprocessableEntity();
}
// @todo Compare the application scope and requested scope
$request = $_REQUEST;
unset($request['pagename']);
$redirect = 'oauth/authorize?' . http_build_query($request);
@ -66,11 +80,11 @@ class Authorize extends BaseApi
DI::session()->remove('oauth_acknowledge');
$token = self::createTokenForUser($application, $uid);
$token = self::createTokenForUser($application, $uid, $scope);
if (!$token) {
DI::mstdnError()->UnprocessableEntity();
}
DI::app()->redirect($application['redirect_uri'] . '?code=' . $token['code']);
DI::app()->redirect($application['redirect_uri'] . '?' . http_build_query(['code' => $token['code'], 'state' => $state]));
}
}

View file

@ -29,39 +29,44 @@ use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/spec/oauth/
* @see https://aaronparecki.com/oauth-2-simplified/
*/
class Token extends BaseApi
{
public static function post(array $parameters = [])
{
$client_secret = $_REQUEST['client_secret'] ?? '';
$code = $_REQUEST['code'] ?? '';
$grant_type = $_REQUEST['grant_type'] ?? '';
$code = $_REQUEST['code'] ?? '';
$redirect_uri = $_REQUEST['redirect_uri'] ?? '';
$client_id = $_REQUEST['client_id'] ?? '';
$client_secret = $_REQUEST['client_secret'] ?? '';
if ($grant_type != 'authorization_code') {
Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]);
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing grant type'));
}
$application = self::getApplication();
if (empty($client_id) || empty($client_secret) || empty($redirect_uri)) {
Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data'));
}
$application = self::getApplication($client_id, $client_secret, $redirect_uri);
if (empty($application)) {
DI::mstdnError()->UnprocessableEntity();
}
if ($application['client_secret'] != $client_secret) {
Logger::warning('Wrong client secret', $client_secret);
DI::mstdnError()->Unauthorized();
}
$condition = ['application-id' => $application['id'], 'code' => $code];
// For security reasons only allow freshly created tokens
$condition = ["`application-id` = ? AND `code` = ? AND `created_at` > UTC_TIMESTAMP() - INTERVAL ? MINUTE", $application['id'], $code, 5];
$token = DBA::selectFirst('application-token', ['access_token', 'created_at'], $condition);
if (!DBA::isResult($token)) {
Logger::warning('Token not found', $condition);
Logger::warning('Token not found or outdated', $condition);
DI::mstdnError()->Unauthorized();
}
// @todo Use entity class
System::jsonExit(['access_token' => $token['access_token'], 'token_type' => 'Bearer', 'scope' => $application['scopes'], 'created_at' => $token['created_at']]);
$object = new \Friendica\Object\Api\Mastodon\Token($token['access_token'], 'Bearer', $application['scopes'], $token['created_at']);
System::jsonExit($object->toArray());
}
}