From 887553dd5de423f2f18d7ffec8e807d3ef13a6fd Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 27 May 2022 14:18:50 +0200 Subject: [PATCH] API - add refresh token --- fittrackee/config.py | 1 + fittrackee/oauth2/client.py | 2 +- fittrackee/oauth2/config.py | 3 ++- fittrackee/tests/oauth2/test_oauth2_client.py | 5 ++++- fittrackee/tests/oauth2/test_oauth2_routes.py | 3 ++- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/fittrackee/config.py b/fittrackee/config.py index 8f5bcda6..bf95725a 100644 --- a/fittrackee/config.py +++ b/fittrackee/config.py @@ -65,6 +65,7 @@ class TestingConfig(BaseConfig): TOKEN_EXPIRATION_DAYS = 0 TOKEN_EXPIRATION_SECONDS = 3 PASSWORD_TOKEN_EXPIRATION_SECONDS = 3 + OAUTH2_REFRESH_TOKEN_GENERATOR = True UI_URL = 'http://0.0.0.0:5000' SENDER_EMAIL = 'fittrackee@example.com' diff --git a/fittrackee/oauth2/client.py b/fittrackee/oauth2/client.py index e07e6639..93094fe5 100644 --- a/fittrackee/oauth2/client.py +++ b/fittrackee/oauth2/client.py @@ -19,7 +19,7 @@ def create_oauth_client(metadata: Dict, user: User) -> OAuth2Client: 'client_uri': metadata['client_uri'], 'redirect_uris': metadata['redirect_uris'], 'scope': metadata['scope'], - 'grant_types': ['authorization_code'], + 'grant_types': ['authorization_code', 'refresh_token'], 'response_types': ['code'], 'token_endpoint_auth_method': 'client_secret_post', } diff --git a/fittrackee/oauth2/config.py b/fittrackee/oauth2/config.py index 8f7bb4ea..0eb17145 100644 --- a/fittrackee/oauth2/config.py +++ b/fittrackee/oauth2/config.py @@ -1,7 +1,7 @@ from authlib.oauth2.rfc7636 import CodeChallenge from flask import Flask -from .grants import AuthorizationCodeGrant +from .grants import AuthorizationCodeGrant, RefreshTokenGrant from .server import authorization_server @@ -12,3 +12,4 @@ def config_oauth(app: Flask) -> None: authorization_server.register_grant( AuthorizationCodeGrant, [CodeChallenge(required=True)] ) + authorization_server.register_grant(RefreshTokenGrant) diff --git a/fittrackee/tests/oauth2/test_oauth2_client.py b/fittrackee/tests/oauth2/test_oauth2_client.py index b8638f16..9628fb26 100644 --- a/fittrackee/tests/oauth2/test_oauth2_client.py +++ b/fittrackee/tests/oauth2/test_oauth2_client.py @@ -72,7 +72,10 @@ class TestCreateOAuth2Client: ) -> None: oauth_client = create_oauth_client(TEST_METADATA, user_1) - assert oauth_client.grant_types == ['authorization_code'] + assert oauth_client.grant_types == [ + 'authorization_code', + 'refresh_token', + ] def test_oauth_client_has_expected_redirect_uris( self, app: Flask, user_1: User diff --git a/fittrackee/tests/oauth2/test_oauth2_routes.py b/fittrackee/tests/oauth2/test_oauth2_routes.py index 5fdabf8a..05f36a7e 100644 --- a/fittrackee/tests/oauth2/test_oauth2_routes.py +++ b/fittrackee/tests/oauth2/test_oauth2_routes.py @@ -153,7 +153,7 @@ class TestOAuthClientCreation(OAuth2TestCase): @pytest.mark.parametrize( 'input_key,expected_value', [ - ('grant_types', ['authorization_code']), + ('grant_types', ['authorization_code', 'refresh_token']), ('response_types', ['code']), ('token_endpoint_auth_method', 'client_secret_post'), ], @@ -271,6 +271,7 @@ class TestOAuthIssueToken(OAuth2TestCase): data = json.loads(response.data.decode()) assert data.get('access_token') is not None assert data.get('expires_in') is not None + assert data.get('refresh_token') is not None assert data.get('token_type') == 'Bearer' @staticmethod