Guest User

Untitled

a guest
Jun 22nd, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. from eve import Eve
  2. from eve.auth import BasicAuth
  3. from myApp.auth import * #this is the auth.py
  4. from myApp.rate_limit import *
  5. from myApp.callbacks import inject_signature, log_every_get
  6. from myApp.middleware import Loggermiddleware
  7. from myApp.blueprints import simple_page, page_group_by
  8.  
  9. from myApp.models import db
  10.  
  11. import os
  12. import logging
  13. from logging.handlers import TimedRotatingFileHandler
  14. # from logbook import Logger, StreamHandler
  15.  
  16. import redis
  17. r = redis.StrictRedis(host='127.0.0.1', password='blabla')
  18.  
  19.  
  20. class MyBasicAuth(BasicAuth):
  21. def check_auth(self, username, password, allowed_roles, resource, method):
  22. return username == 'admin' and password == 'secret'
  23.  
  24.  
  25. # Eve App Definition
  26. SETTINGS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'instance/settings.py')
  27. LOGS_PATH = '/var/log/blabla.log'
  28.  
  29. app = Eve(auth=MyBasicAuth, redis=r, settings=SETTINGS_PATH)
  30.  
  31. # MongoDB
  32. db.init_app(app)
  33.  
  34. # BluePrints (routs)
  35. app.register_blueprint(simple_page)
  36. app.register_blueprint(page_group_by)
  37.  
  38. # Hooks / Callbacks
  39. app.on_post_GET += log_every_get
  40. app.on_fetched_item += inject_signature
  41.  
  42. # Middleware
  43. app.wsgi_app = Loggermiddleware(app.wsgi_app)
  44.  
  45.  
  46. # Hooks
  47. @app.after_request
  48. def do_something_whenever_a_request_has_been_handled(response):
  49. # we have a response to manipulate, always return one
  50. print('after_request --> 02')
  51. return response
  52.  
  53.  
  54. @app.after_request
  55. def inject_x_rate_headers(response):
  56. print('after_request --> 01')
  57. limit = get_view_rate_limit()
  58.  
  59. if limit and limit.send_x_headers:
  60. h = response.headers
  61. h.add('X-RateLimit-Remaining', str(limit.remaining))
  62. h.add('X-RateLimit-Limit', str(limit.limit))
  63. h.add('X-RateLimit-Reset', str(limit.reset))
  64.  
  65. return response
  66.  
  67.  
  68. if __name__ == '__main__':
  69. handler = TimedRotatingFileHandler(LOGS_PATH)
  70. handler.setFormatter(logging.Formatter(
  71. '%(asctime)s %(levelname)s: %(message)s '
  72. '[in %(filename)s:%(lineno)d] -- ip: %(clientip)s, '
  73. 'url: %(url)s, method:%(method)s'))
  74. app.logger.addHandler(handler)
  75. app.logger.setLevel(logging.DEBUG)
  76.  
  77. # let's go
  78. app.run(host='0.0.0.0', port=8000)
  79.  
  80. SERVER_NAME = None
  81. DEBUG = True
  82. ENV = 'development'
  83.  
  84. URL_PREFIX = 'api'
  85. API_VERSION = 'v1'
  86.  
  87. SORTING = True
  88.  
  89. # PAGINATION_DEFAULT = 25
  90. # PAGINATION_LIMIT = 50
  91.  
  92. CACHE_CONTROL = 'max-age=15,must-revalidate'
  93. CACHE_EXPIRES = 15
  94.  
  95. RATE_LIMIT_GET = (60, 60 * 1)
  96. RENDERERS = ['eve.render.JSONRenderer']
  97.  
  98. # CORS support
  99. X_DOMAINS = '*'
  100.  
  101. # RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
  102. RESOURCE_METHODS = ['GET']
  103.  
  104. # ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']
  105. ITEM_METHODS = ['GET']
  106.  
  107. from eve.auth import TokenAuth
  108. from flask import Response, abort, request, current_app
  109.  
  110.  
  111. class MyTokenAuth(TokenAuth):
  112. def check_auth(self, token, allowed_roles, resource, method):
  113. """For the purpose of this example the implementation is as simple as
  114. possible. A 'real' token should probably contain a hash of the
  115. username/password combo, which should then validated against the account
  116. data stored on the DB.
  117. """
  118. print('TOKEN: {}'.format(token))
  119. accounts = current_app.data.driver.db['accounts']
  120. account = accounts.find_one({'token': token})
  121.  
  122. print('-- ACCOUNT: {}'.format(account))
  123.  
  124. if account:
  125. active = account["active"]
  126. if active:
  127. return True
  128.  
  129. return False
  130.  
  131. def authenticate(self):
  132. """ Returns a standard a 401. Override if you want to change the
  133. response.
  134. """
  135. resp = Response(None, 401, {'WWW-Authenticate': 'Basic realm="%s"' %
  136. __package__})
  137. abort(401, description='Please provide proper credentials :)',
  138. response=resp)
Add Comment
Please, Sign In to add comment