fix: Finish porting oauth2 usage in other parts of the codebase

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
pull/63288/head
Carl Schwan 5 days ago
parent 867f3f883f
commit 80f412e8eb
No known key found for this signature in database
GPG Key ID: 02325448204E452A
  1. 4
      apps/oauth2/lib/Db/AccessTokenMapper.php
  2. 22
      core/Controller/ClientFlowLoginController.php
  3. 16
      lib/private/Repair/Owncloud/MigrateOauthTables.php
  4. 9
      tests/Core/Controller/ClientFlowLoginControllerTest.php

@ -57,7 +57,7 @@ class AccessTokenMapper extends Repository {
$now = $timeFactory->now()->getTimestamp();
$maxTokenCreationTs = $now - OauthApiController::AUTHORIZATION_CODE_EXPIRES_AFTER;
$qb = $this->getDatabaseConnection()->getQueryBuilder();
$qb = $this->connection->getQueryBuilder();
$qb
->delete($this->getTableName())
->where($qb->expr()->eq('token_count', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
@ -72,7 +72,7 @@ class AccessTokenMapper extends Repository {
* @return int Number of updated rows
*/
public function rotateToken(int $id, string $oldCode, string $newCode, string $encryptedToken, bool $expectAuthorizationCodeState): int {
$qb = $this->getDatabaseConnection()->getQueryBuilder();
$qb = $this->connection->getQueryBuilder();
$qb
->update($this->getTableName())
->set('hashed_code', $qb->createNamedParameter(hash('sha512', $newCode)))

@ -104,7 +104,7 @@ class ClientFlowLoginController extends Controller {
$client = null;
if ($clientIdentifier !== '') {
$client = $this->clientMapper->getByIdentifier($clientIdentifier);
$clientName = $client->getName();
$clientName = $client->name;
}
// No valid clientIdentifier given and no valid API Request (APIRequest header not set)
@ -134,7 +134,7 @@ class ClientFlowLoginController extends Controller {
$csp = new ContentSecurityPolicy();
if ($client) {
$csp->addAllowedFormActionDomain($client->getRedirectUri());
$csp->addAllowedFormActionDomain($client->redirectUri);
} else {
$csp->addAllowedFormActionDomain('nc://*');
}
@ -191,12 +191,12 @@ class ClientFlowLoginController extends Controller {
$client = null;
if ($clientIdentifier !== '') {
$client = $this->clientMapper->getByIdentifier($clientIdentifier);
$clientName = $client->getName();
$clientName = $client->name;
}
$csp = new ContentSecurityPolicy();
if ($client) {
$csp->addAllowedFormActionDomain($client->getRedirectUri());
$csp->addAllowedFormActionDomain($client->redirectUri);
} else {
$csp->addAllowedFormActionDomain('nc://*');
}
@ -274,7 +274,7 @@ class ClientFlowLoginController extends Controller {
$client = false;
if ($clientIdentifier !== '') {
$client = $this->clientMapper->getByIdentifier($clientIdentifier);
$clientName = $client->getName();
$clientName = $client->name;
}
$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
@ -292,16 +292,16 @@ class ClientFlowLoginController extends Controller {
if ($client) {
$code = $this->random->generate(128, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
$accessToken = new AccessToken();
$accessToken->setClientId($client->getId());
$accessToken->setEncryptedToken($this->crypto->encrypt($token, $code));
$accessToken->setHashedCode(hash('sha512', $code));
$accessToken->setTokenId($generatedToken->getId());
$accessToken->setCodeCreatedAt($this->timeFactory->now()->getTimestamp());
$accessToken->clientId = $client->id;
$accessToken->encryptedToken = $this->crypto->encrypt($token, $code);
$accessToken->hashedCode = hash('sha512', $code);
$accessToken->tokenId = $generatedToken->getId();
$accessToken->codeCreatedAt = $this->timeFactory->now()->getTimestamp();
$this->accessTokenMapper->insert($accessToken);
$enableOcClients = $this->config->getSystemValueBool('oauth2.enable_oc_clients', false);
$redirectUri = $client->getRedirectUri();
$redirectUri = $client->redirectUri;
if ($enableOcClients && $redirectUri === 'http://localhost:*') {
// Sanity check untrusted redirect URI provided by the client first
if (!preg_match('/^http:\/\/localhost:[0-9]+$/', $providedRedirectUri)) {

@ -221,8 +221,8 @@ class MigrateOauthTables implements IRepairStep {
$now = $this->timeFactory->now()->getTimestamp();
$index = 0;
while ($row = $result->fetchAssociative()) {
$clientId = $row['client_id'];
$refreshToken = $row['token'];
$clientId = (int)$row['client_id'];
$refreshToken = (string)$row['token'];
// Insert expired token so that it can be rotated on the next refresh
$accessToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
@ -239,12 +239,12 @@ class MigrateOauthTables implements IRepairStep {
$this->tokenProvider->updateToken($authToken);
$accessTokenEntity = new AccessToken();
$accessTokenEntity->setTokenId($authToken->getId());
$accessTokenEntity->setClientId($clientId);
$accessTokenEntity->setHashedCode(hash('sha512', $refreshToken));
$accessTokenEntity->setEncryptedToken($this->crypto->encrypt($accessToken, $refreshToken));
$accessTokenEntity->setCodeCreatedAt($now);
$accessTokenEntity->setTokenCount(1);
$accessTokenEntity->tokenId = $authToken->getId();
$accessTokenEntity->clientId = $clientId;
$accessTokenEntity->hashedCode = hash('sha512', $refreshToken);
$accessTokenEntity->encryptedToken = $this->crypto->encrypt($accessToken, $refreshToken);
$accessTokenEntity->codeCreatedAt = $now;
$accessTokenEntity->tokenCount = 1;
$this->accessTokenMapper->insert($accessTokenEntity);
$index++;

@ -202,8 +202,8 @@ class ClientFlowLoginControllerTest extends TestCase {
['OCS-APIREQUEST', 'false'],
]);
$client = new Client();
$client->setName('My external service');
$client->setRedirectUri('https://example.com/redirect.php');
$client->name = 'My external service';
$client->redirectUri = 'https://example.com/redirect.php';
$this->clientMapper
->expects($this->once())
->method('getByIdentifier')
@ -491,8 +491,9 @@ class ClientFlowLoginControllerTest extends TestCase {
)
->willReturn($token);
$client = new Client();
$client->setName('My OAuth client');
$client->setRedirectUri($redirectUri);
$client->id = 42;
$client->name = 'My OAuth client';
$client->redirectUri = $redirectUri;
$this->clientMapper
->expects($this->once())
->method('getByIdentifier')

Loading…
Cancel
Save