Skip to content

Commit 1fadf42

Browse files
committed
Return KeyRefreshResponse from primary/secondary instead of tuples
1 parent 03fa2e4 commit 1fadf42

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

soauth/api/login.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,7 @@ async def code(
194194

195195
# Create the codes!
196196
app = await app_service.read_by_id(app_id=login_request.app_id, conn=conn)
197-
(
198-
auth_key,
199-
refresh_key,
200-
auth_key_expires,
201-
refresh_key_expires,
202-
) = await flow_service.primary(
197+
key_content = await flow_service.primary(
203198
user=user, app=app, settings=settings, conn=conn, log=log
204199
)
205200

@@ -218,10 +213,10 @@ async def code(
218213
)
219214

220215
return KeyRefreshResponse(
221-
access_token=auth_key,
222-
refresh_token=refresh_key,
223-
access_token_expires=auth_key_expires,
224-
refresh_token_expires=refresh_key_expires,
216+
access_token=key_content.access_token,
217+
refresh_token=key_content.refresh_token,
218+
access_token_expires=key_content.access_token_expires,
219+
refresh_token_expires=key_content.refresh_token_expires,
225220
redirect=redirect,
226221
)
227222

@@ -250,12 +245,7 @@ async def exchange(
250245
refresh_token = content.refresh_token
251246

252247
try:
253-
(
254-
auth_key,
255-
refresh_key,
256-
auth_key_expires,
257-
refresh_key_expires,
258-
) = await flow_service.secondary(
248+
key_content = await flow_service.secondary(
259249
encoded_refresh_key=refresh_token, settings=settings, conn=conn, log=log
260250
)
261251
except refresh_service.AuthorizationError as e:
@@ -271,12 +261,7 @@ async def exchange(
271261
status_code=status.HTTP_401_UNAUTHORIZED, detail="Refresh token expired"
272262
)
273263

274-
return KeyRefreshResponse(
275-
access_token=auth_key,
276-
refresh_token=refresh_key,
277-
access_token_expires=auth_key_expires,
278-
refresh_token_expires=refresh_key_expires,
279-
)
264+
return key_content
280265

281266

282267
@login_app.post(

soauth/service/flow.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
a new access token.
55
"""
66

7-
from datetime import datetime
87

98
from sqlalchemy.ext.asyncio import AsyncSession
109
from structlog.typing import FilteringBoundLogger
1110

1211
from soauth.config.settings import Settings
12+
from soauth.core.models import KeyRefreshResponse
1313
from soauth.core.uuid import UUID
1414
from soauth.database.app import App
1515
from soauth.database.user import User
@@ -31,7 +31,7 @@ async def primary(
3131
settings: Settings,
3232
conn: AsyncSession,
3333
log: FilteringBoundLogger,
34-
) -> tuple[str, str, datetime, datetime]:
34+
) -> KeyRefreshResponse:
3535
"""
3636
Primary authentication flow - assumes that you just created the 'user'
3737
and that the 'user' has _all required credentials, grants and groups_.
@@ -82,11 +82,11 @@ async def primary(
8282
)
8383
await log.ainfo("primary.auth_key_created")
8484

85-
return (
86-
encoded_auth_key,
87-
encoded_refresh_key,
88-
auth_key_expires,
89-
refresh_key.expires_at,
85+
return KeyRefreshResponse(
86+
access_token=encoded_auth_key,
87+
refresh_token=encoded_refresh_key,
88+
access_token_expires=auth_key_expires,
89+
refresh_token_expires=refresh_key.expires_at,
9090
)
9191

9292

@@ -95,7 +95,7 @@ async def secondary(
9595
settings: Settings,
9696
conn: AsyncSession,
9797
log: FilteringBoundLogger,
98-
) -> tuple[str, str, datetime, datetime]:
98+
) -> KeyRefreshResponse:
9999
"""
100100
Secondary authentication flow - turn in your encoded refresh key
101101
for a new one and an authentication key. This checks against
@@ -156,11 +156,11 @@ async def secondary(
156156

157157
await log.ainfo("secondary.auth_key_created")
158158

159-
return (
160-
encoded_auth_key,
161-
encoded_refresh_key,
162-
auth_key_expires,
163-
refresh_key.expires_at,
159+
return KeyRefreshResponse(
160+
access_token=encoded_auth_key,
161+
refresh_token=encoded_refresh_key,
162+
access_token_expires=auth_key_expires,
163+
refresh_token_expires=refresh_key.expires_at,
164164
)
165165

166166

tests/test_service/test_flow.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ async def test_primary_then_secondary(
1818
async with conn.begin():
1919
application = await app_service.read_by_id(app_id=app, conn=conn)
2020
user_obj = await user_service.read_by_id(user_id=user, conn=conn)
21-
auth, refresh, ake, rke = await flow_service.primary(
21+
key_content = await flow_service.primary(
2222
user=user_obj,
2323
app=application,
2424
settings=server_settings,
2525
conn=conn,
2626
log=logger,
2727
)
2828

29+
auth = key_content.access_token
30+
refresh = key_content.refresh_token
31+
ake = key_content.access_token_expires
32+
rke = key_content.refresh_token_expires
33+
2934
APP_PUBLIC_KEY = application.public_key
3035
APP_KEY_PAIR_TYPE = application.key_pair_type
3136
USER_ID = user_obj.user_id
@@ -40,13 +45,18 @@ async def test_primary_then_secondary(
4045
# Ok, now let's refresh it!
4146
async with session_manager.session() as conn:
4247
async with conn.begin():
43-
auth, refresh, nake, nrke = await flow_service.secondary(
48+
key_content = await flow_service.secondary(
4449
encoded_refresh_key=refresh,
4550
settings=server_settings,
4651
conn=conn,
4752
log=logger,
4853
)
4954

55+
auth = key_content.access_token
56+
refresh = key_content.refresh_token
57+
nake = key_content.access_token_expires
58+
nrke = key_content.refresh_token_expires
59+
5060
# Refresh key expiry doesnt change, auth key does.
5161
assert nake > ake
5262
assert nrke == rke

tests/test_service/test_refresh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def test_create_refresh_key(user, app, session_manager, logger, server_set
6060
encoded_payload=encoded, conn=conn
6161
)
6262
await refresh_service.refresh_refresh_key(
63-
payload=decoded, settings=server_settings, conn=conn
63+
payload=decoded, settings=server_settings, conn=conn, log=logger
6464
)
6565

6666
# Now let's refresh our new refresh key.
@@ -73,7 +73,7 @@ async def test_create_refresh_key(user, app, session_manager, logger, server_set
7373
refreshed_encoded,
7474
refreshed_refresh_key,
7575
) = await refresh_service.refresh_refresh_key(
76-
payload=decoded, settings=server_settings, conn=conn
76+
payload=decoded, settings=server_settings, conn=conn, log=logger
7777
)
7878
REFRESHED_KEY_ID = refreshed_refresh_key.refresh_key_id
7979
assert refreshed_refresh_key.previous == NEW_REFRESH_KEY_ID

0 commit comments

Comments
 (0)