Example #1
0
def get_user_data_helper(token, email):
    userdata = database_helper.get_user_by_email(email)

    if database_helper.get_active(token) == None:
        return json.dumps({
            "success": False,
            "message": "You are not signed in."
        })

    if userdata == None:
        return json.dumps({"success": False, "message": "No such user."})
    else:
        if database_helper.increment_user_visits(email):
            print "get_user_data_by_email(): increment_user_visits() succeeded"
        else:
            print "get_user_data_by_email(): increment_user_visits() failed"
        update_visit_count()
        data = {
            "email": userdata[0],
            "firstname": userdata[1],
            "familyname": userdata[2],
            "gender": userdata[3],
            "city": userdata[4],
            "country": userdata[5]
        }
        return json.dumps({
            "success": True,
            "message": "User data retrieved.",
            "data": data
        })
Example #2
0
def sign_in():
    email = request.form["email"]
    password = request.form["password"]
    if '@' in email :
        userdata = database_helper.get_user_by_email(email)
        if userdata != None and userdata[6] == password :
            ws = websockets.get(email, None)
            if ws != None :
                if not ws["websocket"].closed :
                    ws["websocket"].send(json.dumps({"messagetype": "forcedLogout", "message": "Forced logout: User signed in from other location."}))
                    websockets[email]['websocket'].close(1000, "Another user logged in")
                database_helper.remove_active(database_helper.get_active_by_email(email))
                #del websockets[email]
                
            if database_helper.get_active_by_email(email) != None :
                database_helper.remove_active_by_email(email)
                
            print "sign_in(): Generating token."
            token = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(36))
            success = database_helper.add_active(token, email)
            print "sign_in(): database_helper.add_active(token, email) = " + str(success)
            if success == True :
                update_user_count()
                return json.dumps({"success": True, "message": "Successfully signed in.", "data": token})
    return json.dumps({"success": False, "message": "Wrong username or password."})
Example #3
0
def sign_up(email, password, firstname, familyname, gender, city, country):
    if '@' in email and len(
            password
    ) > 7 and firstname and familyname and gender and city and country:
        if database_helper.get_user_by_email(email) == None:
            success = database_helper.add_user(email, password, firstname,
                                               familyname, gender, city,
                                               country)
        else:
            return json.dumps({
                "success": False,
                "message": "User already exist."
            })
    else:
        return json.dumps({
            "success": False,
            "message": "Form data missing or incorrect type."
        })

    if success == False:
        return json.dumps({
            "success": False,
            "message": "Error adding user to database."
        })
    return json.dumps({
        "success": True,
        "message": "Successfully created a new user."
    })
def get_user_data_by_email(token, email):
    userdata = database_helper.get_user_by_email(email)

    if database_helper.get_active(token) == None:
        return json.dumps({
            "success": False,
            "message": "You are not signed in."
        })

    if userdata == None:
        return json.dumps({"success": False, "message": "No such user."})
    else:
        data = {
            "email": userdata[0],
            "firstname": userdata[1],
            "familyname": userdata[2],
            "gender": userdata[3],
            "city": userdata[4],
            "country": userdata[5]
        }
        return json.dumps({
            "success": True,
            "message": "User data retrieved.",
            "data": data
        })
def sign_up(email, password, firstname, familyname, gender, city, country):
    if '@' in email and len(
            password
    ) > 7 and firstname and familyname and gender and city and country:
        if database_helper.get_user_by_email(email) == None:
            print "ENTERED SIGN_IF_STATEMENT"
            success = database_helper.add_user(email, password, firstname,
                                               familyname, gender, city,
                                               country)
        else:
            return json.dumps({
                "success": False,
                "message": "User already exist."
            })
    else:
        print "email:'" + email + "' password:'******' firstname:'" + firstname + "' familyname:'" + familyname + "' gender:'" + gender + "' city:'" + city + "' country:'" + country

        return json.dumps({
            "success": False,
            "message": "Form data missing or incorrect type."
        })

    if success == False:
        return json.dumps({
            "success": False,
            "message": "Error adding user to database."
        })
    return json.dumps({
        "success": True,
        "message": "Successfully created a new user."
    })
Example #6
0
def user_exists(email):
    user = database_helper.get_user_by_email(email)
    if user == None:
        result = False
    else:
        result = True
    return result
