Skip to content

Commit 51c5e89

Browse files
authored
Add external Keycloak functionality (#883)
* change keycloak auth and token requests for external keycloak functionality * remove debugging prints and add back scheme to CLOWDER_URL * add related env vars to docker-compose * fix formatting * change auth_redirect_uri to use API vars
1 parent bc85a42 commit 51c5e89

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

backend/app/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
from typing import List
23

34
from pydantic import BaseSettings, AnyHttpUrl
@@ -43,6 +44,7 @@ class Settings(BaseSettings):
4344
auth_base = "http://localhost:8080"
4445
auth_realm = "clowder"
4546
auth_client_id = "clowder2-backend"
47+
auth_redirect_uri = f"{API_HOST}{API_V2_STR}/auth"
4648
auth_url = f"{auth_base}/keycloak/realms/{auth_realm}/protocol/openid-connect/auth?client_id={auth_client_id}&response_type=code"
4749
oauth2_scheme_auth_url = f"{auth_base}/auth/realms/{auth_realm}/protocol/openid-connect/auth?client_id={auth_client_id}&response_type=code"
4850
# scope=openid email&redirect_uri=http://<domain.com>/<redirect-path>&kc_locale=<two-digit-lang-code>

backend/app/routers/keycloak.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
from app.models.tokens import TokenDB
1919
from app.models.users import UserIn, UserDB
20+
from secrets import token_urlsafe
2021

2122
router = APIRouter()
2223
security = HTTPBearer()
@@ -32,7 +33,13 @@ async def register() -> RedirectResponse:
3233
@router.get("/login")
3334
async def login() -> RedirectResponse:
3435
"""Redirect to keycloak login page."""
35-
return RedirectResponse(settings.auth_url)
36+
return RedirectResponse(
37+
keycloak_openid.auth_url(
38+
redirect_uri=settings.auth_redirect_uri,
39+
scope="openid email",
40+
state=token_urlsafe(32)
41+
)
42+
)
3643

3744

3845
@router.get("/logout")
@@ -100,15 +107,12 @@ async def auth(code: str) -> RedirectResponse:
100107
"""Redirect endpoint Keycloak redirects to after login."""
101108
logger.info(f"In /api/v2/auth")
102109
# get token from Keycloak
103-
payload = (
104-
f"grant_type=authorization_code&code={code}"
105-
f"&redirect_uri={settings.auth_url}&client_id={settings.auth_client_id}"
110+
token_body = keycloak_openid.token(
111+
grant_type="authorization_code",
112+
code=code,
113+
redirect_uri=settings.auth_redirect_uri,
106114
)
107-
headers = {"Content-Type": "application/x-www-form-urlencoded"}
108-
token_response = requests.request(
109-
"POST", settings.auth_token_url, data=payload, headers=headers
110-
)
111-
token_body = json.loads(token_response.content)
115+
112116
access_token = token_body["access_token"]
113117

114118
# create user in db if it doesn't already exist; get the user_id
@@ -154,9 +158,12 @@ async def auth(code: str) -> RedirectResponse:
154158

155159
# redirect to frontend
156160
auth_url = f"{settings.frontend_url}/auth"
161+
157162
response = RedirectResponse(url=auth_url)
163+
158164
response.set_cookie("Authorization", value=f"Bearer {access_token}")
159165
logger.info(f"Authenticated by keycloak. Redirecting to {auth_url}")
166+
160167
return response
161168

162169

deployments/kubernetes/charts/clowder2/templates/backend/deployment.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ spec:
6464
value: http://{{ .Values.hostname }}
6565
- name: auth_base
6666
value: $(CLOWDER2_URL)
67+
- name: auth_realm
68+
value: {{ .Values.auth.realm }}
69+
- name: auth_client_id
70+
value: {{ .Values.auth.clientID }}
71+
- name: auth_redirect_uri
72+
value: {{ .Values.auth.redirectURI }}
6773
- name: auth_url
6874
value: $(CLOWDER2_URL)/keycloak/realms/clowder/protocol/openid-connect/auth?client_id=clowder2-backend&response_type=code
6975
- name: oauth2_scheme_auth_url
@@ -73,7 +79,7 @@ spec:
7379
- name: auth_token_url
7480
value: http://{{ include "clowder2.name" .}}-keycloak-headless:8080/keycloak/realms/clowder/protocol/openid-connect/token
7581
- name: auth_server_url
76-
value: http://{{ include "clowder2.name" .}}-keycloak-headless:8080/keycloak/
82+
value: {{ .Values.auth.server }}
7783
- name: keycloak_base
7884
value: $(CLOWDER2_URL)/api
7985
- name: frontend_url

deployments/kubernetes/charts/clowder2/values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
hostname: clowder2.localhost
22

3+
auth:
4+
server: clowder2.localhost/keycloak/
5+
realm: clowder
6+
clientID: clowder2-backend
7+
redirectURI: clowder2.localhost/api/v2/auth
8+
39
imagePullSecrets: []
410
nameOverride: ""
511
fullnameOverride: ""

docker-compose.test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ services:
4949
RABBITMQ_HOST: rabbitmq:15672
5050
elasticsearch_url: http://elasticsearch:9200
5151
auth_base: http://localhost
52+
auth_realm: clowder
53+
auth_client_id: clowder2-backend
54+
auth_redirect_uri: http://localhost:80/api/v2/auth
5255
auth_url: http://localhost/keycloak/realms/clowder/protocol/openid-connect/auth?client_id=clowder2-backend&response_type=code
5356
oauth2_scheme_auth_url: http://keycloak:8080/keycloak/realms/clowder/protocol/openid-connect/auth?client_id=clowder2-backend&response_type=code
5457
auth_register_url: http://localhost/keycloak/realms/clowder/protocol/openid-connect/registrations?client_id=clowder2-backend&response_type=code

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ services:
5353
API_HOST: ${API_HOST:-http://localhost}
5454
elasticsearch_url: http://elasticsearch:9200
5555
auth_base: http://localhost
56+
auth_realm: clowder
57+
auth_client_id: clowder2-backend
58+
auth_redirect_uri: http://localhost:80/api/v2/auth
5659
auth_url: http://localhost/keycloak/realms/clowder/protocol/openid-connect/auth?client_id=clowder2-backend&response_type=code
5760
oauth2_scheme_auth_url: http://keycloak:8080/keycloak/realms/clowder/protocol/openid-connect/auth?client_id=clowder2-backend&response_type=code
5861
auth_register_url: http://localhost/keycloak/realms/clowder/protocol/openid-connect/registrations?client_id=clowder2-backend&response_type=code

0 commit comments

Comments
 (0)