Package: brighthive-authlib
BrightHive Auth LibraryAuthlib is a library built specifically for simplifying the task of adding authentication and authorization features to RESTful web services that reside within BrightHive Data Trusts. With this library, developers simply need to provision an OAuth 2.0 provider and inject it into a simple decorator to protect specific URLs.Quick LinksRead the docsFeaturesBuilt-in Support for Auth0.Simple decorator for injecting auth capabilities into applications.Purpose-built for use withFlaskapplications, however can be extended to other frameworks.InstallationPyPiPypiis the fastest method for installing this library. Simply install thebrighthive-authlibpackage via the Pip installer. This library is intended for use with Python 3.5+.pipinstallbrighthive-authlibPipenvInstalling the library viaPipenvis straightforward.pipenvinstallbrighthive-authlibDependenciesflask >= 1.0.2pycryptodome >= 3.8.0python-jose[pycryptodome] >= 3.0.1requests >= 2.21.0UsageAuth0 ExampleThe code snippet shown below is a simple Flask application configured to useAuth0as the default OAuth 2.0 provider. Three steps are required to configure the library for use with a Flask application:Instantiate the OAuth 2.0 provider object.Decorate the route(s) to protect with thetoken_requireddecorator (passing the OAuth 2.0 provider object and optionally a dictionary of scopes).Configure the custom error handler to deal withOAuth2ProviderErrorexceptions (in this example, a 401 status is returned).Sit back and marvel at how painless the entire process is.importjsonfromflaskimportFlask,requestfrombrighthive_authlibimportOAuth2ProviderError,OAuth2ProviderFactory,AuthLibConfiguration,token_required# Warning: Testing purposes only. These attributes need to be protected.PROVIDER='AUTH0'OAUTH2_URL='https://brighthive-test.auth0.com'JSON_URL='{}/.well-known/jwks.json'.format(OAUTH2_URL)AUDIENCE='http://localhost:8000'ALGORITHMS=['RS256']# Build the Auth Service Configuration Objectauth_config=AuthLibConfiguration(provider=PROVIDER,base_url=OAUTH2_URL,jwks_url=JSON_URL,algorithms=ALGORITHMS,audience=AUDIENCE)oauth2_provider=OAuth2ProviderFactory.get_provider(PROVIDER,auth_config)# Builed the Flask Appapp=Flask(__name__)# Add a Public Route@app.route('/public')defpublic_resource():returnjson.dumps({'message':'You can see me because I am public!'}),200# Add a Private Route@app.route('/private')@token_required(oauth2_provider,['get:users'])defprivate_resource():returnjson.dumps({'message':'If you can see me, you have a valid token.'}),200# Error Handler for Invalid or Expired Tokens@app.errorhandler(OAuth2ProviderError)defhandle_auth_error(e):returnjson.dumps({'message':'Access Denied'}),401if__name__=='__main__':app.run(debug=True)ContributingTo contribute code to this project, simply:Fork the repository fromhere.Write some code and associated unit tests.Create a pull request.Setting up for DevelopmentTo set up a development environment, clone the repository and create a virtual environmemt withPipenv.gitclonegit@github.com:brighthive/tpot-abacus-api.gitcdtpot-abacus-apiInstall Python project and development dependencies.pipenvinstall--devTestingThis project uses the excellentpytestandexpectslibraries for unit testing. All unit tests are housed in thetestsmodule. To run unit tests, invokepytestat the command prompt.$pytest=======testsessionstarts=======platformdarwin--Python3.7.2,pytest-4.3.1,py-1.8.0,pluggy-0.9.0
rootdir:/Users/gmundy/Work/brighthive-data-trusts/authlib,inifile:pytest.ini
plugins:cov-2.6.1
collected2items

tests/test_provider_factory.py..[100%]===========2passedin0.60seconds===========LicenseThe MIT License (MIT)Copyright ©2019BrightHivePermission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.