Example #7
0
def change_password():
    hash = request.form['hash']
    oldPass = request.form['old']
    newPass = request.form['new']
    email = request.form['email']

    logged_in_user = database_helper.get_logged_in_user_by_email(email)

    if logged_in_user:
        token = logged_in_user[1]
        params = "&email=" + email + "&old=" + oldPass + "&new=" + newPass + "&hash="
        if check_hash(hash, params, token):
            if validate_password(newPass):
                if check_password(token, oldPass):
                    user = database_helper.get_user_by_email(email)
                    salt = user[7]
                    result = database_helper.update_password(
                        email, create_hash(newPass, salt))
                    if result:
                        return_code = create_return_code(
                            True, 'Password changed')
                    else:
                        return_code = create_return_code(
                            False, 'Could not change password')
                else:
                    return_code = create_return_code(False, 'Wrong password')
            else:
                return_code = create_return_code(False,
                                                 'Enter a valid password')
        else:
            return_code = create_return_code(False, 'Bad token')
    else:
        return_code = create_return_code(False, 'You are not logged in')

    return json.dumps(return_code)
Example #8
0
def sign_up():
    input_data = request.get_json()
    input_email = input_data['email']
    # print(input_email)
    if database_helper.get_user_by_email(input_email) is None:
        password = input_data['password']
        if len(password) < 4:
            json.dumps({"success": False, "message": "Password too short."})
        firstname = input_data['firstname']
        familyname = input_data['familyname']
        gender = input_data['gender']
        city = input_data['city']
        country = input_data['country']
        response = database_helper.save_user(input_email, password, firstname,
                                             familyname, gender, city, country)
        if response:
            return json.dumps({
                "success": True,
                "message": "Successfully created a new user."
            })
        else:
            return json.dumps({
                "success": False,
                "message": "Form data missing or incorrect type."
            })
    else:
        return json.dumps({
            "success": False,
            "message": "User already exists."
        })
    return False
Example #9
0
def get_user_data_by_email():
    hash = request.form['hash']
    email = request.form['email']
    searched = request.form['searched']
    user = database_helper.get_logged_in_user_by_email(email)

    if user:
        token = user[1]
        params = "&email=" + email + "&searched=" + searched + "&hash="
        if check_hash(hash, params, token):
            result = database_helper.get_user_by_email(searched)
            if result:
                found_user = {
                    'email': result[0],
                    'firstname': result[2],
                    'familyname': result[3],
                    'gender': result[4],
                    'city': result[5],
                    'country': result[6]
                }
                return_code = create_return_code(True, 'Userdata found',
                                                 found_user)
            else:
                return_code = create_return_code(False, 'User not found')
        else:
            return_code = create_return_code(False, 'Bad token')
    else:
        return_code = create_return_code(False, 'You are not logged in')
    return json.dumps(return_code)
Example #10
0
def sign_in():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']

        result = database_helper.get_user_by_email(email)
        if not result:
            return_code = create_return_code(False,
                                             'Wrong username or password')
        else:
            hashed_password = hashlib.sha512(password + result[7]).hexdigest()

            if hashed_password == result[1]:
                token = generate_token()
                logged_in_status = database_helper.get_logged_in_user_by_email(
                    email)
                if logged_in_status:
                    database_helper.remove_logged_in_user(logged_in_status[1])
                    if email in current_sockets.keys():
                        ws = current_sockets[email]
                        status = create_return_code(
                            False, 'Session logged out',
                            {'statusCode': _LOGGED_OUT_})
                        ws.send(json.dumps(status))
                        del current_sockets[email]

                database_helper.add_logged_in_user(email, token)
                return_code = create_return_code(
                    True, 'User successfully logged in', token)
            else:
                return_code = create_return_code(False,
                                                 'Wrong username or password')
        return json.dumps(return_code)
Example #11
0
def change_password():
    token = request.form["token"]
    oldpass = request.form["oldpassword"]
    newpass = request.form["newpassword"]

    email = database_helper.get_active(token)
    print email
    if email == None:
        return json.dumps({
            "success": False,
            "message": "You are not logged in."
        })

    userdata = database_helper.get_user_by_email(email)
    if userdata[6] != oldpass:
        return json.dumps({"success": False, "message": "Wrong password."})

    success = database_helper.change_password(email, newpass)
    if success == True:
        return json.dumps({"success": True, "message": "Password changed."})
    else:
        return json.dumps({
            "success": False,
            "message": "Password change failed."
        })
