Skip to content

Commit cdd3d44

Browse files
committed
feat: start adding routes that users can use directly with their mTLS cert (also useful for testing)
1 parent fbfd4a2 commit cdd3d44

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/rmmtxauthz/schema/userdirect.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Schemas for direct user mTLS routes"""
2+
3+
from pydantic import Field, BaseModel, ConfigDict
4+
5+
6+
class UserCredentials(BaseModel):
7+
"""Request to add product interoperability."""
8+
9+
username: str = Field(description="MediaMTX username")
10+
password: str = Field(description="MediaMTX password")
11+
12+
model_config = ConfigDict(
13+
extra="forbid",
14+
json_schema_extra={
15+
"examples": [
16+
{
17+
"username": "KOIRA11a",
18+
"password": "SomethingRandom", # pragma: allowlist secret
19+
},
20+
],
21+
},
22+
)

src/rmmtxauthz/web/application.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .instructions import router as irouter
1818
from .interop import interoprouter
1919
from .health import hrouter
20+
from .userdirect import userrouter
2021

2122
LOGGER = logging.getLogger(__name__)
2223

@@ -46,6 +47,7 @@ def get_app_no_init() -> FastAPI:
4647
app.include_router(interoprouter, prefix="/api/v1/interop", tags=["interop"])
4748
app.include_router(crudrouter, prefix="/api/v1/users", tags=["users"])
4849
app.include_router(mtxrouter, prefix="/api/v1/mediamtx", tags=["mediamtx"])
50+
app.include_router(userrouter, prefix="/api/v1/direct", tags=["directuser"])
4951
app.include_router(irouter, prefix="/api/v1", tags=["instructions"])
5052
app.include_router(hrouter, prefix="/api/v1", tags=["health"])
5153
return app

src/rmmtxauthz/web/userdirect.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""APIs usable directly by the user with mTLS"""
2+
3+
import logging
4+
5+
from fastapi import APIRouter, Depends, Request
6+
from libpvarki.middleware import MTLSHeader
7+
8+
from ..db.user import User
9+
from ..schema.userdirect import UserCredentials
10+
11+
LOGGER = logging.getLogger(__name__)
12+
13+
userrouter = APIRouter(dependencies=[Depends(MTLSHeader(auto_error=True))])
14+
15+
16+
def get_callsign(request: Request) -> str:
17+
"""extract callsign from metadata"""
18+
payload = request.state.mtlsdn
19+
return str(payload.get("CN"))
20+
21+
22+
@userrouter.get("/credentials", response_model=UserCredentials)
23+
async def get_credentials(request: Request) -> UserCredentials:
24+
"""Get my MediaMTX credentials"""
25+
user = await User.by_username(get_callsign(request))
26+
return UserCredentials(username=user.username, password=user.mtxpassword)

0 commit comments

Comments
 (0)