1
1
"""FastAPI application using PGStac."""
2
2
3
3
from contextlib import asynccontextmanager
4
+ from typing import Annotated , Any , Dict
4
5
6
+ import jwt
5
7
from eoapi .stac .config import ApiSettings , TilesApiSettings
6
8
from eoapi .stac .extension import TiTilerExtension
7
9
from eoapi .stac .extension import extensions_map as PgStacExtensions
8
- from fastapi import FastAPI
10
+ from fastapi import FastAPI , HTTPException , Security , security , status
9
11
from fastapi .responses import ORJSONResponse
10
12
from stac_fastapi .api .app import StacApi
11
13
from stac_fastapi .api .models import create_get_request_model , create_post_request_model
19
21
from starlette .templating import Jinja2Templates
20
22
from starlette_cramjam .middleware import CompressionMiddleware
21
23
24
+ from .auth import KeycloakAuth
25
+
22
26
try :
23
27
from importlib .resources import files as resources_files # type: ignore
24
28
except ImportError :
32
36
tiles_settings = TilesApiSettings ()
33
37
settings = Settings ()
34
38
39
+ keycloak = KeycloakAuth (
40
+ realm = "eoapi" ,
41
+ client_id = "stac-api" ,
42
+ host = "http://localhost:8080" ,
43
+ internal_host = "http://keycloak:8080" ,
44
+ )
45
+
35
46
36
47
@asynccontextmanager
37
48
async def lifespan (app : FastAPI ):
@@ -54,7 +65,15 @@ async def lifespan(app: FastAPI):
54
65
GETModel = create_get_request_model (extensions )
55
66
56
67
api = StacApi (
57
- app = FastAPI (title = api_settings .name , lifespan = lifespan ),
68
+ app = FastAPI (
69
+ title = api_settings .name ,
70
+ lifespan = lifespan ,
71
+ swagger_ui_init_oauth = {
72
+ "appName" : "eoAPI" ,
73
+ "clientId" : keycloak .client_id ,
74
+ "usePkceWithAuthorizationCodeGrant" : True ,
75
+ },
76
+ ),
58
77
title = api_settings .name ,
59
78
description = api_settings .name ,
60
79
settings = settings ,
@@ -84,10 +103,18 @@ async def lifespan(app: FastAPI):
84
103
85
104
86
105
@app .get ("/index.html" , response_class = HTMLResponse )
87
- async def viewer_page (request : Request ):
106
+ async def viewer_page (
107
+ request : Request , token : Annotated [str , Security (keycloak .scheme )]
108
+ ):
88
109
"""Search viewer."""
89
110
return templates .TemplateResponse (
90
111
"stac-viewer.html" ,
91
112
{"request" : request , "endpoint" : str (request .url ).replace ("/index.html" , "" )},
92
113
media_type = "text/html" ,
93
114
)
115
+
116
+
117
+ @app .get ("/user" , tags = ["auth" ])
118
+ def get_user (user_token : Annotated [Dict [Any , Any ], Security (keycloak .user_validator )]):
119
+ """View auth token."""
120
+ return user_token
0 commit comments