add Kerberos authentication support with proxy headers
This commit is contained in:
1 parent
b8041c52a4
commit
de0781053a
4 files changed
+47
-10
No files matched your search
+30
-5
@@ -7,7 +7,7 @@ from typing import List
|
||||
import time
|
||||
|
||||
import jwt
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi import Depends, HTTPException, Request, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -46,15 +46,40 @@ def create_access_token(token: TokenData):
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def authenticate_user(cfg: BeamlineConfig, form_data: OAuth2PasswordRequestForm) -> str:
|
||||
user_info = pwd.getpwnam(form_data.username)
|
||||
def authenticate_from_proxy_header(request: Request) -> str:
|
||||
"""
|
||||
Extract the pre-authenticated username from the trusted Apache proxy header.
|
||||
Only trust this header when the request originates from localhost (127.0.0.1 / ::1),
|
||||
meaning only the Apache proxy running on the same host can supply it.
|
||||
"""
|
||||
client_host = request.client.host if request.client else None
|
||||
if client_host not in ("127.0.0.1", "::1"):
|
||||
raise AuthenticationException(
|
||||
message="X-Remote-User header is only trusted from the localhost proxy",
|
||||
status_code=401,
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
code=AuthErrorCode.INVALID_TOKEN,
|
||||
)
|
||||
remote_user = request.headers.get("X-Remote-User")
|
||||
if not remote_user:
|
||||
raise AuthenticationException(
|
||||
message="Missing X-Remote-User header from proxy",
|
||||
status_code=401,
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
code=AuthErrorCode.INVALID_TOKEN,
|
||||
)
|
||||
return remote_user
|
||||
|
||||
|
||||
def authenticate_user(cfg: BeamlineConfig, username: str) -> str:
|
||||
user_info = pwd.getpwnam(username)
|
||||
|
||||
groups = os.getgrouplist(user_info.pw_name, user_info.pw_gid)
|
||||
supplementary_groups = [grp.getgrgid(g).gr_name.lower() for g in groups]
|
||||
super_user = form_data.username in SUPER_USERS
|
||||
super_user = username in SUPER_USERS
|
||||
pgroups = [group for group in supplementary_groups if group.startswith('p')]
|
||||
staff = "unx-mxgroup" in supplementary_groups or "unx-sls_mx" in supplementary_groups or super_user
|
||||
token = TokenData(sub=form_data.username,
|
||||
token = TokenData(sub=username,
|
||||
pgroups=pgroups,
|
||||
staff=staff,
|
||||
session=cfg.generate_session())
|
||||
|
||||
Reference in new issue
Block a user