diff options
author | Julien Veyssier <julien-nc@posteo.net> | 2023-06-20 11:54:43 +0200 |
---|---|---|
committer | Julien Veyssier <julien-nc@posteo.net> | 2023-10-05 14:24:02 +0200 |
commit | 807f173dec7288945fca98548e80e43d3e401d12 (patch) | |
tree | 72918c69010f20e6f70b2dd0215bc8bea051cc9f /apps/oauth2/lib/Controller | |
parent | f3f2d9b9784ef3a9304543969a0a88cd1f1054d8 (diff) | |
download | nextcloud-server-807f173dec7288945fca98548e80e43d3e401d12.tar.gz nextcloud-server-807f173dec7288945fca98548e80e43d3e401d12.zip |
make oauth2 authorization code expire after 10 minutes
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
Diffstat (limited to 'apps/oauth2/lib/Controller')
-rw-r--r-- | apps/oauth2/lib/Controller/OauthApiController.php | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/apps/oauth2/lib/Controller/OauthApiController.php b/apps/oauth2/lib/Controller/OauthApiController.php index af1205be0d7..443db314f2a 100644 --- a/apps/oauth2/lib/Controller/OauthApiController.php +++ b/apps/oauth2/lib/Controller/OauthApiController.php @@ -39,6 +39,7 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\DB\Exception; use OCP\IRequest; use OCP\Security\Bruteforce\IThrottler; use OCP\Security\ICrypto; @@ -46,6 +47,8 @@ use OCP\Security\ISecureRandom; use Psr\Log\LoggerInterface; class OauthApiController extends Controller { + // the authorization code expires after 10 minutes + private const AUTHORIZATION_CODE_EXPIRES_AFTER = 10 * 60; public function __construct( string $appName, @@ -57,7 +60,8 @@ class OauthApiController extends Controller { private ISecureRandom $secureRandom, private ITimeFactory $time, private LoggerInterface $logger, - private IThrottler $throttler + private IThrottler $throttler, + private ITimeFactory $timeFactory, ) { parent::__construct($appName, $request); } @@ -70,16 +74,20 @@ class OauthApiController extends Controller { * Get a token * * @param string $grant_type Token type that should be granted - * @param string $code Code of the flow - * @param string $refresh_token Refresh token - * @param string $client_id Client ID - * @param string $client_secret Client secret + * @param string|null $code Code of the flow + * @param string|null $refresh_token Refresh token + * @param string|null $client_id Client ID + * @param string|null $client_secret Client secret + * @throws Exception * @return JSONResponse<Http::STATUS_OK, array{access_token: string, token_type: string, expires_in: int, refresh_token: string, user_id: string}, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, array{error: string}, array{}> * * 200: Token returned * 400: Getting token is not possible */ - public function getToken($grant_type, $code, $refresh_token, $client_id, $client_secret): JSONResponse { + public function getToken( + string $grant_type, ?string $code, ?string $refresh_token, + ?string $client_id, ?string $client_secret + ): JSONResponse { // We only handle two types if ($grant_type !== 'authorization_code' && $grant_type !== 'refresh_token') { @@ -105,6 +113,20 @@ class OauthApiController extends Controller { return $response; } + // check authorization code expiration + if ($grant_type === 'authorization_code') { + $now = $this->timeFactory->now()->getTimestamp(); + $tokenCreatedAt = $accessToken->getCreatedAt(); + if ($tokenCreatedAt < $now - self::AUTHORIZATION_CODE_EXPIRES_AFTER) { + $response = new JSONResponse([ + 'error' => 'invalid_request', + ], Http::STATUS_BAD_REQUEST); + $expiredSince = $now - self::AUTHORIZATION_CODE_EXPIRES_AFTER - $tokenCreatedAt; + $response->throttle(['invalid_request' => 'authorization_code_expired', 'expired_since' => $expiredSince]); + return $response; + } + } + try { $client = $this->clientMapper->getByUid($accessToken->getClientId()); } catch (ClientNotFoundException $e) { |