Skip to content

Authentication API reference

Password reset tokens

Password reset token domain module.

Sign up tokens

Sign-up token domain module.

Profile sessions

The signed-in user's own sessions, mounted under /profile/sessions.

delete_profile_session

delete_profile_session(request, session_id, token_user_id, db)

Delete user session from database.

Parameters:

Name Type Description Default
request Request

The incoming request, read by the rate limiter.

required
session_id AuthSessionId

Session identifier to delete.

required
token_user_id CurrentUserId

User ID from access token.

required
db DbSession

Database session.

required

Returns:

Type Description
None

None.

Source code in backend/app/modules/auth/account_security/sessions_router.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
@router.delete(
    "/{session_id}",
    status_code=status.HTTP_204_NO_CONTENT,
    response_model=None,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.WRITE)
def delete_profile_session(
    request: Request,
    session_id: auth_dependencies.AuthSessionId,
    token_user_id: auth_dependencies.CurrentUserId,
    db: core_dependencies.DbSession,
) -> None:
    """Delete user session from database.

    Args:
        request: The incoming request, read by the rate limiter.
        session_id: Session identifier to delete.
        token_user_id: User ID from access token.
        db: Database session.

    Returns:
        None.
    """
    account_security_service.delete_user_session(session_id, token_user_id, db)

read_sessions_me

read_sessions_me(token_user_id, token_session_id, page, db)

Retrieve one page of the authenticated user's sessions.

is_current marks the session the request is made from; it is false for every item when an API key calls.

