Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. from flask import Flask
  2. from functools import wraps
  3.  
  4. app = Flask(__name__)
  5.  
  6. # Check that the request has the correct `bearer_token`.
  7. def authenticate_admin(func):
  8. @wraps(func)
  9. def wrapped(*args, **kwargs):
  10. bearer_token = vault.get('secret/oauth')['bearer_token']
  11. expected = ("Bearer " + bearer_token)
  12. if expected != request.headers.get('Authorization'):
  13. return jsonify({'error': "Authorization token incorrect"}), 401
  14.  
  15. return func(*args, **kwargs)
  16. return wrapped
  17.  
  18.  
  19. # .... Define a bunch of routes (Elided) ....
  20.  
  21. for rule in app.url_map.iter_rules():
  22. # NEXT LINE IS PSEUDOCODE; IT IS WHAT I WANT TO ACHIEVE
  23. rule.fx = authenticate_admin(rule.fx)
  24.  
  25. from flask import request
  26. import vault
  27.  
  28. app = Flask(__name__)
  29.  
  30. # .... Define a bunch of routes (Elided) ....
  31.  
  32.  
  33. WHITELIST_POST = ['/post1', '/post2', '/post3']
  34.  
  35.  
  36. WHITELIST_GET = ['/', '/get1', '/get2']
  37.  
  38.  
  39. def authenticate():
  40. if request.method == "GET" and request.url_rule.rule in WHITELIST_GET:
  41. return
  42. if request.method == "POST" and request.url_rule.rule in WHITELIST_POST:
  43. return
  44. bearer_token = vault.get('secret/oauth')['bearer_token']
  45. expected = ("Bearer " + bearer_token)
  46. if expected != request.headers.get('Authorization'):
  47. abort(401)
  48.  
  49. app.before_request(authenticate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement