3535from flask_sqlalchemy import SQLAlchemy
3636from flask_migrate import Migrate
3737from flask_marshmallow import Marshmallow
38- from flask_oidc import OpenIDConnect
3938from flask_smorest import Api
40-
41- log = logging .getLogger (__name__ )
39+ from authlib .integrations .flask_client import OAuth
4240
4341#
4442# Establish consistent logging
5149except importlib .metadata .PackageNotFoundError :
5250 # You have not yet installed this as a package, likely because you're hacking on it in some IDE
5351 __version__ = '0.0.0.dev0'
52+ __app_version_header__ = 'X-MrMat-Python-API-Flask-Version'
5453
5554#
5655# Initialize supporting services
5756
5857db = SQLAlchemy ()
5958ma = Marshmallow ()
6059migrate = Migrate ()
61- oidc = OpenIDConnect ()
6260api = Api ()
61+ oauth = OAuth ()
6362
6463
6564def create_app (config_override = None , instance_path = None ):
@@ -93,55 +92,59 @@ def create_app(config_override=None, instance_path=None):
9392 app .config .setdefault ('OPENAPI_JSON_PATH' , '/openapi.json' )
9493 app .config .setdefault ('OPENAPI_SWAGGER_UI_PATH' , '/swagger-ui' )
9594 app .config .setdefault ('OPENAPI_SWAGGER_UI_URL' , 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.5.0/' )
96- app_config_file = os .path . expanduser ( os . environ .get ('APP_CONFIG' , '~/etc/mrmat-python-api-flask.json' ))
95+ app_config_file = os .environ .get ('APP_CONFIG' , os . path . expanduser ( '~/etc/mrmat-python-api-flask.json' ))
9796 if os .path .exists (app_config_file ):
98- log .info ('Applying configuration from %s' , app_config_file )
97+ app . logger .info ('Applying configuration from %s' , app_config_file )
9998 with open (app_config_file , 'r' , encoding = 'UTF-8' ) as c :
10099 config = json .load (c )
101100 app .config .from_object (config )
102- #app.config.from_json(app_config_file)
103101 if config_override is not None :
104102 for override in config_override :
105- log .info ('Overriding configuration for %s from the command line' , override )
103+ app . logger .info ('Overriding configuration for %s from the command line' , override )
106104 app .config .from_mapping (config_override )
107105 if app .config ['SECRET_KEY' ] is None :
108- log .warning ('Generating new secret key' )
106+ app . logger .warning ('Generating new secret key' )
109107 app .config ['SECRET_KEY' ] = secrets .token_urlsafe (16 )
110108
111109 #
112110 # Create the instance folder if it does not exist
113111
114112 try :
115113 if not os .path .exists (app .instance_path ):
116- log .info ('Creating new instance path at %s' , app .instance_path )
114+ app . logger .info ('Creating new instance path at %s' , app .instance_path )
117115 os .makedirs (app .instance_path )
118116 else :
119- log .info ('Using existing instance path at %s' , app .instance_path )
117+ app . logger .info ('Using existing instance path at %s' , app .instance_path )
120118 except OSError :
121- log .error ('Failed to create new instance path at %s' , app .instance_path )
119+ app . logger .error ('Failed to create new instance path at %s' , app .instance_path )
122120 sys .exit (1 )
123121
124- # When using Flask-SQLAlchemy, there is no need to explicitly import DAO classes because they themselves
125- # inherit from the SQLAlchemy model
122+ # There is no need to explicitly load DAO classes here because they inherit from the SQLAlchemy model
126123
127124 db .init_app (app )
128125 migrate .init_app (app , db )
129126 ma .init_app (app )
130127 api .init_app (app )
131- if 'OIDC_CLIENT_SECRETS' in app .config .keys ():
132- oidc .init_app (app )
133- else :
134- log .warning ('Running without any authentication/authorisation' )
128+ oauth .init_app (app )
129+ oauth .register (name = 'mrmat' ,
130+ server_metadata_url = 'http://localhost:5001/.well-known/openid-configuration' ,
131+ client_kwargs = {'scope' :'openid email profile' })
132+ # if 'OIDC_CLIENT_SECRETS' in app.config.keys():
133+ # oauth.init_app(app)
134+ # oauth.register(name='mrmat',
135+ # server_metadata_url='http://localhost:5001/.well-known/openid-configuration',
136+ # client_kwargs={'scope':'openid email profile'})
137+ # else:
138+ # app.logger.warning('Running without any authentication/authorisation')
135139
136140 #
137- # Security Schemes
141+ # Set Security Schemes in the generated OpenAPI descriptor
138142
139- api .spec .components .security_scheme ('openId' , dict (
140- type = 'openIdConnect' ,
141- description = 'MrMat OIDC' ,
142- openIdConnectUrl = 'http://localhost:8080/auth/realms/master'
143- '/.well-known/openid-configuration'
144- ))
143+ # api.spec.components.security_scheme('openId',
144+ # {'type': 'openIdConnect',
145+ # 'description': 'MrMat OIDC',
146+ # 'openIdConnectUrl': 'http://localhost:8080/auth/realms/master/.well-known'
147+ # '/openid-configuration'})
145148
146149 #
147150 # Import and register our APIs here
@@ -163,8 +166,8 @@ def create_app(config_override=None, instance_path=None):
163166
164167 @app .after_request
165168 def after_request (response : flask .Response ) -> flask .Response :
166- log .info ('[%s]' , response .status_code )
167- response .headers .add ('X-MrMat-Python-API-Flask-Version' , __version__ )
169+ app . logger .info ('[%s]' , response .status_code )
170+ response .headers .add (__app_version_header__ , __version__ )
168171 return response
169172
170173 return app
0 commit comments