Source code in backend/app/modules/auth/account_security/sessions_router.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@router.get(
    "",
    response_model=auth_sessions_schema.UserSessionsPage,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
def read_sessions_me(
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.OptionalSessionId,
    page: core_pagination.ListPage,
    db: core_dependencies.DbSession,
) -> auth_sessions_schema.UserSessionsPage:
    """Retrieve one page of the authenticated user's sessions.

    `is_current` marks the session the request is made from; it is false for every item when an API key calls.
    """
    return account_security_service.list_user_sessions(token_user_id, page, db, token_session_id)

revoke_profile_sessions

revoke_profile_sessions(request, response, token_user_id, token_session_id, db, keep_current=False)

Sign out everywhere, or only on the other devices.

By default every session of the authenticated user ends, the current one included, so the client must discard its tokens and send the user to the login page; the browser's refresh-token cookie is cleared on the response. With keep_current=true every other session ends and the caller stays signed in, so the cookie is left alone.

Parameters:

Name Type Description Default
request Request

The incoming request, read by the rate limiter.

required
response Response

The response, on which the refresh-token cookie is cleared.

required
token_user_id CurrentUserId

User ID from access token.

required
token_session_id CurrentSessionId

The caller's current session, preserved when keep_current is set.

required
db DbSession

Database session.

required
keep_current Annotated[bool, Query(description="Keep the caller's current session and end only the others")]

Whether to keep the caller's current session.

False

Returns:

Type Description
None

None.

Source code in backend/app/modules/auth/account_security/sessions_router.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@router.post(
    "/revoke",
    status_code=status.HTTP_204_NO_CONTENT,
    response_model=None,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.WRITE)
def revoke_profile_sessions(
    request: Request,
    response: Response,
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.CurrentSessionId,
    db: core_dependencies.DbSession,
    keep_current: Annotated[
        bool,
        Query(description="Keep the caller's current session and end only the others"),
    ] = False,
) -> None:
    """Sign out everywhere, or only on the other devices.

    By default every session of the authenticated user ends, the current one included, so the client must discard
    its tokens and send the user to the login page; the browser's refresh-token cookie is cleared on the response.
    With `keep_current=true` every other session ends and the caller stays signed in, so the cookie is left alone.

    Args:
        request: The incoming request, read by the rate limiter.
        response: The response, on which the refresh-token cookie is cleared.
        token_user_id: User ID from access token.
        token_session_id: The caller's current session, preserved when `keep_current` is set.
        db: Database session.
        keep_current: Whether to keep the caller's current session.

    Returns:
        None.
    """
    if keep_current:
        account_security_service.delete_other_user_sessions(token_user_id, token_session_id, db)
        return
    account_security_service.sign_out_everywhere(token_user_id, db)
    authentication_dependencies.clear_refresh_token_cookies(response, environment=core_config.settings.ENVIRONMENT)

Profile two-step verification

The signed-in user's own two-step verification, mounted under /profile/mfa.

disable_mfa

disable_mfa(request, body, token_user_id, token_session_id, step_up_store, db)

Disable MFA for authenticated user after step-up verification.

Disabling MFA materially weakens the account's security posture, so a valid access token alone is not sufficient: the caller must re-prove identity with both their current password and a fresh MFA code, both in step_up. The MFA code may be either a 6-digit TOTP or an unused XXXX-XXXX backup code — the same set of factors accepted at login. Step-up verification is the single source of truth for the MFA check; :func:modules.auth.mfa.service.disable_user_mfa only clears state. The user's other sessions end with the change unless revoke_other_sessions is false.

Parameters:

Name Type Description Default
request Request

The incoming request, read by the rate limiter.

required
body MFADisableRequest

The step-up verification, with the MFA code, and whether to end the other sessions.

required
token_user_id CurrentUserId

User ID from access token.

required
token_session_id CurrentSessionId

Session ID from access token.

required
step_up_store Annotated[StepUpStore, Depends(get_step_up_attempts)]

Step-up lockout store.

required
db DbSession

Database session.

required

Raises:

Type Description
DomainError

403 if step-up verification fails (the current password is wrong or the MFA code is invalid); 409 if MFA is not currently enabled.

Source code in backend/app/modules/auth/account_security/mfa_router.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
@router.post(
    "/disable",
    response_model=None,
    status_code=status.HTTP_204_NO_CONTENT,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.SENSITIVE)
def disable_mfa(
    request: Request,
    body: mfa_schema.MFADisableRequest,
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.CurrentSessionId,
    step_up_store: Annotated[
        auth_dependencies.StepUpStore,
        Depends(auth_dependencies.get_step_up_attempts),
    ],
    db: core_dependencies.DbSession,
) -> None:
    """Disable MFA for authenticated user after step-up verification.

    Disabling MFA materially weakens the account's security
    posture, so a valid access token alone is not sufficient: the
    caller must re-prove identity with both their current
    password and a fresh MFA code, both in ``step_up``. The MFA code may be either a
    6-digit TOTP or an unused ``XXXX-XXXX`` backup code — the
    same set of factors accepted at login. Step-up verification
    is the single source of truth for the MFA check;
    :func:`modules.auth.mfa.service.disable_user_mfa` only clears state. The user's
    other sessions end with the change unless ``revoke_other_sessions`` is false.

    Args:
        request: The incoming request, read by the rate limiter.
        body: The step-up verification, with the MFA code, and whether to end the other sessions.
        token_user_id: User ID from access token.
        token_session_id: Session ID from access token.
        step_up_store: Step-up lockout store.
        db: Database session.

    Raises:
        DomainError: 403 if step-up verification fails (the current password
            is wrong or the MFA code is invalid); 409 if MFA is not currently
            enabled.
    """
    mfa_workflow_service.disable_mfa(
        body.step_up,
        token_user_id,
        token_session_id,
        step_up_store,
        db,
        revoke_other_sessions=body.revoke_other_sessions,
    )

enable_mfa

enable_mfa(request, response, mfa_request, token_user_id, token_session_id, step_up_store, mfa_secret_store, db)

Enable MFA for authenticated user after step-up verification.

A stolen access token alone must not be sufficient to enrol a new TOTP secret on a victim's account (which would lock the legitimate user out and hand the attacker the backup codes). The caller therefore has to re-prove identity with their current password before the binding is committed. The step-up verification is the step_up object (see :func:modules.auth.account_security.step_up_service.verify_step_up_credentials). The totp_code in the request body is the fresh enrolment code from the user's authenticator app and is verified separately by :func:modules.auth.mfa.service.enable_user_mfa against the secret issued by POST /profile/mfa/setup. The user's other sessions end with the change unless revoke_other_sessions is false.

Parameters:

Name Type Description Default
request Request

The incoming request, read by the rate limiter.

required
response Response

The response, marked as not cacheable because it carries the backup codes.

required
mfa_request MFASetupRequest

MFA setup request with the TOTP code and the step-up verification.

required
token_user_id CurrentUserId

User ID from access token.

required
token_session_id CurrentSessionId

Session ID from access token.

required
mfa_secret_store Annotated[MFASecretStoreBackend, Depends(get_mfa_secret_store)]

Temporary secret storage.

required
step_up_store Annotated[StepUpStore, Depends(get_step_up_attempts)]

Step-up lockout store.

required
db DbSession

Database session.

required

Returns:

Type Description
MFABackupCodesRead

The freshly issued backup codes and when they were created.

Raises:

Type Description
DomainError

409 if no setup is in progress or MFA is already enabled; 400 if the TOTP code is invalid; 403 if step-up verification fails.

Source code in backend/app/modules/auth/account_security/mfa_router.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
@router.post(
    "/enable",
    response_model=mfa_backup_codes_schema.MFABackupCodesRead,
    responses={200: {"headers": core_openapi_contract.NO_STORE_RESPONSE_HEADERS}},
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.SENSITIVE)
def enable_mfa(
    request: Request,
    response: Response,
    mfa_request: mfa_schema.MFASetupRequest,
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.CurrentSessionId,
    step_up_store: Annotated[
        auth_dependencies.StepUpStore,
        Depends(auth_dependencies.get_step_up_attempts),
    ],
    mfa_secret_store: Annotated[
        auth_dependencies.MFASecretStoreBackend,
        Depends(auth_dependencies.get_mfa_secret_store),
    ],
    db: core_dependencies.DbSession,
) -> mfa_backup_codes_schema.MFABackupCodesRead:
    """Enable MFA for authenticated user after step-up verification.

    A stolen access token alone must not be sufficient to enrol a
    new TOTP secret on a victim's account (which would lock the
    legitimate user out and hand the attacker the backup codes).
    The caller therefore has to re-prove identity with their
    current password before the binding is committed. The step-up
    verification is the ``step_up`` object (see
    :func:`modules.auth.account_security.step_up_service.verify_step_up_credentials`).
    The ``totp_code`` in the request body is the fresh enrolment code from the
    user's authenticator app and is verified separately by
    :func:`modules.auth.mfa.service.enable_user_mfa` against the secret
    issued by ``POST /profile/mfa/setup``. The user's other sessions end with the
    change unless ``revoke_other_sessions`` is false.

    Args:
        request: The incoming request, read by the rate limiter.
        response: The response, marked as not cacheable because it carries the backup codes.
        mfa_request: MFA setup request with the TOTP code and the step-up verification.
        token_user_id: User ID from access token.
        token_session_id: Session ID from access token.
        mfa_secret_store: Temporary secret storage.
        step_up_store: Step-up lockout store.
        db: Database session.

    Returns:
        The freshly issued backup codes and when they were created.

    Raises:
        DomainError: 409 if no setup is in progress or MFA is already enabled; 400 if the TOTP
            code is invalid; 403 if step-up verification fails.
    """
    core_cache_control.set_no_store(response)
    return mfa_workflow_service.enable_mfa(
        mfa_request, token_user_id, token_session_id, step_up_store, mfa_secret_store, db
    )

generate_mfa_backup_codes

generate_mfa_backup_codes(response, request, body, token_user_id, token_session_id, step_up_store, db)

Generate new MFA backup codes for authenticated user.

Requires step-up verification (current password + MFA code, when MFA is enabled). Issuing new backup codes invalidates the previous set, so an attacker holding only an access token must not be able to lock the legitimate user out of their second factor. The user's other sessions end with the change unless revoke_other_sessions is false.

Parameters:

Name Type Description Default
response Response

The response, marked as not cacheable because it carries the codes.

required
request Request

FastAPI request object for rate limiting.

required
body MFABackupCodesRequest

The step-up verification and whether to end the other sessions.

required
token_user_id CurrentUserId

User ID from access token.

required
token_session_id CurrentSessionId

Session ID from access token.

required
step_up_store Annotated[StepUpStore, Depends(get_step_up_attempts)]

Step-up lockout store.

required
db DbSession

Database session.

required

Returns:

Type Description
MFABackupCodesRead

Response with generated backup codes and timestamp.

Raises:

Type Description
DomainError

403 if step-up verification fails, 404 if user not found, 409 if MFA is not enabled.

Source code in backend/app/modules/auth/account_security/mfa_router.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
@router.post(
    "/backup-codes",
    status_code=status.HTTP_201_CREATED,
    response_model=mfa_backup_codes_schema.MFABackupCodesRead,
    responses={201: {"headers": core_openapi_contract.NO_STORE_RESPONSE_HEADERS}},
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.SENSITIVE)
def generate_mfa_backup_codes(
    response: Response,
    request: Request,
    body: mfa_schema.MFABackupCodesRequest,
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.CurrentSessionId,
    step_up_store: Annotated[
        auth_dependencies.StepUpStore,
        Depends(auth_dependencies.get_step_up_attempts),
    ],
    db: core_dependencies.DbSession,
) -> mfa_backup_codes_schema.MFABackupCodesRead:
    """Generate new MFA backup codes for authenticated user.

    Requires step-up verification (current password + MFA code,
    when MFA is enabled). Issuing new backup codes invalidates
    the previous set, so an attacker holding only an access
    token must not be able to lock the legitimate user out of
    their second factor. The user's other sessions end with the change
    unless ``revoke_other_sessions`` is false.

    Args:
        response: The response, marked as not cacheable because it carries the codes.
        request: FastAPI request object for rate limiting.
        body: The step-up verification and whether to end the other sessions.
        token_user_id: User ID from access token.
        token_session_id: Session ID from access token.
        step_up_store: Step-up lockout store.
        db: Database session.

    Returns:
        Response with generated backup codes and timestamp.

    Raises:
        DomainError: 403 if step-up verification fails, 404 if
            user not found, 409 if MFA is not enabled.
    """
    core_cache_control.set_no_store(response)
    return mfa_workflow_service.generate_backup_codes(
        body.step_up,
        token_user_id,
        token_session_id,
        step_up_store,
        db,
        revoke_other_sessions=body.revoke_other_sessions,
    )

get_backup_code_status

get_backup_code_status(token_user_id, db)

Retrieve MFA backup code status for authenticated user.

Parameters:

Name Type Description Default
token_user_id CurrentUserId

User ID from access token.

required
db DbSession

Database session.

required

Returns:

Type Description
MFABackupCodeStatusRead

Backup code status with counts and creation date.

Source code in backend/app/modules/auth/account_security/mfa_router.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@router.get(
    "/backup-codes/status",
    response_model=mfa_backup_codes_schema.MFABackupCodeStatusRead,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
def get_backup_code_status(
    token_user_id: auth_dependencies.CurrentUserId,
    db: core_dependencies.DbSession,
) -> mfa_backup_codes_schema.MFABackupCodeStatusRead:
    """Retrieve MFA backup code status for authenticated user.

    Args:
        token_user_id: User ID from access token.
        db: Database session.

    Returns:
        Backup code status with counts and creation date.
    """
    return mfa_workflow_service.get_backup_code_status(token_user_id, db)

get_mfa_status

get_mfa_status(token_user_id, db)

Return whether the account has MFA and the second factors it has enrolled.

Source code in backend/app/modules/auth/account_security/mfa_router.py
20
21
22
23
24
25
26
27
28
29
30
@router.get(
    "/status",
    response_model=mfa_schema.MFAStatusRead,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
def get_mfa_status(
    token_user_id: auth_dependencies.CurrentUserId,
    db: core_dependencies.DbSession,
) -> mfa_schema.MFAStatusRead:
    """Return whether the account has MFA and the second factors it has enrolled."""
    return mfa_workflow_service.get_mfa_status(token_user_id, db)

setup_mfa

setup_mfa(request, response, token_user_id, mfa_secret_store, db)

Initiate MFA setup for authenticated user.

Parameters:

Name Type Description Default
request Request

The incoming request, read by the rate limiter.

required
response Response

The response, marked as not cacheable because it carries the secret.

required
token_user_id CurrentUserId

User ID from access token.

required
mfa_secret_store Annotated[MFASecretStoreBackend, Depends(get_mfa_secret_store)]

Temporary secret storage.

required
db DbSession

Database session.

required

Returns:

Type Description
MFASetupRead

MFA setup response with secret and QR code.

Source code in backend/app/modules/auth/account_security/mfa_router.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@router.post(
    "/setup",
    status_code=status.HTTP_201_CREATED,
    response_model=mfa_schema.MFASetupRead,
    responses={201: {"headers": core_openapi_contract.NO_STORE_RESPONSE_HEADERS}},
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.SENSITIVE)
def setup_mfa(
    request: Request,
    response: Response,
    token_user_id: auth_dependencies.CurrentUserId,
    mfa_secret_store: Annotated[
        auth_dependencies.MFASecretStoreBackend,
        Depends(auth_dependencies.get_mfa_secret_store),
    ],
    db: core_dependencies.DbSession,
) -> mfa_schema.MFASetupRead:
    """Initiate MFA setup for authenticated user.

    Args:
        request: The incoming request, read by the rate limiter.
        response: The response, marked as not cacheable because it carries the secret.
        token_user_id: User ID from access token.
        mfa_secret_store: Temporary secret storage.
        db: Database session.

    Returns:
        MFA setup response with secret and QR code.
    """
    core_cache_control.set_no_store(response)
    return mfa_workflow_service.setup_mfa(token_user_id, mfa_secret_store, db)

The signed-in user's own identity provider links, mounted under /profile/identity-providers.

delete_my_identity_provider(idp_id, body, request, token_user_id, token_session_id, step_up_store, db)

Delete (unlink) an identity provider from the authenticated user's account.

This endpoint allows users to remove the association between their account and a specific identity provider. It requires step-up verification (current password or a fresh identity-provider sign-in, and MFA when enabled) and includes safety checks to prevent account lockout by ensuring users maintain at least one authentication method (either a password or another IdP link).

Parameters:

Name Type Description Default
idp_id UUID

The ID of the identity provider to unlink.

required
body StepUpRequest

The step-up verification.

required
request Request

The FastAPI request object.

required
token_user_id UUID

User ID extracted from the access token.

required
token_session_id CurrentSessionId

Session ID from access token.

required
step_up_store Annotated[StepUpStore, Depends(get_step_up_attempts)]

Step-up lockout store.

required
db DbSession

Database session.

required

Returns:

Name Type Description
None None

Returns 204 No Content on successful deletion.

Raises:

Type Description
DomainError(403)

If step-up verification fails.

DomainError(404)

If the identity provider doesn't exist or is not linked to the user's account.

DomainError(409)

If attempting to unlink the last authentication method without having a password set (prevents account lockout).

DomainError(500)

If the deletion operation fails at the database level.

Notes
  • Enforces step-up verification before unlinking.
  • Prevents account lockout by ensuring users have at least one authentication method (password or remaining IdP link).
  • Logs the unlinking action for audit purposes.
  • Uses token-based authentication to ensure users can only unlink their own IdPs.
Source code in backend/app/modules/auth/account_security/identity_links_router.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
@router.post(
    "/{idp_id}/unlink",
    status_code=status.HTTP_204_NO_CONTENT,
    response_model=None,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.SENSITIVE)
def delete_my_identity_provider(
    idp_id: core_dependencies.UuidId,
    body: auth_schema.StepUpRequest,
    request: Request,
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.CurrentSessionId,
    step_up_store: Annotated[
        auth_dependencies.StepUpStore,
        Depends(auth_dependencies.get_step_up_attempts),
    ],
    db: core_dependencies.DbSession,
) -> None:
    """Delete (unlink) an identity provider from the authenticated user's account.

    This endpoint allows users to remove the association between their
    account and a specific identity provider. It requires step-up
    verification (current password or a fresh identity-provider
    sign-in, and MFA when enabled) and includes
    safety checks to prevent account lockout by ensuring users maintain
    at least one authentication method (either a password or another
    IdP link).

    Args:
        idp_id (uuid.UUID): The ID of the identity provider to unlink.
        body (StepUpRequest): The step-up verification.
        request (Request): The FastAPI request object.
        token_user_id (uuid.UUID): User ID extracted from the access token.
        token_session_id: Session ID from access token.
        step_up_store: Step-up lockout store.
        db: Database session.

    Returns:
        None: Returns 204 No Content on successful deletion.

    Raises:
        DomainError (403): If step-up verification fails.
        DomainError (404): If the identity provider doesn't exist or
            is not linked to the user's account.
        DomainError (409): If attempting to unlink the last authentication method
            without having a password set (prevents account lockout).
        DomainError (500): If the deletion operation fails at the database level.

    Notes:
        - Enforces step-up verification before unlinking.
        - Prevents account lockout by ensuring users have at least one
          authentication method (password or remaining IdP link).
        - Logs the unlinking action for audit purposes.
        - Uses token-based authentication to ensure users can only
          unlink their own IdPs.
    """
    identity_link_service.delete_identity_provider_link(
        idp_id, body.step_up, token_user_id, token_session_id, step_up_store, db
    )
    return None
get_my_identity_providers(token_user_id, page, db)

Retrieve one page of the authenticated user's identity provider links.

Each link carries its connection metadata and the provider's name, slug, icon and type.

Parameters:

Name Type Description Default
token_user_id CurrentUserId

The authenticated user's ID from the access token.

required
page ListPage

The requested window.

required
db DbSession

Database session.

required

Returns:

Type Description
IdentityProviderLinkPage

The page of the user's identity provider links, oldest first.

Source code in backend/app/modules/auth/account_security/identity_links_router.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
@router.get(
    "",
    response_model=auth_identity_links_schema.IdentityProviderLinkPage,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
def get_my_identity_providers(
    token_user_id: auth_dependencies.CurrentUserId,
    page: core_pagination.ListPage,
    db: core_dependencies.DbSession,
) -> auth_identity_links_schema.IdentityProviderLinkPage:
    """Retrieve one page of the authenticated user's identity provider links.

    Each link carries its connection metadata and the provider's name, slug, icon and type.

    Args:
        token_user_id: The authenticated user's ID from the access token.
        page: The requested window.
        db: Database session.

    Returns:
        The page of the user's identity provider links, oldest first.
    """
    return identity_link_service.get_user_identity_provider_links(token_user_id, page, db)
start_idp_link(idp_id, link, request, response, token_user_id, token_session_id, step_up_store, state, db)

Start linking an identity provider to the authenticated user's account.

Linking enables persistent sign-in, so it needs step-up verification in step_up: current_password (or, for an account without one, idp_reauth), and an MFA code when MFA is enabled. The browser then follows authorization_url, signs in at the provider, and the callback returns to redirect. For a web client the response sets a cookie that binds the link to this browser. For a native-app redirect the app opens authorization_url in the system browser, which is not the app's HTTP client: the URL is an Endurain address with a single-use ticket that lives two minutes, and the browser that opens it receives the binding cookie and is redirected to the provider. The callback then returns to redirect with idp_link=success or idp_link=error.

Parameters:

Name Type Description Default
idp_id UuidId

The identity provider to link.

required
link IdentityProviderLinkStart

The frontend return path and the step-up verification.

required
request Request

The incoming request, read by the rate limiter and for the client address.

required
response Response

The response, which receives the browser-binding cookie.

required
token_user_id CurrentUserId

User ID from access token.

required
token_session_id CurrentSessionId

Session ID from access token.

required
step_up_store Annotated[StepUpStore, Depends(get_step_up_attempts)]

Step-up lockout store.

required
state PlatformState

The platform state provider, which holds the link ticket of a native app.

required
db DbSession

Database session.

required

Returns:

Type Description
BrowserFlowStartRead

The provider's authorization URL, or the ticket URL of a native app.

Source code in backend/app/modules/auth/account_security/identity_links_router.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@router.post(
    "/{idp_id}/link",
    response_model=identity_providers_schema.BrowserFlowStartRead,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.SENSITIVE)
def start_idp_link(
    idp_id: core_dependencies.UuidId,
    link: users_schema.IdentityProviderLinkStart,
    request: Request,
    response: Response,
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.CurrentSessionId,
    step_up_store: Annotated[
        auth_dependencies.StepUpStore,
        Depends(auth_dependencies.get_step_up_attempts),
    ],
    state: core_dependencies.PlatformState,
    db: core_dependencies.DbSession,
) -> identity_providers_schema.BrowserFlowStartRead:
    """Start linking an identity provider to the authenticated user's account.

    Linking enables persistent sign-in, so it needs step-up verification in ``step_up``: ``current_password`` (or, for
    an
    account without one, ``idp_reauth``), and an MFA code when MFA is enabled. The browser then follows
    ``authorization_url``, signs in at the provider, and the callback returns to ``redirect``. For a web client the
    response sets a cookie that binds the link to this browser. For a native-app ``redirect`` the app opens
    ``authorization_url`` in the system browser, which is not the app's HTTP client: the URL is an Endurain address
    with a single-use ticket that lives two minutes, and the browser that opens it receives the binding cookie and is
    redirected to the provider. The callback then returns to ``redirect`` with ``idp_link=success`` or
    ``idp_link=error``.

    Args:
        idp_id: The identity provider to link.
        link: The frontend return path and the step-up verification.
        request: The incoming request, read by the rate limiter and for the client address.
        response: The response, which receives the browser-binding cookie.
        token_user_id: User ID from access token.
        token_session_id: Session ID from access token.
        step_up_store: Step-up lockout store.
        state: The platform state provider, which holds the link ticket of a native app.
        db: Database session.

    Returns:
        The provider's authorization URL, or the ticket URL of a native app.
    """
    flow = identity_link_service.start_idp_link(
        idp_id,
        link,
        core_network.get_ip_address(request),
        token_user_id,
        token_session_id,
        step_up_store,
        state,
        db,
    )
    if isinstance(flow, identity_providers_contracts.BrowserFlowStart):
        auth_dependencies.set_idp_browser_binding_cookie(response, flow.browser_binding)
    return identity_providers_schema.BrowserFlowStartRead(authorization_url=flow.authorization_url)
start_idp_step_up(idp_id, start, request, response, token_user_id, token_session_id, step_up_store, db)

Start a fresh sign-in with a linked identity provider, for an account without a local password.

Such an account has no password to prove, so a sensitive operation asks for this instead: the browser follows authorization_url, the provider signs the user in again, and the callback returns to redirect with a single-use step_up_ticket in the URL fragment. The sensitive request then sends the ticket and the PKCE verifier of code_challenge as idp_reauth. The response sets a cookie that binds the sign-in to this browser.

Parameters:

Name Type Description Default
idp_id UuidId

The linked provider to sign in with again.

required
start StepUpStartRequest

The PKCE challenge, the operation and the return path.

required
request Request

The incoming request, read by the rate limiter.

required
response Response

The response, which receives the browser-binding cookie.

required
token_user_id CurrentUserId

User ID from access token.

required
token_session_id CurrentSessionId

Session ID from access token.

required
step_up_store Annotated[StepUpStore, Depends(get_step_up_attempts)]

Step-up lockout store.

required
db DbSession

Database session.

required

Returns:

Type Description
BrowserFlowStartRead

The provider's authorization URL.

Source code in backend/app/modules/auth/account_security/identity_links_router.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@router.post(
    "/{idp_id}/step-up",
    response_model=identity_providers_schema.BrowserFlowStartRead,
    dependencies=[Security(auth_dependencies.check_scopes, scopes=["account:security"])],
)
@core_rate_limit.limiter.limit(core_rate_limit.SENSITIVE)
def start_idp_step_up(
    idp_id: core_dependencies.UuidId,
    start: identity_providers_schema.StepUpStartRequest,
    request: Request,
    response: Response,
    token_user_id: auth_dependencies.CurrentUserId,
    token_session_id: auth_dependencies.CurrentSessionId,
    step_up_store: Annotated[
        auth_dependencies.StepUpStore,
        Depends(auth_dependencies.get_step_up_attempts),
    ],
    db: core_dependencies.DbSession,
) -> identity_providers_schema.BrowserFlowStartRead:
    """Start a fresh sign-in with a linked identity provider, for an account without a local password.

    Such an account has no password to prove, so a sensitive operation asks for this instead: the browser
    follows ``authorization_url``, the provider signs the user in again, and the callback returns to
    ``redirect`` with a single-use ``step_up_ticket`` in the URL fragment. The sensitive request then sends the
    ticket and the PKCE verifier of ``code_challenge`` as ``idp_reauth``. The response sets a cookie that binds
    the sign-in to this browser.

    Args:
        idp_id: The linked provider to sign in with again.
        start: The PKCE challenge, the operation and the return path.
        request: The incoming request, read by the rate limiter.
        response: The response, which receives the browser-binding cookie.
        token_user_id: User ID from access token.
        token_session_id: Session ID from access token.
        step_up_store: Step-up lockout store.
        db: Database session.

    Returns:
        The provider's authorization URL.
    """
    flow = identity_link_service.start_idp_step_up(
        idp_id,
        start,
        core_network.get_ip_address(request),
        token_user_id,
        token_session_id,
        step_up_store,
        db,
    )
    auth_dependencies.set_idp_browser_binding_cookie(response, flow.browser_binding)
    return identity_providers_schema.BrowserFlowStartRead(authorization_url=flow.authorization_url)