|
12 | 12 | from src.opencdms_api.config import settings |
13 | 13 | from src.opencdms_api.db import db_session_scope |
14 | 14 | from src.opencdms_api import climsoft_rbac_config |
15 | | -from src.opencdms_api.schema import CurrentUserSchema |
| 15 | +from src.opencdms_api.schema import CurrentUserSchema, CurrentClimsoftUserSchema |
16 | 16 | from opencdms.models.climsoft import v4_1_1_core as climsoft_models |
17 | 17 | from fastapi import Header, Depends |
18 | 18 | from fastapi.security import OAuth2PasswordBearer |
@@ -98,6 +98,23 @@ class ClimsoftRBACMiddleware(AuthMiddleWare): |
98 | 98 | def __init__(self, app: ASGIApp): |
99 | 99 | super().__init__(app) |
100 | 100 |
|
| 101 | + def authenticate_request(self, request: Request): |
| 102 | + authorization_header = request.headers.get("authorization") |
| 103 | + if authorization_header is None: |
| 104 | + raise HTTPException(401, "Unauthorized request") |
| 105 | + scheme, token = get_authorization_scheme_param(authorization_header) |
| 106 | + if scheme.lower() != "bearer": |
| 107 | + raise HTTPException(401, "Invalid authorization header scheme") |
| 108 | + try: |
| 109 | + claims = jwt.decode(token, settings.SURFACE_SECRET_KEY) |
| 110 | + except JWTError: |
| 111 | + raise HTTPException(401, "Unauthorized request") |
| 112 | + username = claims["sub"] |
| 113 | + user = CurrentClimsoftUserSchema(username=username) |
| 114 | + if user is None: |
| 115 | + raise HTTPException(401, "Unauthorized request") |
| 116 | + return user |
| 117 | + |
101 | 118 | async def __call__(self, scope: Scope, receive: Receive, send: Send): |
102 | 119 | request = Request(scope, receive, send) |
103 | 120 | user = None |
@@ -134,7 +151,7 @@ def get_authorized_climsoft_user( |
134 | 151 |
|
135 | 152 | username = claims["sub"] |
136 | 153 |
|
137 | | - user = get_user(username) |
| 154 | + user = CurrentClimsoftUserSchema(username=username) |
138 | 155 |
|
139 | 156 | if user is None: |
140 | 157 | raise HTTPException(401, "Unauthorized request") |
|
0 commit comments