Example #12
0
def post_message():
    token = request.form['token']
    message = request.form['message']
    toEmail = request.form['email']
    fromEmail = database_helper.get_email_by_token(token)
    toUser = database_helper.get_user_by_email(toEmail)
    #check if fromEmail is signed in
    if (fromEmail):
        if (toUser is None):
            return jsonify(success = False, message = "No such user.")
        else:
            try:
                database_helper.post_message(fromEmail[0], toEmail, message);
                # Update chart
                if (toEmail == fromEmail[0]):
                    print "PM EQUALS............."
                    live_data(fromEmail[0], {"id": "update_chart", "online": -1, "sent": database_helper.get_num_messages_sent(fromEmail[0])[0], "received": database_helper.get_num_messages_received(toEmail)[0]})

                else:
                    print "PM NO EQ............."
                    live_data(fromEmail[0], {"id": "update_chart", "online": -1, "sent": database_helper.get_num_messages_sent(fromEmail[0])[0], "received":-1})
                    live_data(toEmail, {"id": "update_chart", "online":-1, "sent": -1, "received": database_helper.get_num_messages_received(toEmail)[0]})
                return jsonify(success = True, message = "Message posted.")
            # Catching IntegrityError
            except sqlite3.IntegrityError:
                return jsonify(success = False, message = "This message is already posted.")           
    else:
        return jsonify(success = False, message = "You are not logged in.")
Example #13
0
def sign_in():
    # print("sign in")
    input_data = request.get_json()
    input_email = input_data['email']
    input_password = input_data['password']

    db_user = database_helper.get_user_by_email(input_email)
    if (db_user is not None and db_user['password'] == input_password):
        # if input_email in logged_user:
        #     message = {'message': "logged in other place"}
        #     try:
        #         logged_user[input_email].send(json.dumps(message))
        #     except:
        #         pass
        # token = create token
        letters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        token = ""
        for i in range(36):
            token += letters[random.randrange(0, len(letters))]
        # insert token and email to db
        database_helper.save_token(token, input_email)
        return json.dumps({
            "success": True,
            "message": "Successfully signed in.",
            "data": token
        })
    else:
        return json.dumps({
            "success": False,
            "message": "Wrong username or password."
        })
def sign_in(email, password):
    if '@' in email :
        userdata = database_helper.get_user_by_email(email)
        if userdata != None and userdata[6] == password :
            token = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(36))
            success = database_helper.add_active(token, email)
            if success == True :
                return json.dumps({"success": True, "message": "Successfully signed in.", "data": token})
    return json.dumps({"success": False, "message": "Wrong username or password."})
Example #15
0
def get_user_data_by_token():
    input_data = request.get_json()
    token = input_data['token']
    email = database_helper.get_email_by_token(token)
    if email:
        user_data = database_helper.get_user_by_email(email)
        if user_data:
            return json.dumps({"success": True, "message": "User data retrieved.", "data": user_data})
        else:
            return json.dumps({"success": False, "message": "No such user."})
    else:
        return json.dumps({"success": False, "message": "You are not signed in."})
Example #16
0
def sign_up(email, password, firstname, familyname, gender, city, country):
    if '@' in email and len(password) > 7 and firstname and familyname and gender and city and country :
        if database_helper.get_user_by_email(email) == None :
            success = database_helper.add_user(email, password, firstname, familyname, gender, city, country)
        else :
            return json.dumps ({"success": False, "message": "User already exist."})           
    else : 
        return json.dumps ({"success": False, "message": "Form data missing or incorrect type."})
        
    if success == False :
        return json.dumps ({"success": False, "message": "Error adding user to database."})
    return json.dumps({"success": True, "message": "Successfully created a new user."})
Example #17
0
def get_user_data_by_token():
    token = request.values.get('token')
    user = database_helper.get_user_by_token(token)
    
    if len(user) > 0:
        userdata = database_helper.get_user_by_email(user[0][0])
        if len(userdata) > 0:
            return jsonify(success = True, message = "Data successfully got", data = [userdata[0][0], userdata[0][2], userdata[0][3], userdata[0][4], userdata[0][5], userdata[0][6]])
        else:
            return jsonify(success = False, message = "Something went terribly wrong, this shouldn't happen")

    else:
        return jsonify(success = False, message = "You're not logged in")
