def test_get_guest_user_bad_audience(self): now = time.time() user = {"username": "******"} resources = [{"some": "resource"}] aud = get_url_host() claims = { "user": user, "resources": resources, "rls_rules": [], # standard jwt claims: "aud": "bad_audience", "iat": now, # issued at "type": "guest", } token = jwt.encode( claims, self.app.config["GUEST_TOKEN_JWT_SECRET"], algorithm=self.app.config["GUEST_TOKEN_JWT_ALGO"], ) fake_request = FakeRequest() fake_request.headers[ current_app.config["GUEST_TOKEN_HEADER_NAME"]] = token guest_user = security_manager.get_guest_user_from_request(fake_request) self.assertRaisesRegex(jwt.exceptions.InvalidAudienceError, "Invalid audience") self.assertIsNone(guest_user)
def test_create_guest_access_token(self, get_time_mock): now = time.time() get_time_mock.return_value = now # so we know what it should = user = {"username": "******"} resources = [{"some": "resource"}] rls = [{"dataset": 1, "clause": "access = 1"}] token = security_manager.create_guest_access_token( user, resources, rls) aud = get_url_host() # unfortunately we cannot mock time in the jwt lib decoded_token = jwt.decode( token, self.app.config["GUEST_TOKEN_JWT_SECRET"], algorithms=[self.app.config["GUEST_TOKEN_JWT_ALGO"]], audience=aud, ) self.assertEqual(user, decoded_token["user"]) self.assertEqual(resources, decoded_token["resources"]) self.assertEqual(now, decoded_token["iat"]) self.assertEqual(aud, decoded_token["aud"]) self.assertEqual("guest", decoded_token["type"]) self.assertEqual( now + (self.app.config["GUEST_TOKEN_JWT_EXP_SECONDS"]), decoded_token["exp"], )
def test_post_guest_token_authorized(self): self.dash = db.session.query(Dashboard).filter_by( slug="births").first() self.embedded = EmbeddedDAO.upsert(self.dash, []) self.login(username="******") user = { "username": "******", "first_name": "Bob", "last_name": "Also Bob" } resource = {"type": "dashboard", "id": str(self.embedded.uuid)} rls_rule = {"dataset": 1, "clause": "1=1"} params = {"user": user, "resources": [resource], "rls": [rls_rule]} response = self.client.post(self.uri, data=json.dumps(params), content_type="application/json") self.assert200(response) token = json.loads(response.data)["token"] decoded_token = jwt.decode( token, self.app.config["GUEST_TOKEN_JWT_SECRET"], audience=get_url_host(), algorithms=["HS256"], ) self.assertEqual(user, decoded_token["user"]) self.assertEqual(resource, decoded_token["resources"][0])
def test_post_guest_token_authorized(self): self.login(username="******") user = { "username": "******", "first_name": "Bob", "last_name": "Also Bob" } resource = {"type": "dashboard", "id": "blah"} rls_rule = {"dataset": 1, "clause": "1=1"} params = {"user": user, "resources": [resource], "rls": [rls_rule]} response = self.client.post(self.uri, data=json.dumps(params), content_type="application/json") self.assert200(response) token = json.loads(response.data)["token"] decoded_token = jwt.decode(token, self.app.config["GUEST_TOKEN_JWT_SECRET"], audience=get_url_host()) self.assertEqual(user, decoded_token["user"]) self.assertEqual(resource, decoded_token["resources"][0])
def _get_guest_token_jwt_audience() -> str: audience = current_app.config["GUEST_TOKEN_JWT_AUDIENCE"] or get_url_host() if callable(audience): audience = audience() return audience