Repository: busykoala/fastapi-opa (language: python)

Issue:
Unable to load /docs and /redoc
**Describe the bug**
I tried to follow your example for OIDC + OPA and I'm unable to see the documentation. I suspect it is because `fastapi-opa` is causing a redirect instead of returning the openapi.json (see the log below). Maybe it is redirecting to login?

**To Reproduce**
Follow this guide https://github.com/busykoala/fastapi-opa/wiki#dev-setup and try to access /docs or /redoc

**Expected behavior**
Expected to be able to use /docs interactively and login like in the fastapi examples like here https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/
![image](https://user-images.githubusercontent.com/1872050/132914324-aa188fc1-71f9-4b23-833a-432d989e03f4.png)

**Screenshots**
```
test-fastapi-keycloak_1  | INFO:     127.0.0.1:42760 - "GET /redoc?session_state=bb916ff5-4570-41c1-8bea-1a28b1ba767e&code=d2170d1c-feae-4150-8274-7273924d5347.bb916ff5-4570-41c1-8bea-1a28b1ba767e.f70a6f2b-5f7f-4ce0-8c47-e80b58c40fbc HTTP/1.1" 200 OK
test-fastapi-keycloak_1  | INFO:     127.0.0.1:42760 - "GET /openapi.json HTTP/1.1" 307 Temporary Redirect
```

**Additional context**
Possibly related, I'm not sure that we need to save anything as a query parameter. Here is an alternative implementation that I've used before for auth0 https://github.com/dorinclisu/fastapi-auth0 that does not modify the url. Maybe this is a limitation enforced by OPA though?



Discussion / hints:
Thanks for reporting the bug. I'll try to address the issue asap, but it might take a while till I get the time. Feel free to open a PR to accelerate the process.

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 32f79b5..e454e08 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
 
 - Allow updating fastapi >= 0.66 and force updating because of CVE in
   versions < 0.65.2.
+- Stop protecting the openapi endpoints by this middleware.
 
 ## [1.3.0] - 2021-08-08
 
diff --git a/fastapi_opa/opa/opa_middleware.py b/fastapi_opa/opa/opa_middleware.py
index 47f5473..304b22f 100644
--- a/fastapi_opa/opa/opa_middleware.py
+++ b/fastapi_opa/opa/opa_middleware.py
@@ -27,6 +27,12 @@ async def __call__(
         self, scope: Scope, receive: Receive, send: Send
     ) -> None:
         request = Request(scope, receive, send)
+        # allow openapi endpoints without authentication
+        if any(
+            request.url.path == endpoint
+            for endpoint in ["/openapi.json", "/docs", "/redoc"]
+        ):
+            return await self.app(scope, receive, send)
         # authenticate user or get redirect to identity provider
         try:
             user_info_or_auth_redirect = (


Test patch:
diff --git a/tests/test_opa.py b/tests/test_opa.py
index c7f03e5..6e00b38 100644
--- a/tests/test_opa.py
+++ b/tests/test_opa.py
@@ -1,6 +1,7 @@
 import json
 
 import pytest
+from lxml import html
 from mock import patch
 
 from fastapi_opa import OPAConfig
@@ -78,3 +79,23 @@ async def test_function_injection(injected_client):
 
     payload = json.loads(req.call_args_list[0][1].get("data")).get("input")
     assert expected_payload == payload
+
+
+def test_openapi_docs_endpoint_accessable(client):
+    response = client.get("/docs")
+    doc = html.fromstring(response.content)
+    title = doc.xpath(".//title")[0].text
+    assert "FastAPI - Swagger UI" == title
+
+
+def test_openapi_redoc_endpoint_accessable(client):
+    response = client.get("/redoc")
+    doc = html.fromstring(response.content)
+    title = doc.xpath(".//title")[0].text
+    assert "FastAPI - ReDoc" == title
+
+
+def test_openapi_json_endpoint_accessable(client):
+    response = client.get("/openapi.json")
+    title = response.json()["info"]["title"]
+    assert "FastAPI" == title