Skip to content

Commit 5169ee2

Browse files
refactor: Moved login for pilots into token file
1 parent 12da5c0 commit 5169ee2

File tree

7 files changed

+116
-114
lines changed

7 files changed

+116
-114
lines changed

diracx-client/src/diracx/client/_generated/aio/operations/_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ async def refresh_pilot_tokens(
902902
) -> _models.TokenResponse:
903903
"""Refresh Pilot Tokens.
904904
905-
Endpoint where a pilot can exchange a refresh token against a token.
905+
Endpoint where a pilot can exchange a refresh token for a token.
906906
907907
:keyword refresh_token: Required.
908908
:paramtype refresh_token: str

diracx-client/src/diracx/client/_generated/operations/_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ def refresh_pilot_tokens(
16121612
) -> _models.TokenResponse:
16131613
"""Refresh Pilot Tokens.
16141614
1615-
Endpoint where a pilot can exchange a refresh token against a token.
1615+
Endpoint where a pilot can exchange a refresh token for a token.
16161616
16171617
:keyword refresh_token: Required.
16181618
:paramtype refresh_token: str

diracx-logic/src/diracx/logic/auth/token.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ async def generate_pilot_tokens(
427427
config=config,
428428
settings=settings,
429429
available_properties=available_properties,
430-
pilot_exchange=legacy_exchange,
430+
pilot_exchange=True,
431+
legacy_exchange=legacy_exchange,
431432
refresh_token_expire_minutes=refresh_token_expire_minutes,
432433
include_refresh_token=include_refresh_token,
433434
)

diracx-routers/src/diracx/routers/auth/pilot.py

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -9,128 +9,22 @@
99
)
1010

1111
from diracx.core.exceptions import (
12-
AuthorizationError,
13-
InvalidCredentialsError,
1412
PilotAlreadyExistsError,
15-
PilotNotFoundError,
1613
)
17-
from diracx.core.models import TokenResponse
1814
from diracx.logic.auth.pilot import (
1915
add_pilot_credentials,
2016
register_new_pilots,
21-
try_login,
2217
)
23-
from diracx.logic.auth.token import create_token, generate_pilot_tokens
2418
from diracx.logic.pilots.utils import get_pilot_ids_from_references
25-
from diracx.routers.pilots.access_policies import RegisteredPilotAccessPolicyCallable
2619

2720
from ..dependencies import (
28-
AuthDB,
2921
AuthSettings,
30-
AvailableSecurityProperties,
31-
Config,
3222
PilotAgentsDB,
3323
)
3424
from ..fastapi_classes import DiracxRouter
3525
from ..utils.users import AuthorizedUserInfo, verify_dirac_access_token
3626

