def register_extensions(app): from flask_webpack import Webpack webpack = Webpack() webpack.init_app(app) from security import authenticate, identity, jwt_error_handler, jwt_payload_callback from flask_jwt import JWT jwt = JWT(app, authentication_handler=authenticate, identity_handler=identity) jwt.jwt_error_callback = jwt_error_handler jwt.jwt_payload_callback = jwt_payload_callback from models import db db.init_app(app) print "app.debug %s" % app.debug print "app.config['SECRET_KEY'] %s" % app.config['SECRET_KEY']
user_id=new_user.user_id, location_string="Timbuktu", active=True, date_time=datetime.datetime.now() ) models.db.session.add(new_location) new_location2 = models.Location( user_id=new_user.user_id, location_string="Stanford", active=True, date_time=datetime.datetime.now() - datetime.timedelta(500), ) models.db.session.add(new_location2) new_location3 = models.Location( user_id=new_user.user_id, location_string="Secret Location", active=False, date_time=datetime.datetime.now() - datetime.timedelta(50), ) models.db.session.add(new_location3) models.db.session.commit() print(new_user.user_id) models.db.create_all() jwt = JWT(app=None, authentication_handler=authenticate, identity_handler=identity) jwt.app = app jwt.auth_request_callback = jwt_handlers.auth_request_handler jwt.jwt_encode_callback = jwt_handlers.encode_handler jwt.jwt_payload_callback = jwt_handlers.payload_handler jwt.auth_response_callback = jwt_handlers.auth_response_handler jwt.init_app(jwt.app)
app.config.update({ 'DEBUG': False, 'SECRET_KEY': 'secret_xxx', 'JWT_AUTH_URL_RULE': None, }) CORS(app) Pony(app) CRYPT_CONTEXT = CryptContext(schemes=['scrypt', 'bcrypt']) day_names = list(calendar.day_name) logging.basicConfig(level=logging.INFO) jwt = JWT(None, None, lambda payload: db.Student.get(id=payload['id'])) jwt.auth_request_callback = None jwt.jwt_payload_callback = lambda identity: dict( _default_jwt_payload_handler(identity), username=identity.name) jwt.init_app(app) def do_404(func): @wraps(func) def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except FileNotFoundError: return flask.abort(404) return wrapper def parse_time_constraint(param):
tok = sts.get_session_token(DurationSeconds=app.config.get('AWS_STS_EXPIRE_DELTA_SECONDS') or 900) return { 'credentials': { 'aws_access_key_id': tok['Credentials']['AccessKeyId'], 'aws_secret_access_key': tok['Credentials']['SecretAccessKey'], 'aws_session_token': tok['Credentials']['SessionToken'] }, 'expiration': str(tok['Credentials']['Expiration']) } @api.route('/apps') class Applications(Resource): @jwt_required() def get(self): """Handles a GET request for the applications resource""" # apps at this point are in the current_identity object. technically a user # could unwrap it themselves but we prefer they don't depend on the jwt payload to get # this information just in case the implementation changes return current_identity.apps jwt = JWT(app, jwt_authenticate, jwt_identity) jwt.jwt_payload_callback = jwt_payload if __name__ == '__main__': app.run() def start(): app.run()
from flask import jsonify from sqlalchemy.exc import SQLAlchemyError from flask_jwt import JWT from user_api.resources.user import UserResource from user_api import (user_api_app, api, db) from user_api.auth import (generate_jwt_token, generate_jwt_payload, generate_jwt_headers, generate_auth_response, generate_error_response, authenticate, load_identity) api.add_resource(UserResource, "/users", "/users/<int:id>") jwt = JWT(user_api_app, authenticate, load_identity) jwt.jwt_encode_callback = generate_jwt_token jwt.jwt_payload_callback = generate_jwt_payload jwt.jwt_headers_callback = generate_jwt_headers jwt.auth_response_callback = generate_auth_response @jwt.jwt_error_handler def error_handler(e): return generate_error_response(e) @user_api_app.route("/api/healthcheck") def healthcheck(): try: db.engine.execute("SELECT 1;").fetchone() return jsonify({"status": "OK"}) except SQLAlchemyError: return jsonify({"status": "DOWN"})
from datetime import timedelta, datetime from flask import Flask from flask_jwt import JWT from flask_cors import CORS app = Flask(__name__) # app.debug = True app.config['SECRET_KEY'] = 'super-secret' app.config['JWT_AUTH_URL_RULE'] = None app.config['JWT_EXPIRATION_DELTA'] = timedelta(days=30) app.config['JWT_SECRET_KEY'] = app.config['SECRET_KEY'] # CORS(app, max_age=3628800, origins='*', supports_credentials=True) CORS(app=app) from api_v1.auth import * from api_v1.views import * jwt = JWT(app, authenticate, identity) jwt.jwt_payload_callback = jwt_payload_callback if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)