Example #18
0
def post_message(token, message, email):
    sender = database_helper.get_active(token)
    if sender == None :
        return json.dumps({"success": False, "message": "You are not signed in."})
    if database_helper.get_user_by_email(email) == None :
        return json.dumps({"success": False, "message" : "No such user."})
        
    post_time = int(time.time())
    success = database_helper.add_message(email, sender, post_time, message)
    
    if success == True :
        return json.dumps({"success": True, "message": "Message posted."})
    return json.dumps({"success": False, "message": "Failed to post message."})
Example #19
0
def get_user_messages_by_email(token, email) :
    if database_helper.get_active(token) == None :
        return json.dumps({"success": False, "message" : "You are not signed in."})
    if database_helper.get_user_by_email(email) == None :
        return json.dumps({"success": False, "message" : "No such user."})

    messages = database_helper.get_messages(email)
    
    message_list = []
    for (recipient, sender, time, message) in messages : 
        json_message  = {"sender" : sender, "time" : time, "message" : message}
        message_list.append(json_message)
    return json.dumps({"success": True, "message" : "User messages retrieved", "data" : message_list})
Example #20
0
def get_user_messages_by_token():
    token = request.form['token']
    email = database_helper.get_email_by_token(token)
    # Check if the user is loggedin
    if (email is None):
        return jsonify(success = False, message = "You are not logged in.")
    else:
        user = database_helper.get_user_by_email(email[0])
        if (user is None):
            return jsonify(success = False, message = "No such user.")
        else:
            messages = database_helper.get_messages_by_email(email[0])
            return jsonify(success = True, message = "User messages retrieved.", data = messages)
Example #21
0
def get_user_messages_helper(token, email):
    if database_helper.get_active(token) == None :
        return json.dumps({"success": False, "message" : "You are not signed in."})
    if database_helper.get_user_by_email(email) == None :
        return json.dumps({"success": False, "message" : "No such user."})
    
    messages = database_helper.get_messages(email)
    
    message_list = []
    for (recipient, sender, time, message) in messages : 
        json_message  = {"sender" : sender, "time" : time, "message" : message}
        message_list.append(json_message)
    return json.dumps({"success": True, "message" : "User messages retrieved", "data" : message_list})
def post_message(token, message, email):
    sender = database_helper.get_active(token)
    if sender == None :
        return json.dumps({"success": False, "message": "You are not signed in."})
    if database_helper.get_user_by_email(email) == None :
        return json.dumps({"success": False, "message" : "No such user."})
        
    post_time = int(time.time())
    success = database_helper.add_message(email, sender, post_time, message)
    
    if success == True :
        return json.dumps({"success": True, "message": "Message posted."})
    return json.dumps({"success": False, "message": "Failed to post message."})
Example #23
0
def get_user_data_by_email():
    email = request.values.get('email')
    token = request.values.get('token')
    userFromToken = database_helper.get_user_by_token(token)
    if len(userFromToken) > 0:
        userdata = database_helper.get_user_by_email(email)
        if len(userdata) > 0:
            print "asd"
            return jsonify(data = [userdata[0][0], userdata[0][2], userdata[0][3], userdata[0][4], userdata[0][5], userdata[0][6]])
        else:
            return jsonify(message = "No such user")
    else:
        return jsonify(message = "You're not signed in")
Example #24
0
def change_password():
    input_data = request.get_json()
    token = input_data['token']
    email = database_helper.get_email_by_token(token)
    if email:
        # user logged in
        user_data = database_helper.get_user_by_email(email)
        if input_data['oldPassword'] == user_data['password']:
            database_helper.change_password(email, input_data['newPassword'])
            return json.dumps({"success": True, "message": "Password changed."})
        else:
            return json.dumps({"success": False, "message": "Wrong password."})
    else:
        return json.dumps({"success": False, "message": "You are not logged in."})
