Created
January 29, 2016 20:01
-
-
Save rdegges/e1b94e5981534668f697 to your computer and use it in GitHub Desktop.
Flask-Stormpath API Authentication Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from flask.ext.stormpath import StormpathManager | |
from stormpath.api_auth import BasicRequestAuthenticator | |
app = Flask(__name__) | |
app.config['DEBUG'] = True | |
app.config['STORMPATH_API_KEY_ID'] = 'xxx' | |
app.config['STORMPATH_API_KEY_SECRET'] = 'yyy' | |
app.config['STORMPATH_APPLICATION'] = 'blah' | |
stormpath_manager = StormpathManager(app) | |
@app.route('/test') | |
def test(): | |
authenticator = BasicRequestAuthenticator(stormpath_manager.application) | |
result = authenticator.authenticate(headers=request.headers) | |
if not result or not result.api_key.is_enabled(): | |
return jsonify(error='Bad API credentials supplied.'), 401 | |
# do stuff with user object here | |
user = stormpath_manager.load_user(result.account.href) | |
print user.email | |
return jsonify(status='you successfully authenticated!') | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment