Exemplo n.º 1
0
def login_attempt(username, password):
  mongo = database_service.get_mongo_client()
  token = None
  for user in mongo.db.users.find({'profile.username': username}):
    if (AuthSession().compare_username_password(user, username, password)):
      token = AuthSession().make_token({'profile': user['profile'], 'role': user['role']})

  if token:
    return {'token': token, 'valid': True}
  
  return {'valid': False}
Exemplo n.º 2
0
def require_permission(valid_permissions):
  mongo = database_service.get_mongo_client()
  def wrapped_method(f):
    def check_for_role(**args):
      if not 'Authorization' in flask.request.headers:
        return json.dumps({'error': 'Request is missing an authorization header.'}), 401

      authorization_header = flask.request.headers.get('Authorization')
      auth_result = AuthSession().validate_header(authorization_header, valid_permissions, mongo)
      if not auth_result['valid']:
        return json.dumps({'error': auth_result['error']}), 401
      return f(**args)
    return check_for_role
  return wrapped_method
Exemplo n.º 3
0
def add_routes(app):
    mongo = database_service.get_mongo_client()
    # Login endpoint.  Checks a submitted username and password against the users in the users collection.
    # If there is a match, the user's profile and role are put into a Json Web Token and that token is
    # returned.
    @app.route('/login', methods=['POST'])
    def login():
        body = flask.request.get_json()
        username = body['username']
        password = body['password']
        result = auth_service.login_attempt(username, password)

        if result['valid']:
            return json.dumps({'token': result['token']}), 200, {
                'Content-Type': 'application/json'
            }

        return json.dumps(
            {'error':
             'There is no user with this username and password.'}), 400
Exemplo n.º 4
0
def save_tweets(tweets):
    mongo = database_service.get_mongo_client()
    mongo.db.tweets.insert_many(copy.deepcopy(tweets))
Exemplo n.º 5
0
def get_tweets_from_db(search_text):
    mongo = database_service.get_mongo_client()
    matching_tweets = mongo.db.tweets.find(format_db_search(search_text))
    return dumps(list(matching_tweets))
Exemplo n.º 6
0
def get_tweets_from_db(search_text=None, limit=100):
    """ Retrieve Tweets from Database. """
    mongo = database_service.get_mongo_client()
    matching_tweets = mongo.db.tweets.find(format_db_search(search_text)).sort(
        'id', -1).limit(limit)
    return list(matching_tweets)
Exemplo n.º 7
0
def get_all():
    mongo = database_service.get_mongo_client()
    users = []
    for user in mongo.db.users.find():
        users.append({'profile': user['profile'], 'role': user['role']})
    return users