def sign_up(email, password, firstname, familyname, gender, city, country):
    if '@' in email and len(password) > 7 and firstname and familyname and gender and city and country :
        if database_helper.get_user_by_email(email) == None :
            print "ENTERED SIGN_IF_STATEMENT"
            success = database_helper.add_user(email, password, firstname, familyname, gender, city, country)
        else :
            return json.dumps ({"success": False, "message": "User already exist."})           
    else : 
        print "email:'"+email+"' password:'******' firstname:'"+firstname+"' familyname:'"+familyname+"' gender:'"+gender+"' city:'"+city+"' country:'"+country

        return json.dumps ({"success": False, "message": "Form data missing or incorrect type."})
        
    if success == False :
        return json.dumps ({"success": False, "message": "Error adding user to database."})
    return json.dumps({"success": True, "message": "Successfully created a new user."})
Example #26
0
def change_password():
    token = request.values.get('token')
    old_pw = request.values.get('old_password')
    new_pw = request.values.get('new_password')
    
    user = database_helper.get_user_by_token(token)
    if len(user) <= 0:
        return jsonify(success = False, message = "You are not logged in")

    userpw = database_helper.get_user_by_email(user[0][0])

    if old_pw == userpw[0][1]:
        database_helper.change_password(user[0][0], new_pw)
        return jsonify(success = True, message = "Password changed")
    else:
        return jsonify(success = False, message = "Wrong password")
Example #27
0
def get_user_messages_by_email():
    input_data = request.get_json()
    email = input_data['email']
    token = input_data['token']
    login_email = database_helper.get_email_by_token(token)
    if login_email is not None:
        if database_helper.get_user_by_email(email) is not None:
            messages = database_helper.get_messages_by_email(email)
            # if messages is not None:
            return json.dumps({"success": True, "message": "User messages retrieved.", "data": messages})
            # else:
            #     return json.dumps({"success": False, "message": "No message."})
        else:
            return json.dumps({"success": False, "message": "No such user."})
    else:
        return json.dumps({"success": False, "message": "You are not signed in."})
def change_password(token, oldpass, newpass):
    
    email = database_helper.get_active(token)
    print email
    if email == None :
        return json.dumps({"success": False, "message": "You are not logged in."})

    userdata = database_helper.get_user_by_email(email)
    if userdata[6] != oldpass :
        return json.dumps({"success": False, "message": "Wrong password."})
    
    success = database_helper.change_password(email, newpass)
    if success == True :
        return json.dumps({"success": True, "message": "Password changed."})
    else :
        return json.dumps({"success": False, "message": "Password change failed."})
Example #29
0
def post_message():
    token = request.values.get('token')
    message = request.values.get('message')
    reciever = request.values.get('email')
    sender = database_helper.get_user_by_token(token)
    if(token != None and message != None and reciever != None):
        if len(sender) > 0:
            tmp = database_helper.get_user_by_email(reciever)
            if len(tmp) > 0:
                database_helper.post_message(sender[0][0], reciever, message)
                return jsonify(success = True, message = "Successfully posted message")
            else:
                return jsonify(success = False, message = "No such user")
        else:
            return jsonify(success = False, message = "You're not signed in")
    else:
        return jsonify(success = False, message = "Wrong input data")
Example #30
0
def post_message():
    token = request.form["token"]
    message = request.form["message"]
    email = request.form["email"]
    sender = database_helper.get_active(token)
    if sender == None :
        return json.dumps({"success": False, "message": "You are not signed in."})
    if database_helper.get_user_by_email(email) == None :
        return json.dumps({"success": False, "message" : "No such user."})
        
    post_time = int(time.time())
    success = database_helper.add_message(email, sender, post_time, message)
    
    if success == True :
        update_message_count(email)
        return json.dumps({"success": True, "message": "Message posted."})
    return json.dumps({"success": False, "message": "Failed to post message."})
def get_user_data_by_email(token, email):
    userdata = database_helper.get_user_by_email(email)
        
    if database_helper.get_active(token) == None :
        return json.dumps({"success": False, "message": "You are not signed in."});

    if userdata == None :
        return json.dumps({"success": False, "message": "No such user."})
    else :
        data = {
                "email": userdata[0],
                "firstname": userdata[1],
                "familyname": userdata[2],
                "gender": userdata[3], 
                "city": userdata[4],
                "country": userdata[5]}
        return json.dumps({"success": True, "message": "User data retrieved.", "data": data})
