Coverage for seedboxsync/front/oauth2.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-26 17:14 +0000

1# 

2# Copyright (C) 2015-2026 Guillaume Kulakowski <guillaume@kulakowski.fr> 

3# 

4# For the full copyright and license information, please view the LICENSE 

5# file that was distributed with this source code. 

6# 

7"""SeedboxSync front OAuth module.""" 

8 

9from authlib.integrations.flask_client import OAuth 

10from seedboxsync.core import Flask 

11 

12# Global OAuth client instance 

13oauth = OAuth() 

14 

15 

16def init_oauth2(app: Flask) -> None: 

17 """ 

18 Initialize OAuth and register configured authentication providers with Flask. 

19 

20 Checks application configuration for enabled OAuth/OIDC settings and registers 

21 the external OpenID Connect identity provider using Authlib integration. 

22 

23 Args: 

24 app (Flask): The target Flask application instance. 

25 """ 

26 oauth._registry.clear() 

27 oauth._clients.clear() 

28 

29 # Verify if OAuth provider authentication is enabled in application configuration 

30 if app.seedboxsync_config.get("oauth_enabled"): 

31 # Bind Authlib extension to Flask app lifecycle 

32 oauth.init_app(app) 

33 

34 # Register remote OIDC provider with configured OAuth client credentials 

35 oauth.register( 

36 name=app.seedboxsync_config.get("oauth_name"), 

37 client_id=app.seedboxsync_config.get("oauth_client_id"), 

38 client_secret=app.seedboxsync_config.get("oauth_client_secret"), 

39 server_metadata_url=app.seedboxsync_config.get("oauth_server_metadata_url"), 

40 client_kwargs={ 

41 "scope": "openid profile email", 

42 "code_challenge_method": "S256", 

43 "token_endpoint_auth_method": "client_secret_post", 

44 }, 

45 ) 

46 app.logger.debug( 

47 "OAuth provider '%s' (%s) registered successfully with client_id '%s'.", 

48 app.seedboxsync_config.get("oauth_name"), 

49 app.seedboxsync_config.get("oauth_server_metadata_url"), 

50 app.seedboxsync_config.get("oauth_client_id"), 

51 )