37-
router = DiracxRouter(require_auth=False)
38-
39-
40-
@router.post("/pilot-login")
41-
async def pilot_login(
42-
pilot_db: PilotAgentsDB,
43-
auth_db: AuthDB,
44-
pilot_job_reference: str,
45-
pilot_secret: str,
46-
config: Config,
47-
settings: AuthSettings,
48-
available_properties: AvailableSecurityProperties,
49-
) -> TokenResponse:
50-
"""Endpoint without policy, the pilot uses only its secret."""
51-
try:
52-
await try_login(
53-
pilot_reference=pilot_job_reference,
54-
pilot_db=pilot_db,
55-
pilot_secret=pilot_secret,
56-
)
57-
except AuthorizationError as e:
58-
raise HTTPException(
59-
status_code=status.HTTP_401_UNAUTHORIZED, detail=e.detail
60-
) from e
61-
except PilotNotFoundError as e:
62-
raise HTTPException(
63-
status_code=status.HTTP_401_UNAUTHORIZED,
64-
detail="bad pilot_id / pilot_secret",
65-
) from e
66-
67-
try:
68-
access_token, refresh_token = await generate_pilot_tokens(
69-
pilot_db=pilot_db,
70-
auth_db=auth_db,
71-
pilot_job_reference=pilot_job_reference,
72-
config=config,
73-
settings=settings,
74-
available_properties=available_properties,
75-
)
76-
except ValueError as e:
77-
raise HTTPException(
78-
status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)
79-
) from e
80-
81-
serialized_access_token = create_token(access_token, settings=settings)
82-
83-
serialized_refresh_token = create_token(refresh_token, settings=settings)
84-
85-
return TokenResponse(
86-
access_token=serialized_access_token,
87-
expires_in=settings.access_token_expire_minutes * 60,
88-
refresh_token=serialized_refresh_token,
89-
)
90-
91-
92-
@router.post("/pilot-refresh-token")
93-
async def refresh_pilot_tokens(
94-
pilot_db: PilotAgentsDB,
95-
auth_db: AuthDB,
96-
config: Config,
97-
settings: AuthSettings,
98-
available_properties: AvailableSecurityProperties,
99-
check_permissions: RegisteredPilotAccessPolicyCallable,
100-
refresh_token: str,
101-
pilot_info: Annotated[AuthorizedUserInfo, Depends(verify_dirac_access_token)],
102-
) -> TokenResponse:
103-
"""Endpoint where a pilot can exchange a refresh token against a token."""
104-
await check_permissions()
105-
106-
try:
107-
new_access_token, new_refresh_token = await generate_pilot_tokens(
108-
pilot_db=pilot_db,
109-
auth_db=auth_db,
110-
pilot_job_reference=pilot_info.preferred_username,
111-
config=config,
112-
settings=settings,
113-
available_properties=available_properties,
114-
refresh_token=refresh_token,
115-
)
116-
except InvalidCredentialsError as e:
117-
raise HTTPException(
118-
status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e)
119-
) from e
120-
except ValueError as e:
121-
raise HTTPException(
122-
status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)
123-
) from e
124-
125-
serialized_access_token = create_token(new_access_token, settings=settings)
126-
127-
serialized_refresh_token = create_token(new_refresh_token, settings=settings)
128-
129-
return TokenResponse(
130-
access_token=serialized_access_token,
131-
expires_in=settings.access_token_expire_minutes * 60,
132-
refresh_token=serialized_refresh_token,
133-
)
27+
router = DiracxRouter()
13428

13529

13630
@router.post("/register-new-pilots")

diracx-routers/src/diracx/routers/auth/token.py

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,39 @@
99
from fastapi import Depends, Form, Header, HTTPException, status
1010

1111
from diracx.core.exceptions import (
12+
AuthorizationError,
1213
DiracHttpResponseError,
1314
ExpiredFlowError,
1415
InvalidCredentialsError,
1516
PendingAuthorizationError,
17+
PilotNotFoundError,
1618
)
1719
from diracx.core.models import (
1820
AccessTokenPayload,
1921
GrantType,
2022
RefreshTokenPayload,
2123
TokenResponse,
2224
)
23-
from diracx.logic.auth.token import create_token
25+
from diracx.logic.auth.pilot import (
26+
try_login,
27+
)
28+
from diracx.logic.auth.token import create_token, generate_pilot_tokens
2429
from diracx.logic.auth.token import get_oidc_token as get_oidc_token_bl
2530
from diracx.logic.auth.token import (
2631
perform_legacy_exchange as perform_legacy_exchange_bl,
2732
)
2833
from diracx.routers.access_policies import BaseAccessPolicy
34+
from diracx.routers.pilots.access_policies import RegisteredPilotAccessPolicyCallable
2935

30-
from ..dependencies import AuthDB, AuthSettings, AvailableSecurityProperties, Config
36+
from ..dependencies import (
37+
AuthDB,
38+
AuthSettings,
39+
AvailableSecurityProperties,
40+
Config,
41+
PilotAgentsDB,
42+
)
3143
from ..fastapi_classes import DiracxRouter
44+
from ..utils.users import AuthorizedUserInfo, verify_dirac_access_token
3245

3346
router = DiracxRouter(require_auth=False)
3447