Example #32
0
def post_message():
    token = request.values.get("token")
    message = request.values.get("message")
    reciever = request.values.get("email")
    sender = database_helper.get_user_by_token(token)
    if token != None and message != None and reciever != None:
        if len(sender) > 0:
            tmp = database_helper.get_user_by_email(reciever)
            if len(tmp) > 0:
                print "sender: " + sender[0][0] + " reciever: " + reciever + " message: " + message
                database_helper.post_message(sender[0][0], reciever, message, "no")
                return jsonify(success=True, message="Successfully posted message")
            else:
                return jsonify(success=False, message="No such user")
        else:
            return jsonify(success=False, message="You're not signed in")
    else:
        return jsonify(success=False, message="Wrong input data")
Example #33
0
def post_message():
    input_data = request.get_json()
    # fromEmail = input_data['fromEmail']
    message = input_data['message']
    token = input_data['token']
    fromEmail = database_helper.get_email_by_token(token)
    if fromEmail is not None:
        toEmail = input_data['email']
        if toEmail == "own":
            toEmail = fromEmail
        if database_helper.get_user_by_email(toEmail) is not None:
            database_helper.save_message(fromEmail, toEmail, message)
            return json.dumps({"success": True, "message": "Message posted"})
        else:
            return json.dumps({"success": False, "message": "No such user."})
    else:
        return json.dumps({"success": False, "message": "You are not signed in."})
    return False
def sign_in(email, password):
    if '@' in email:
        userdata = database_helper.get_user_by_email(email)
        if userdata != None and userdata[6] == password:
            token = ''.join(
                random.choice(string.ascii_letters + string.digits)
                for _ in range(36))
            success = database_helper.add_active(token, email)
            if success == True:
                return json.dumps({
                    "success": True,
                    "message": "Successfully signed in.",
                    "data": token
                })
    return json.dumps({
        "success": False,
        "message": "Wrong username or password."
    })
Example #35
0
def sign_in():
    email = request.form["email"]
    password = request.form["password"]
    if '@' in email:
        userdata = database_helper.get_user_by_email(email)
        if userdata != None and userdata[6] == password:
            ws = websockets.get(email, None)
            if ws != None:
                if not ws["websocket"].closed:
                    ws["websocket"].send(
                        json.dumps({
                            "messagetype":
                            "forcedLogout",
                            "message":
                            "Forced logout: User signed in from other location."
                        }))
                    websockets[email]['websocket'].close(
                        1000, "Another user logged in")
                database_helper.remove_active(
                    database_helper.get_active_by_email(email))
                #del websockets[email]

            if database_helper.get_active_by_email(email) != None:
                database_helper.remove_active_by_email(email)

            print "sign_in(): Generating token."
            token = ''.join(
                random.choice(string.ascii_letters + string.digits)
                for _ in range(36))
            success = database_helper.add_active(token, email)
            print "sign_in(): database_helper.add_active(token, email) = " + str(
                success)
            if success == True:
                update_user_count()
                return json.dumps({
                    "success": True,
                    "message": "Successfully signed in.",
                    "data": token
                })
    return json.dumps({
        "success": False,
        "message": "Wrong username or password."
    })
Example #36
0
def post_message():
    token = request.form['token']
    message = request.form['message']
    toEmail = request.form['email']
    fromEmail = database_helper.get_email_by_token(token)
    toUser = database_helper.get_user_by_email(toEmail)
    #check if fromEmail is signed in
    if (fromEmail):
        if (toUser is None):
            return jsonify(success = False, message = "No such user.")
        else:
            try:
                database_helper.post_message(fromEmail[0], toEmail, message);
                return jsonify(success = True, message = "Message posted.")
            # Catching IntegrityError
            except sqlite3.IntegrityError:
                return jsonify(success = False, message = "This message is already posted.")           
    else:
        return jsonify(success = False, message = "You are not logged in.")
Example #37
0
def post_message():
    token = request.form["token"]
    message = request.form["message"]
    email = request.form["email"]
    sender = database_helper.get_active(token)
    if sender == None:
        return json.dumps({
            "success": False,
            "message": "You are not signed in."
        })
    if database_helper.get_user_by_email(email) == None:
        return json.dumps({"success": False, "message": "No such user."})

    post_time = int(time.time())
    success = database_helper.add_message(email, sender, post_time, message)

    if success == True:
        update_message_count(email)
        return json.dumps({"success": True, "message": "Message posted."})
    return json.dumps({"success": False, "message": "Failed to post message."})
