Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import wraps
- from flask import Flask, Response, request
- from flask_babel import Babel
- app = Flask(__name__)
- babel = Babel(app)
- app.config.from_object('config')
- def check_auth(username, password):
- return username == 'admin' and password == 'secret'
- def authenticate():
- return Response(
- 'Could not verify your access level for that URL.\n'
- 'You have to login with proper credentials', 401,
- {'WWW-Authenticate': 'Basic realm="Login Required"'})
- def requires_auth(f):
- @wraps(f)
- def decorated(*args, **kwargs):
- auth = request.authorization
- if not auth or not check_auth(auth.username, auth.password):
- return authenticate()
- return f(*args, **kwargs)
- return decorated
- @app.route('/admin', methods=['GET', 'POST'])
- @requires_auth
- def admin():
- return 'admin'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement