Example #1
0
def open_socket():
    token = get_param_or_default('token')
    email = db.get_email(token)[0]
    if (db.is_connected_mail(email)):
        db.update_token(email, token)
    success("Socket opened")
    return json.dumps(output)
Example #2
0
def sign_out():
    arg = request.args.to_dict()
    token = arg.get('token', None)
    email = logged_users_by_token.pop(token, None)

    if email:
        database_helper.update_token(email, '')
        response = {'success': True, 'message': 'Successfully signed out.'}
    else:
        response = {'success': False, 'message': 'You are not signed in.'}

    return json.dumps(response)
Example #3
0
def sign_in():
    email = request.json['email']
    password = request.json['password']
    user = database_helper.get_user(email)

    if len(user) == 0:
        return json.dumps({
            'success': False,
            'message': "User not found",
            'data': ""
        })
    elif password != user[0]['password']:
        return json.dumps({
            'success': False,
            'message': "Wrong password",
            'data': ""
        })
    else:
        token = generate_token()
        if database_helper.is_logged_in(email):
            ws = ws_dic[email]
            ws.send("Log out command!")
            ws.close()
            print "before delete:"
            print ws_dic
            del ws_dic[email]
            print "after delete"
            print ws_dic
            database_helper.update_token(email, token)
            return json.dumps({
                'success': True,
                'message':
                "You successfully signed in (and your other logged in session was logged out)",
                'data': token
            })
        else:
            res = database_helper.save_token(email, token)
            if res:
                return json.dumps({
                    'success': True,
                    'message': "You successfully signed in",
                    'data': token
                })
            else:
                return json.dumps({
                    'success': False,
                    'message': "Sign in was unsuccessful",
                    'data': ""
                })
Example #4
0
def connect():
    #send message to client with the token
    if request.environ.get('wsgi.websocket'):
        ws = request.environ['wsgi.websocket']
        while True:
            user_token = ws.receive()
            if user_token != None:
                if (db.is_connected(user_token)):
                    email = db.get_email(user_token)[0]
                    if email in socket_dict:
                        db.update_token(email, user_token)
                        socket_dict[email].close()
                    socket_dict[email] = ws
    success("Socket closed")
    return json.dumps(output)
Example #5
0
def sign_in():
    arg = request.args.to_dict(
    )  # Example: {'password': '******', 'email': 'a@a'}
    email = arg.get('email', None)
    password = arg.get('password', None)
    user = database_helper.get_user_data_by_email(email)

    if user and user['password'] == password:
        token = generate_token()
        database_helper.update_token(user['email'], token)
        logged_users_by_token[token] = email
        response = {
            'success': True,
            'message': 'Successfully signed in.',
            'token': token
        }
    else:
        response = {'success': False, 'message': 'Wrong username or password.'}

    return json.dumps(response)
Example #6
0
def sign_in():
    email = get_param_or_default("email")
    user = db.find_user(email)
    if (user == None):
        err(ERR_INVALID_USER_PASSWORD)
        return json.dumps(output)

    user = json.loads(user)
    if user != None:
        password = user["password"]
        typed_password = get_param_or_default("password")
        if typed_password == password:
            token = generate_token()
            connected = db.is_connected_mail(email)
            if (connected):
                db.update_token(email, token)
            else:
                db.log_user(email, token)
            success(SUC_USER_LOGGED_IN, token)
        else:
            err(ERR_INVALID_USER_PASSWORD)
    else:
        err(ERR_INVALID_USER_PASSWORD)
    return json.dumps(output)