Merge pull request #13299 from annando/issue-13020

Issue 13020: Fix OAuth connection with Buffer
This commit is contained in:
Tobias Diekershoff 2023-07-24 06:38:02 +02:00 committed by GitHub
commit 98e50a49f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 5 deletions

View file

@ -91,7 +91,7 @@ class Authorize extends BaseApi
} }
if ($application['redirect_uri'] != 'urn:ietf:wg:oauth:2.0:oob') { if ($application['redirect_uri'] != 'urn:ietf:wg:oauth:2.0:oob') {
DI::app()->redirect($application['redirect_uri'] . (strpos($application['redirect_uri'], '?') ? '&' : '?') . http_build_query(['code' => $token['code'], 'state' => $request['state']])); DI::app()->redirect($request['redirect_uri'] . (strpos($request['redirect_uri'], '?') ? '&' : '?') . http_build_query(['code' => $token['code'], 'state' => $request['state']]));
} }
self::$oauth_code = $token['code']; self::$oauth_code = $token['code'];

View file

@ -30,6 +30,7 @@ use Friendica\Module\BaseApi;
use Friendica\Module\Special\HTTPException; use Friendica\Module\Special\HTTPException;
use Friendica\Security\OAuth; use Friendica\Security\OAuth;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
/** /**
@ -89,8 +90,11 @@ class Token extends BaseApi
$me = null; $me = null;
} elseif ($request['grant_type'] == 'authorization_code') { } elseif ($request['grant_type'] == 'authorization_code') {
// For security reasons only allow freshly created tokens // For security reasons only allow freshly created tokens
$condition = ["`redirect_uri` = ? AND `id` = ? AND `code` = ? AND `created_at` > ?", $uri = new Uri($request['redirect_uri']);
$request['redirect_uri'], $application['id'], $request['code'], DateTimeFormat::utc('now - 5 minutes')]; $condition = [
"`redirect_uri` LIKE ? AND `id` = ? AND `code` = ? AND `created_at` > ?",
'%' . $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '%', $application['id'], $request['code'], DateTimeFormat::utc('now - 5 minutes')
];
$token = DBA::selectFirst('application-view', ['access_token', 'created_at', 'uid'], $condition); $token = DBA::selectFirst('application-view', ['access_token', 'created_at', 'uid'], $condition);
if (!DBA::isResult($token)) { if (!DBA::isResult($token)) {

View file

@ -29,6 +29,7 @@ use Friendica\Model\Contact;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use GuzzleHttp\Psr7\Uri;
/** /**
* OAuth Server * OAuth Server
@ -128,8 +129,11 @@ class OAuth
if (!empty($client_secret)) { if (!empty($client_secret)) {
$condition['client_secret'] = $client_secret; $condition['client_secret'] = $client_secret;
} }
if (!empty($redirect_uri)) { if (!empty($redirect_uri)) {
$condition['redirect_uri'] = $redirect_uri; $uri = new Uri($redirect_uri);
$redirect_uri = $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath();
$condition = DBA::mergeConditions($condition, ["`redirect_uri` LIKE ?", '%' . $redirect_uri . '%']);
} }
$application = DBA::selectFirst('application', [], $condition); $application = DBA::selectFirst('application', [], $condition);
@ -137,6 +141,12 @@ class OAuth
Logger::warning('Application not found', $condition); Logger::warning('Application not found', $condition);
return []; return [];
} }
// The redirect_uri could contain several URI that are separated by spaces.
if (($application['redirect_uri'] != $redirect_uri) && !in_array($redirect_uri, explode(' ', $application['redirect_uri']))) {
return [];
}
return $application; return $application;
} }
@ -187,7 +197,8 @@ class OAuth
'write' => (stripos($scope, BaseApi::SCOPE_WRITE) !== false), 'write' => (stripos($scope, BaseApi::SCOPE_WRITE) !== false),
'follow' => (stripos($scope, BaseApi::SCOPE_FOLLOW) !== false), 'follow' => (stripos($scope, BaseApi::SCOPE_FOLLOW) !== false),
'push' => (stripos($scope, BaseApi::SCOPE_PUSH) !== false), 'push' => (stripos($scope, BaseApi::SCOPE_PUSH) !== false),
'created_at' => DateTimeFormat::utcNow()]; 'created_at' => DateTimeFormat::utcNow()
];
foreach ([BaseApi::SCOPE_READ, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_PUSH] as $scope) { foreach ([BaseApi::SCOPE_READ, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_PUSH] as $scope) {
if ($fields[$scope] && !$application[$scope]) { if ($fields[$scope] && !$application[$scope]) {