def wrapper(*args, **kwars): token = request.headers.get("Authorization") role = Utils.request_role() try: if not token or token.split(" ")[0] != "Bearer": raise InvalidTokenError(token, "Token must be a bearer token", "Invalid token") token_decode = Auth.token_decode(token.split(" ")[1]) session_id = token_decode["token"] exists = Auth.session_exists(session_id, role) if not exists: raise InvalidTokenError( token, "User does not have an active session", "Invalid token") except InvalidTokenError as e: return Utils.response(status=e.status, message="Invalid token", data=e.errors) return f(*args, **kwars)
def post(self): manage = ManageUser() strategy = LoginUserStrategy() try: tokens = manage.login(strategy, request.json) except InvalidParametersError as e: return Utils.response(status=e.status, data=e.errors, message="Invalid parameters") except AuthenticationError as e: return Utils.response(status=e.status, data=e.errors, message="Invalid credentials") return Utils.response_auth(tokens["token_id"], tokens["refresh_token"])
def post(self): manage = ManageUser() strategy = SignupAdminStrategy() try: admin = manage.signup(strategy, request.json) data = AdminCreateSchema().dumps(admin) except InvalidParametersError as e: return Utils.response(status=e.status, data=e.errors, message="Invalid parameters") except UserExistsError as e: return Utils.response(status=e.status, data=e.errors, message="Parameter must be unique") return Utils.response(status=201, data=json.loads(data), message="Request succesfull")
def login(self, credentials: dict) -> dict: schema = AdminLoginSchema() try: credentials = schema.load(credentials) storedAdmin = Admin.query.filter_by(username=credentials["username"]).first() if storedAdmin is None: return None isLogged = Utils.verify_password( storedAdmin.password[0], storedAdmin.password[1], credentials["password"] ) if not isLogged: return None data = AdminCreateSchema().dump(storedAdmin) tokens = Auth.generate_session_token("ADMIN", **data) except ValidationError as e: raise InvalidParametersError(e.data, e.messages, "Error occurred when trying to loggin admin", e) return tokens
def login(self, credentials: dict) -> dict: schema = UserLoginSchema() try: credentials = schema.load(credentials) storedUser = User.query.filter_by(email=credentials["email"]).first() if storedUser is None: return None isLogged = Utils.verify_password( storedUser.password[0], storedUser.password[1], credentials["password"] ) if not isLogged: return None data = UserCreateSchema().dump(storedUser) tokens = Auth.generate_session_token("USER", **data) except ValidationError as e: raise InvalidParametersError(e.data, e.messages, "Error occurred when trying to loggin user", e) return tokens
def get(self): refresh_token = request.headers.get("REFRESH_TOKEN").split(" ")[-1] maneger = ManageUser() strategy = RefreshTokenAdminStrategy() tokens = maneger.refresh(strategy, refresh_token) return Utils.response_auth(tokens["token_id"], tokens["refresh_token"])
def test_check_fail_role(self): with patch('app.src.common.utils.request') as mock_request: mock_request.path = "/hola/*" role = Utils.request_role() self.assertNotIn(role, self.roles) self.assertIsNone(role)
def test_check_role_user(self): with patch("app.src.common.utils.request") as mock_request: mock_request.path = "/user/secure/*" role = Utils.request_role() self.assertIn(role, self.roles) self.assertEqual(role, self.roles[1])
def wrapper(*args, **kwars): token = request.headers.get("REFRESH_TOKEN") try: if not token or token.split(" ")[0] != "Bearer": raise InvalidTokenError(token, "Token must be a bearer token", "Invalid token") exists = Auth.refresh_token_exists(token.split(" ")[1], role) if not exists: raise InvalidTokenError(token, "Invalid refresh token", "Invalid token") except InvalidTokenError as e: return Utils.response(status=e.status, message="Invalid token", data=e.errors) return f(*args, **kwars)
def get(self): token = request.headers.get("Authorization").split(" ")[-1] Auth.delete_session(token) return Utils.response(message="Request succesfull")
def password(self, plain_password): self._salt, self._password = Utils.hash_password(plain_password)
def test_check_hash_and_invalid_plain_password(self): invalid_password = "******" s_salt, s_password = Utils.hash_password(self.password) isCheck = Utils.verify_password(s_salt, s_password, invalid_password) self.assertFalse(isCheck)
def test_check_valid_hash_and_plain_password(self): s_salt, s_password = Utils.hash_password(self.password) isCheck = Utils.verify_password(s_salt, s_password, self.password) self.assertTrue(isCheck)
def test_hash_password_is_not_none(self): salt, password = Utils.hash_password(self.password) self.assertIsNotNone(salt) self.assertIsNotNone(password)