Example #38
0
def post_message(userEmail):
    authorEmail = request.json['myEmail']
    hashedData = request.headers.get('hashedData')
    message = request.json['message']
    data = authorEmail + userEmail + message
    serverHash = server_hash(data, authorEmail)
    if (hashedData == serverHash):
        if (database_helper.get_user_by_email(userEmail) != None):
            database_helper.add_to_messages(userEmail, authorEmail, message)
            return json.dumps({"success": True, "message": "Message posted"})
        else:
            return json.dumps({
                "success": False,
                "message": "User doesn't exist"
            })
    else:
        return json.dumps({
            "success": False,
            "message": "You have to be logged in"
        })
Example #39
0
def get_user_data_by_token():
    token = request.form['token']
    #Check if there is such a user
    email = database_helper.get_email_by_token(token)

    if (email is not None):
        user = database_helper.get_user_by_email(email[0])
        if (user is None):
            return jsonify(success = False, message = "No such user.")
        else:
            u = {}
            u['email'] = user[0]
            u['firstname'] = user[1]
            u['familyname'] = user[2]
            u['gender'] = user[3]
            u['city'] = user[4]
            u['country'] = user[5]
            return jsonify(success = True, message = "User data retrieved.", data = u)
    else:
        return jsonify(success = False, message = "You are not logged in.")
Example #40
0
def upload_file():
    if request.method == "POST":
        print "[upload_file] entered"
        token = request.values.get("token")
        message = request.values.get("message")
        reciever = request.values.get("reciever")
        sender = database_helper.get_user_by_token(token)
        file = request.files["upload_file"]
        print "[upload_file] data retrieved"
        if token != None and reciever != None:
            if len(sender) > 0:
                tmp = database_helper.get_user_by_email(reciever)
                if len(tmp) > 0:
                    characters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
                    name = ""
                    for i in range(0, 35):
                        name += characters[randint(0, len(characters) - 1)]
                    print "[upload_file] name generated"
                    print "[upload_file] message posted"

                    if file and allowed_file(file.filename):
                        print "[upload_file] inside if before split"
                        print file.filename
                        filetype = file.filename.rsplit(".", 1)[1]
                        print filetype
                        print "[upload_file] filename splitted"
                        message = name + "." + filetype
                        file.save(os.path.join(app.root_path, app.config["UPLOAD_FOLDER"], message))
                        print "[upload_file] save file"
                        print "sender: " + sender[0][0] + " reciever: " + reciever + " message: " + message
                        database_helper.post_message(sender[0][0], reciever, message, filetype)
                        print "[upload_file] save to database"
                        return jsonify(success=True, message="File uploaded and shared!")
                else:
                    return jsonify(success=False, message="No such user")
            else:
                return jsonify(success=False, message="You're not signed in")
        else:
            return jsonify(success=False, message="Wrong input data")

    return ""
Example #41
0
def get_user_data_by_email():
    email = request.values.get("email")
    token = request.values.get("token")
    userFromToken = database_helper.get_user_by_token(token)
    if len(userFromToken) > 0:
        userdata = database_helper.get_user_by_email(email)
        if len(userdata) > 0:
            return jsonify(
                success=True,
                message="Data successfully retrieved",
                email=userdata[0][0],
                fname=userdata[0][2],
                lname=userdata[0][3],
                gender=userdata[0][4],
                city=userdata[0][5],
                country=userdata[0][6],
            )
        else:
            return jsonify(success=False, message="No such user")
    else:
        return jsonify(success=False, message="You're not signed in")
Example #42
0
def sign_in(email, password):
    if '@' in email :
        userdata = database_helper.get_user_by_email(email)
        if userdata != None and userdata[6] == password :
            ws = websockets.get(email, None)
            if ws != None :
                ws["websocket"].send(json.dumps({"messagetype": "forcedLogout", "message": "Forced logout: User signed in from other location."}))
                database_helper.remove_active(database_helper.get_active_by_email(email))
                print "sign_in(): releasing sema for [" + email + "]"
                ws["sema"].release()
                del websockets[email]
                
            if database_helper.get_active_by_email(email) != None :
                database_helper.remove_active_by_email(email)
                
            print "sign_in(): Generating token."
            token = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(36))
            success = database_helper.add_active(token, email)
            print "sign_in(): database_helper.add_active(token, email) = " + str(success)
            if success == True :
                return json.dumps({"success": True, "message": "Successfully signed in.", "data": token})
    return json.dumps({"success": False, "message": "Wrong username or password."})
