Exemplo n.º 1
0
    def post(self):
        logger.getLogger().debug("POST at /users/login")
        logger.getLogger().debug(request.json)
        try:
            # (shared-server) First ask shared server for credentials validation
            (valid, response) = ServerRequest.validateUser(request.json)

            if not valid:
                logger.getLogger().debug(
                    'Error 418: I\' m a teapot and your credentials are not valid!'
                )
                return ResponseMaker.response_error(response.status_code,
                                                    "Shared server error")
            logger.getLogger().debug(
                "Credentials are valid, server responsed with user")

            user_js = User.UserJSON(response)

            # (token-generation) Generate a new UserToken for that user
            token = TokenGenerator.generateToken(response)

            users_online = MongoController.getCollection("online")

            # (mongodb) If credentials are valid, and user is not here, add it
            for user in users_online.find():
                if user_js["_id"] == user["_id"]:
                    # Found it! Checking refs!
                    logger.getLogger().debug(
                        "User trying to log in already found in the local db")
                    if user_js["_ref"] != user["_ref"]:
                        logger.getLogger().debug(
                            "Login of an user already in bdd. _ref is old")
                        user_js = User.UserUpdateDedicatedFields(user_js, user)
                    else:
                        user_js = user
                    break

            user_js["online"] = True
            users_online.update({"_id": user_js["_id"]}, user_js, upsert=True)

            return ResponseMaker.response_object(constants.SUCCESS,
                                                 ['user', 'token'],
                                                 [user_js, token])
        except Exception as e:
            logger.getLogger().exception(str(e))
            print(str(e))
            return ResponseMaker.response_error(500, "Internal Error")
Exemplo n.º 2
0
 def test_decoded_gives_same_payload(self):
     payload = {"username": "******", "_id": 1}
     token = TokenGenerator.generateToken(payload)
     valid, decoded = TokenGenerator.validateToken(token)
     assert (valid == True)
     assert (decoded == payload)