Пример #1
0
def logout_user(user_id):
    response = create_generic_response_dict()
    
    if request.method == 'POST':
        datastore = UserStore()
        user = datastore.get_user_by_id(user_id)
        
        if user == None:
            response['error'] = True
            response['message'] = 'User was not found.'
        else:
            token_store = AuthTokenStore()
            token_store.kill_user_token(user)
            response['message'] = 'User logged out successfully'
    
    logger.info('Caught logout request for user_id:' + str(user_id) + '->' + response['message'])
    return jsonify(response)
Пример #2
0
def authenticate_user(user_id):
    response = create_generic_response_dict()
    request
    if request.method == 'GET':
        data = request.args
        user = UserStore().get_user_by_id(user_id)        
        token = data.get('token')
        
        if not user:
            response['error'] = True
            response['message'] = 'User not found.'
        elif not token:
            response['error'] = True
            response['message'] = 'Invalid token.'
        else:
            token_store = AuthTokenStore()
            response['message'] = 'Token was processed'
            response['data']['valid'] = token_store.is_token_valid(user, token)
    
    logger.info('Caught authenticate request for user_id:' + str(user_id) + '->' + response['message'])
    
    return jsonify(response)
Пример #3
0
def login_user(user_id):
    response = create_generic_response_dict()
    
    if request.method == 'POST':
        data = request.form
        password = data.get("password", None)
        
        datastore = UserStore()
        user = datastore.get_user_by_id(user_id)
        
        if user == None or password == None or not security.validate_login(user, password):
            response['error'] = True
            response['message'] = 'User and password combination does not validate.'
        else:
            auth_token = AuthTokenStore().register(user)
            user_data = user.get_values_as_dict()
            user_data['token'] = auth_token.get_values_as_dict()
            response['data'] = user_data
            response['message'] = 'User successfully logged in'
            
    logger.info('Caught login request for user_id:' + str(user_id) + '->' + response['message'])
    return jsonify(response)