Skip to content

Commit

Permalink
fix: Make clientId optional in config.yml (#207)
Browse files Browse the repository at this point in the history
fix issue:
#206
  • Loading branch information
duwenxin99 committed Feb 7, 2024
1 parent 26ad5d9 commit 3914d8c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 3 additions & 3 deletions retrieval_service/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from contextlib import asynccontextmanager
from ipaddress import IPv4Address, IPv6Address
from typing import Optional

import yaml
from fastapi import FastAPI
Expand All @@ -32,7 +32,7 @@ class AppConfig(BaseModel):
host: IPv4Address | IPv6Address = IPv4Address("127.0.0.1")
port: int = 8080
datastore: datastore.Config
clientId: str
clientId: Optional[str] = None


def parse_config(path: str) -> AppConfig:
Expand All @@ -53,7 +53,7 @@ async def initialize_datastore(app: FastAPI):


def init_app(cfg: AppConfig) -> FastAPI:
os.environ["CLIENT_ID"] = cfg.clientId
app = FastAPI(lifespan=gen_init(cfg))
app.state.client_id = cfg.clientId
app.include_router(routes)
return app
7 changes: 4 additions & 3 deletions retrieval_service/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ def _ParseUserIdToken(headers: Mapping[str, Any]) -> Optional[str]:
return parts[1]


async def get_user_info(headers: Mapping[str, Any]):
async def get_user_info(request):
headers = request.headers
token = _ParseUserIdToken(headers)
try:
id_info = id_token.verify_oauth2_token(
token, requests.Request(), audience=os.environ["CLIENT_ID"]
token, requests.Request(), audience=request.app.state.client_id
)

return {
Expand Down Expand Up @@ -157,7 +158,7 @@ async def insert_ticket(
departure_time: str,
arrival_time: str,
):
user_info = await get_user_info(request.headers)
user_info = await get_user_info(request)
if user_info is None:
raise HTTPException(
status_code=401,
Expand Down
3 changes: 2 additions & 1 deletion retrieval_service/example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ datastore:
# port: 5432
database: "my_database"
user: "my-user"
password: "my-password"
password: "my-password"
clientId: "my-clientId"

0 comments on commit 3914d8c

Please sign in to comment.