Fixed chat login using Bearer token.

This commit is contained in:
flash 2025-02-02 02:34:51 +00:00
parent ab1bcaebc0
commit 6e0726fd3f
2 changed files with 14 additions and 40 deletions

View file

@ -308,7 +308,7 @@ final class OAuth2WebRoutes implements RouteHandler {
break; break;
} }
$scope[] = $scopeInfo->getSummary(); $scope[] = $scopeInfo->summary;
} }
} }
@ -392,7 +392,7 @@ final class OAuth2WebRoutes implements RouteHandler {
if(is_string($scopeInfo)) if(is_string($scopeInfo))
return ['error' => 'scope', 'scope' => $scopeName, 'reason' => $scopeInfo]; return ['error' => 'scope', 'scope' => $scopeName, 'reason' => $scopeInfo];
$scope[] = $scopeInfo->getSummary(); $scope[] = $scopeInfo->summary;
} }
$result = [ $result = [

View file

@ -2,16 +2,17 @@
namespace Misuzu\SharpChat; namespace Misuzu\SharpChat;
use RuntimeException; use RuntimeException;
use Misuzu\Auth\{AuthContext,AuthInfo,Sessions};
use Misuzu\Counters\CountersData;
use Misuzu\Emoticons\EmotesData;
use Misuzu\Perms\PermissionsData;
use Misuzu\Users\{BansData,UsersContext,UserInfo};
use Index\Colour\Colour; use Index\Colour\Colour;
use Index\Config\Config; use Index\Config\Config;
use Index\Http\{FormHttpContent,HttpRequest,HttpResponseBuilder}; use Index\Http\{FormHttpContent,HttpRequest,HttpResponseBuilder};
use Index\Http\Routing\{HandlerAttribute,HttpDelete,HttpGet,HttpOptions,HttpPost,RouteHandler,RouteHandlerCommon}; use Index\Http\Routing\{HandlerAttribute,HttpDelete,HttpGet,HttpOptions,HttpPost,RouteHandler,RouteHandlerCommon};
use Index\Urls\UrlRegistry; use Index\Urls\UrlRegistry;
use Misuzu\Auth\{AuthContext,AuthInfo,Sessions};
use Misuzu\Counters\CountersData;
use Misuzu\Emoticons\EmotesData;
use Misuzu\OAuth2\{OAuth2AccessInfoGetField,OAuth2Context};
use Misuzu\Perms\PermissionsData;
use Misuzu\Users\{BansData,UsersContext,UserInfo};
final class SharpChatRoutes implements RouteHandler { final class SharpChatRoutes implements RouteHandler {
use RouteHandlerCommon; use RouteHandlerCommon;
@ -24,6 +25,7 @@ final class SharpChatRoutes implements RouteHandler {
private UrlRegistry $urls, private UrlRegistry $urls,
private UsersContext $usersCtx, private UsersContext $usersCtx,
private AuthContext $authCtx, private AuthContext $authCtx,
private OAuth2Context $oauth2Ctx,
private EmotesData $emotes, private EmotesData $emotes,
private PermissionsData $perms, private PermissionsData $perms,
private AuthInfo $authInfo, private AuthInfo $authInfo,
@ -208,45 +210,17 @@ final class SharpChatRoutes implements RouteHandler {
return ['success' => false, 'reason' => 'hash']; return ['success' => false, 'reason' => 'hash'];
if(strcasecmp($authMethod, 'Bearer') === 0) { if(strcasecmp($authMethod, 'Bearer') === 0) {
$bearerCheck = $this->config->getString('bearerCheck');
if($bearerCheck === '')
return ['success' => false, 'reason' => 'unsupported'];
$req = curl_init($bearerCheck);
try { try {
curl_setopt_array($req, [ $accessInfo = $this->oauth2Ctx->tokens->getAccessInfo($authToken, OAuth2AccessInfoGetField::Token);
CURLOPT_AUTOREFERER => false, } catch(RuntimeException $ex) {
CURLOPT_FAILONERROR => false, return ['success' => false, 'reason' => 'token'];
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TCP_FASTOPEN => true,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_MAXREDIRS => 2,
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_TIMEOUT => 5,
CURLOPT_USERAGENT => 'Misuzu',
CURLOPT_HTTPHEADER => [
sprintf('Authorization: Bearer %s', $authToken),
],
]);
$response = curl_exec($req);
if($response === false)
return ['success' => false, 'reason' => 'request'];
} finally {
curl_close($req);
} }
$decoded = json_decode($response); if(empty($accessInfo->userId))
if($decoded === null)
return ['success' => false, 'reason' => 'decode'];
if(empty($decoded->user_id))
return ['success' => false, 'reason' => 'token']; return ['success' => false, 'reason' => 'token'];
try { try {
$userInfo = $this->usersCtx->users->getUser($decoded->user_id, 'id'); $userInfo = $this->usersCtx->users->getUser($accessInfo->userId, 'id');
} catch(RuntimeException $ex) { } catch(RuntimeException $ex) {
return ['success' => false, 'reason' => 'user']; return ['success' => false, 'reason' => 'user'];
} }