-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Make OAuth2 authorization code expire #40766
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
Changes from 1 commit
807f173
2995b09
7bba410
1ab45ba
779e1d5
ddfc124
e944980
c6da994
32f984c
d2bc483
da63d3c
98c8a46
d56950a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…have expired Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2023 Julien Veyssier <julien-nc@posteo.net> | ||
| * | ||
| * @author Julien Veyssier <julien-nc@posteo.net> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as | ||
| * published by the Free Software Foundation, either version 3 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
|
|
||
| namespace OCA\OAuth2\BackgroundJob; | ||
|
|
||
| use OCA\OAuth2\Db\AccessTokenMapper; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\IJob; | ||
| use OCP\BackgroundJob\TimedJob; | ||
| use OCP\DB\Exception; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class CleanupExpiredAuthorizationCode extends TimedJob { | ||
|
|
||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| private AccessTokenMapper $accessTokenMapper, | ||
| private LoggerInterface $logger, | ||
|
|
||
| ) { | ||
| parent::__construct($timeFactory); | ||
| // 30 days | ||
| $this->setInterval(60 * 60 * 24 * 30); | ||
| $this->setTimeSensitivity(IJob::TIME_INSENSITIVE); | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed $argument | ||
| * @inheritDoc | ||
| */ | ||
| protected function run($argument) { | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| try { | ||
| $this->accessTokenMapper->cleanupExpiredAuthorizationCode(); | ||
| } catch (Exception $e) { | ||
| $this->logger->warning('Failed to cleanup tokens with expired authorization code', ['exception' => $e]); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,13 +27,16 @@ | |||||
|
|
||||||
| use Closure; | ||||||
| use OCP\DB\ISchemaWrapper; | ||||||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||||||
| use OCP\DB\Types; | ||||||
| use OCP\IDBConnection; | ||||||
| use OCP\Migration\IOutput; | ||||||
| use OCP\Migration\SimpleMigrationStep; | ||||||
|
|
||||||
| class Version011603Date20230620111039 extends SimpleMigrationStep { | ||||||
|
|
||||||
| public function __construct( | ||||||
| private IDBConnection $connection, | ||||||
| ) { | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -43,15 +46,36 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt | |||||
|
|
||||||
| if ($schema->hasTable('oauth2_access_tokens')) { | ||||||
| $table = $schema->getTable('oauth2_access_tokens'); | ||||||
| $dbChanged = false; | ||||||
| if (!$table->hasColumn('created_at') || !$table->hasColumn('token_count')) { | ||||||
| $dbChanged = true; | ||||||
| } | ||||||
| if (!$table->hasColumn('created_at')) { | ||||||
| $table->addColumn('created_at', Types::BIGINT, [ | ||||||
| 'notnull' => true, | ||||||
| 'default' => 0, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Default 0 is automatically, unsigned will allow more numbers
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not explicitly setting the default to 0? I didn't know about it, maybe others don't know either.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also fine 👍🏼 |
||||||
| ]); | ||||||
| } | ||||||
| if (!$table->hasColumn('token_count')) { | ||||||
| $table->addColumn('token_count', Types::BIGINT, [ | ||||||
| 'notnull' => true, | ||||||
| 'default' => 0, | ||||||
julien-nc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| ]); | ||||||
| } | ||||||
| if ($dbChanged) { | ||||||
| return $schema; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { | ||||||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||||||
| // we consider that existing access_tokens have already produced at least one oauth token | ||||||
| // which prevents cleaning them up | ||||||
| $qbUpdate = $this->connection->getQueryBuilder(); | ||||||
| $qbUpdate->update('oauth2_access_tokens') | ||||||
| ->set('token_count', $qbUpdate->createNamedParameter(1, IQueryBuilder::PARAM_INT)); | ||||||
| $qbUpdate->executeStatement(); | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.