Example #1
0
def user(uid=None):
    key = request.headers.get('x-key')
    user = KeyStore.search(key)

    if uid:
        if not user:
            abort(401)

        if uid=="me":
            uid = user["_id"]

        res = model.users.get(uid)
        return jsonify(status="ok", message="", data=res)

    if request.method == 'GET':
        if not user:
            abort(401)

        search = request.args.get("search", "")
        skip = request.args.get("skip", 0)
        limit = request.args.get("limit", 10)

        res = model.users.search(search, skip=skip, limit=limit)

        return jsonify(status="ok", message="", data=res)

    elif request.method == 'POST':
        token = request.form["token"]
        user = model.users.login(token)

        if user:
            key = GenerateKey()
            KeyStore.save(key, user)
            user["key"] = key

            return jsonify(status="ok", message="", data=user)
        else:
            model.users.add(token)
            return jsonify(status="ok", message="news", data={})

    elif request.method == 'PUT':
        token = request.form["token"]
        name = request.form["name"]
        fullname = request.form["fullname"]
        image = request.form["image"]

        if model.users.update(token, name, fullname, image):
            return jsonify(status="ok", message="", data={})
        else:
            abort(404)

    elif request.method == 'DELETE':
        key = request.headers.get('x-key')
        user = KeyStore.search(key)

        if not user:
            abort(401)

        KeyStore.delete(key)
        return jsonify(status="ok", message="", data={})
Example #2
0
def auth():
    if request.method == 'POST': # Login
        username = request.form["username"]
        password = GetHash( request.form["password"] )
        user = auth_db.find_one({
                            "username":username,
                        })
        if not user or not validPasssword(password, user['password']):
            return jsonify(status="ok", message="Authenticate failed", data={}), 401

        key = GenerateKey()
        KeyStore.save(key, user)
        return jsonify(status="ok", message="Welcome back, {0}.".format(username), data={"key":key, "user":user})

    if request.method == 'PUT': # Register
        username = request.form["username"]
        password = GetHash( request.form["password"] )
        fullname = request.form["fullname"]
        email = request.form["email"]
        user = auth_db.find_one({
                            "username":username, 
                        })
        if user:
            return jsonify(status="fail", message="Username {0} already exist".format(username), data={})
        result = auth_db.insert_one({"username":username, "password":password})
        if not result:
            return jsonify(status="fail", data="DB Fail"), 500
        return jsonify(status="ok", message="Welcome {0}".format(username), data={})