Example #43
0
def get_user_data_by_token():
    token = request.values.get("token")
    user = database_helper.get_user_by_token(token)

    if len(user) > 0:
        userdata = database_helper.get_user_by_email(user[0][0])
        if len(userdata) > 0:
            return jsonify(
                success=True,
                message="Data successfully retrieved",
                email=userdata[0][0],
                fname=userdata[0][2],
                lname=userdata[0][3],
                gender=userdata[0][4],
                city=userdata[0][5],
                country=userdata[0][6],
            )
        else:
            return jsonify(success=False, message="Something went terribly wrong, this shouldn't happen")

    else:
        return jsonify(success=False, message="You're not logged in")
Example #44
0
def get_user_data_helper(token, email):
    userdata = database_helper.get_user_by_email(email)
        
    if database_helper.get_active(token) == None :
        return json.dumps({"success": False, "message": "You are not signed in."});

    if userdata == None :
        return json.dumps({"success": False, "message": "No such user."})
    else :
        if database_helper.increment_user_visits(email) :
            print "get_user_data_by_email(): increment_user_visits() succeeded"
        else:
            print "get_user_data_by_email(): increment_user_visits() failed"
        update_visit_count()
        data = {
                "email": userdata[0],
                "firstname": userdata[1],
                "familyname": userdata[2],
                "gender": userdata[3], 
                "city": userdata[4],
                "country": userdata[5]}
        return json.dumps({"success": True, "message": "User data retrieved.", "data": data})
Example #45
0
def get_user_data_by_email():
    myEmail = request.json['myEmail']
    email = request.json['email']
    hashedData = request.headers.get('hashedData')
    serverHash = server_hash(email + myEmail, myEmail)
    if (serverHash == hashedData):
        user = database_helper.get_user_by_email(email)
        if (user != None):
            return json.dumps({
                'success': True,
                'message': 'User data received',
                'data': user
            })
        else:
            return json.dumps({
                'success': False,
                'message': 'User does not exist'
            })
    else:
        return json.dumps({
            'success': False,
            'message': 'Authentication failed'
        })
Example #46
0
def sign_up():
    email = request.form['email']
    password = request.form['password']
    firstname = request.form['firstname']
    familyname = request.form['familyname']
    gender = request.form['gender']
    city = request.form['city']
    country = request.form['country']

    #Is there already an user?
    user = database_helper.get_user_by_email(email)
    
    #If there isn't, signup
    if (user is None):
        if (type(email) is unicode
            and type(password) is unicode 
            and type(firstname) is unicode 
            and type(familyname) is unicode 
            and type(gender) is unicode
            and type(city) is unicode 
            and type(country) is unicode):

            database_helper.add_user(email,
                                     password,
                                     firstname,
                                     familyname,
                                     gender,
                                     city,
                                     country);
                  
            return jsonify(success = True, message = "Successfully created a new user.")
            
        else:
            return jsonify(success = False, message = "Form data missing or incorrect type.")

    else:
        return jsonify(success = False, message = "User already exists.")
Example #47
0
def get_user_data_by_token():
    email = request.form['email']
    hash = request.form['hash']
    user = database_helper.get_logged_in_user_by_email(email)

    if user:
        params = "&email=" + email + "&hash="
        token = user[1]
        result = database_helper.get_user_by_email(email)
        if check_hash(hash, params, token):
            found_user = {
                'email': result[0],
                'firstname': result[2],
                'familyname': result[3],
                'gender': result[4],
                'city': result[5],
                'country': result[6]
            }
            return_code = create_return_code(True, 'User found', found_user)
        else:
            return_code = create_return_code(False, 'Bad token')
    else:
        return_code = create_return_code(False, 'User not found')
    return json.dumps(return_code)
Example #48
0
def email_check(email):
    return database_helper.get_user_by_email(email)