Package: busie-flask-helper
Busie Flask HelperA package to help with common initialization code across Busie Flask projectsHelpful for use with the application factory pattern.
Allows for modularization of initialization code, such that factories are not cluttered with boilerplate initializationInstallationpip install busie-flask-helperUsage# in app.py, or wherever application initialization livesfrombusie_flaskimportFlaskHelperhelper=FlaskHelper('abs/path/to/project/')db=helper.dbauth=helper.authcache=helper.cache# Other Initialization Code omitteddefcreate_app():app=Flask(__name__)# Other App Factory initialization omitted# this invocation initializes all of the helper objects with the application# this includes migration commands, models, views, auth, cache client, ormhelper.init_app(app,'relative/path/to/models','/relative/path/to/views')returnapp# in some module, maybe an API Viewfromflaskimportrequest,render_templatefromsrc.appimportauth,app@app.route('/foo')@auth.requires_web_authdeffoo():returnrender_template('foo.html')classSomeView(MethodView):decorators=[auth.require_api_auth]defget(self):ifauth.requires_permission('SomeView:bar'):return'ok',200raiseAuthError({'code':'some_error','description':'something went wrong'},403)APIFlaskHelperPublic Propertiesdb<[SQLAlchemy] (https://flask-sqlalchemy.palletsprojects.com/en/2.x/)>auth<[AuthHelper] (#authhelper)>cache<[CacheClient] (#cacheclient)>Public MethodsFlaskHelper(root_dir, app=None, models_dir=None, views_dir=None)paramroot_dir: The root directory of the project, best practice to pass an abspath here. if your application lives in src/app then your root dir should be src/Seeinit_appmethod documentation for specifics on the other paramsinit_app(self, app, models_dir, views_dir)paramapp: A Flask application instanceparammodels_dir: Relative path to the models directory, with respect to the root_dir that the helper was initialized withparamviews_dir: same asmodels_dir, but for viewsAuthHelperThis is an Auth0 Integration. It comes with helpful decorators for protecting both API and Web App views. It also automatically adds/login,/callback, and/logoutroutes to your application for Auth0 Authorization Code Flow Authentication. Note that this feature cannot be disabled at this time, but this ability will be added in a future release. In web applications and API's, merely authenticating shouldneverbe enough to gain access to a protected resource.Public PropertiesNonePublic MethodsAuthHelper(app=None): Constructorinit_app(self, app): Initialize the AuthHelper with a Flask applicationparamapp: A Flask application instance. app.configmusthaveAUTH0_AUDIENCEAUTH0_DOMAINandAUTH0_ALGORITHMSrequires_api_auth(self, f): A decorator for views that require the auth code with pkce flow.requires_web_auth(self, f): A decorator for views that require the auth code flow (no pkce)get_rsa_key_from_unverified_token(self, token): Determines the RSA key to use from the token'skidparamtoken: A json web token with appropriate headersreturn:rsa_key (dict)orNonehandle_rsa_decode(self, rsa_key, token): decodes the token using rsaparamrsa_key: dict representation of an RSA keyparamtoken: a json web token with necessary claimsrequires_scope(self, required_scope): determine if the auth token has the provided required scopeparamrequired_scope: string, the desired scope for the resourcereturn:boolrequires_permission(self, required_permission): determine if the auth token has the provided required permissionparamrequired_permission: string, the desired permission for the resourcereturn:boolstatic -> get_token_auth_header(): gets the auth header from the request context and formats it properlyreturn:strCacheClientPublic Propertiesredis: The redis instance. Allows for direct interation with the python Redis APIPublic MethodsCacheClient(app=None): Constructorinit_app(self, app): Initialize the redis client with application.paramapp: A Flask application instance. app.configmusthaveREDIS_HOSTREDIS_PORTandREDIS_DB.NOTEREDIS_PASSWORDis also used, but this methodwill notthrow without itget: Alias for redis.getset: Alias for redis.set