@@ -250,3 +263,97 @@ async def perform_legacy_exchange(
250263
return await mint_token(
251264
access_payload, refresh_payload, None, all_access_policies, settings
252265
)
266+
267+
268+
@router.post("/pilot-login")
269+
async def pilot_login(
270+
pilot_db: PilotAgentsDB,
271+
auth_db: AuthDB,
272+
pilot_job_reference: str,
273+
pilot_secret: str,
274+
config: Config,
275+
settings: AuthSettings,
276+
available_properties: AvailableSecurityProperties,
277+
all_access_policies: Annotated[
278+
dict[str, BaseAccessPolicy], Depends(BaseAccessPolicy.all_used_access_policies)
279+
],
280+
) -> TokenResponse:
281+
"""Endpoint without policy, the pilot uses only its secret."""
282+
try:
283+
await try_login(
284+
pilot_reference=pilot_job_reference,
285+
pilot_db=pilot_db,
286+
pilot_secret=pilot_secret,
287+
)
288+
except AuthorizationError as e:
289+
raise HTTPException(
290+
status_code=status.HTTP_401_UNAUTHORIZED, detail=e.detail
291+
) from e
292+
except PilotNotFoundError as e:
293+
raise HTTPException(
294+
status_code=status.HTTP_401_UNAUTHORIZED,
295+
detail="bad pilot_id / pilot_secret",
296+
) from e
297+
298+
try:
299+
access_token, refresh_token = await generate_pilot_tokens(
300+
pilot_db=pilot_db,
301+
auth_db=auth_db,
302+
pilot_job_reference=pilot_job_reference,
303+
config=config,
304+
settings=settings,
305+
available_properties=available_properties,
306+
)
307+
except ValueError as e:
308+
raise HTTPException(
309+
status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)
310+
) from e
311+
312+
return await mint_token(
313+
access_token, refresh_token, None, all_access_policies, settings
314+
)
315+
316+
317+
@router.post("/pilot-refresh-token")
318+
async def refresh_pilot_tokens(
319+
pilot_db: PilotAgentsDB,
320+
auth_db: AuthDB,
321+
config: Config,
322+
settings: AuthSettings,
323+
available_properties: AvailableSecurityProperties,
324+
check_permissions: RegisteredPilotAccessPolicyCallable,
325+
refresh_token: str,
326+
pilot_info: Annotated[AuthorizedUserInfo, Depends(verify_dirac_access_token)],
327+
all_access_policies: Annotated[
328+
dict[str, BaseAccessPolicy], Depends(BaseAccessPolicy.all_used_access_policies)
329+
],
330+
) -> TokenResponse:
331+
"""Endpoint where a pilot can exchange a refresh token for a token."""
332+
await check_permissions()
333+
334+
try:
335+
new_access_token, new_refresh_token = await generate_pilot_tokens(
336+
pilot_db=pilot_db,
337+
auth_db=auth_db,
338+
pilot_job_reference=pilot_info.preferred_username,
339+
config=config,
340+
settings=settings,
341+
available_properties=available_properties,
342+
refresh_token=refresh_token,
343+
)
344+
except InvalidCredentialsError as e:
345+
raise HTTPException(
346+
status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e)
347+
) from e
348+
except ValueError as e:
349+
raise HTTPException(
350+
status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)
351+
) from e
352+
353+
return await mint_token(
354+
new_access_token,
355+
new_refresh_token,
356+
refresh_token,
357+
all_access_policies,
358+
settings,
359+
)

extensions/gubbins/gubbins-client/src/gubbins/client/_generated/aio/operations/_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ async def refresh_pilot_tokens(
902902
) -> _models.TokenResponse:
903903
"""Refresh Pilot Tokens.
904904
905-
Endpoint where a pilot can exchange a refresh token against a token.
905+
Endpoint where a pilot can exchange a refresh token for a token.
906906
907907
:keyword refresh_token: Required.
908908
:paramtype refresh_token: str

extensions/gubbins/gubbins-client/src/gubbins/client/_generated/operations/_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ def refresh_pilot_tokens(
16121612
) -> _models.TokenResponse:
16131613
"""Refresh Pilot Tokens.
16141614
1615-
Endpoint where a pilot can exchange a refresh token against a token.
1615+
Endpoint where a pilot can exchange a refresh token for a token.
16161616
16171617
:keyword refresh_token: Required.
16181618
:paramtype refresh_token: str

0 commit comments

Comments
 (0)