Skip to content

Commit

Permalink
Merge pull request #3 from CSCfi/feature/cors
Browse files Browse the repository at this point in the history
add cors
  • Loading branch information
teemukataja authored Jun 17, 2022
2 parents 782dfcf + 0cbb12f commit 808f6c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Configuration variables are set in [config.json](config.json), which resides at
"url_callback": "http://localhost:8080/callback",
"url_redirect": "http://localhost:8080/frontend",
"scope": "openid",
"cookie_domain": ""
"cookie_domain": "",
"cors_domains": [""]
}
```
The app contacts `url_oidc` on startup and retrieves the `authorization_endpoint`, `token_endpoint`, `revocation_endpoint` and `userinfo_endpoint` values, which are used at `/login`, `/callback`, `/logout` and `/userinfo` respectively.
Expand Down
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from fastapi import FastAPI, Cookie
from fastapi.exceptions import HTTPException
from fastapi.responses import PlainTextResponse, RedirectResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware

# the web app
app = FastAPI()
Expand All @@ -41,6 +42,15 @@
@app.on_event("startup")
async def startup_event():
"""Request OpenID configuration from OpenID provider."""
# add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=CONFIG["cors_domains"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# get missing OIDC configurations
async with httpx.AsyncClient(verify=False) as client:
# request OpenID provider endpoints from their configuration
LOG.debug(f"requesting OpenID configuration from {CONFIG['url_oidc']}")
Expand Down

0 comments on commit 808f6c8

Please sign in to comment.