feat: Add recovery code MFA support (#9954)

<!--
Please inform yourself about the contribution guidelines on submitting a
PR here:
https://github.com/zitadel/zitadel/blob/main/CONTRIBUTING.md#submit-a-pull-request-pr.
Take note of how PR/commit titles should be written and replace the
template texts in the sections below. Don't remove any of the sections.
It is important that the commit history clearly shows what is changed
and why.
Important: By submitting a contribution you agree to the terms from our
Licensing Policy as described here:
https://github.com/zitadel/zitadel/blob/main/LICENSING.md#community-contributions.
-->

One-time recovery codes are a common multi-factor authentication (MFA)
backup method, letting users access their account if they lose other MFA
devices. Support for recovery codes can also reduce support burden for
users locked out of their accounts and provide a more secure and
reliable form of verification than security questions.

# Which Problems Are Solved

Zitadel currently lacks support for recovery codes.

# How the Problems Are Solved

This PR partially addresses recovery code support in Zitadel.
Importantly, it adds recovery codes as a new 2FA `Factor` and an
additional `Check` type for the Session API.

```
Example recovery code flow:

1. User generates N new recovery codes using `POST /v2/users/{user_id}/recovery_codes`
2. Zitadel hashes and stores these codes and returns the un-hashed codes in the response
3. User creates new session with an additional check: `recoveryCode`
4. Code is checked against hash and, if valid, cannot be used again
5. User attempts to adds N more codes using the same endpoint 
6. If `remaining_codes + N <= RecoveryCodes->MaxCount` config value, then recovery codes are added in addition to original codes
7. User can remove all recovery codes using `DEL /v2/users/:userId/recovery_codes` 
```
This PR adds: 
- [x] Session recovery_code check support on `POST+PATCH /v2/sessions`
endpoints
- [x] Adds `mfa_recovery_code_checked_at` column (default null) to
`projections.sessions8` table
- [x] Support for `SECOND_FACTOR_TYPE_RECOVERY_CODES` as available 2FA
method on login policy
- [x] Support for importing recovery codes in /import code

Missing, will _not_ implement in this PR:
- [ ] Admin console support for displaying Recovery Code settings for
user(s)
- [ ] Zitadel Typescript login support for recovery codes 

TODO: 
- [x] Additional unit and integration tests
- [x] Error translations

# Additional Changes

None

# Additional Context

- Closes #6898

---------

Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
Zach Hirschtritt
2025-11-24 06:44:48 +00:00
committed by GitHub
co-authored by Livio Spring
parent afa92e55f7
commit 5beeb5738a
101 changed files with 3397 additions and 169 deletions
+29
View File
@@ -8734,6 +8734,30 @@ message ImportHumanUserRequest {
}
];
}
message RecoveryCode {
oneof code_type {
string raw = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
(google.api.field_behavior) = REQUIRED,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
example: "\"a435b-34kns\"";
description: "\"Encoded hash of a recovery code in Modular Crypt Format: https://zitadel.com/docs/concepts/architecture/secrets#hashed-secrets\"";
min_length: 1,
max_length: 200;
}
];
string hash = 2 [
(validate.rules).string = {min_len: 1, max_len: 200},
(google.api.field_behavior) = REQUIRED,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
example: "\"$2a$12$lJ08fqVr8bFJilRVnDT9QeULI7YW.nT3iwUv6dyg0aCrfm3UY8XR2\"";
description: "\"Encoded hash of a recovery code in Modular Crypt Format: https://zitadel.com/docs/concepts/architecture/secrets#hashed-secrets\"";
min_length: 1,
max_length: 200;
}
];
}
}
string user_name = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
@@ -8773,6 +8797,11 @@ message ImportHumanUserRequest {
description: "To link your user directly with an external identity provider (Identity brokering)"
}
];
repeated RecoveryCode recovery_codes = 11 [
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
description: "Import your existing recovery codes. You can import the raw code or the hashed code. The raw code will be hashed on import, the hashed code will be stored as is and need to be in Modular Crypt Format: https://zitadel.com/docs/concepts/architecture/secrets#hashed-secrets";
}
];
}
message ImportHumanUserResponse {
+1
View File
@@ -261,6 +261,7 @@ enum SecondFactorType {
SECOND_FACTOR_TYPE_U2F = 2;
SECOND_FACTOR_TYPE_OTP_EMAIL = 3;
SECOND_FACTOR_TYPE_OTP_SMS = 4;
SECOND_FACTOR_TYPE_RECOVERY_CODES = 5;
}
enum MultiFactorType {
+6
View File
@@ -52,6 +52,7 @@ message Factors {
TOTPFactor totp = 5;
OTPFactor otp_sms = 6;
OTPFactor otp_email = 7;
RecoveryCodeFactor recovery_code = 8;
}
message UserFactor {
@@ -103,6 +104,11 @@ message OTPFactor {
google.protobuf.Timestamp verified_at = 1;
}
message RecoveryCodeFactor {
// The timestamp when the Recovery Code was last verified.
google.protobuf.Timestamp verified_at = 1;
}
message SearchQuery {
oneof query {
option (validate.required) = true;
@@ -535,6 +535,12 @@ message Checks {
// containing the verification time.
// Note that the OTP code is valid for a single use only and will be invalidated after a successful check.
optional CheckOTP otp_email = 7;
// Check the Recovery Code and update the session on success.
// Requires that the user is already checked, either in the previous or the same request.
// On successful Recovery Code check, the session's `factors` field will be updated with a `recovery_code` factor,
// containing the verification time.
optional CheckRecoveryCode recovery_code = 8;
}
message CheckUser {
@@ -643,3 +649,17 @@ message CheckOTP {
}
];
}
message CheckRecoveryCode {
// The Recovery Code of the user to be checked.
// The code must match the exact code previously generated for the user, including dashes if any.
// On successful check, the recovery code will be invalidated and cannot be used again.
string code = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
min_length: 1;
max_length: 200;
example: "\"1234567890\"";
}
];
}
@@ -151,6 +151,7 @@ enum SecondFactorType {
SECOND_FACTOR_TYPE_U2F = 2;
SECOND_FACTOR_TYPE_OTP_EMAIL = 3;
SECOND_FACTOR_TYPE_OTP_SMS = 4;
SECOND_FACTOR_TYPE_RECOVERY_CODES = 5;
}
enum MultiFactorType {
+88
View File
@@ -1145,6 +1145,53 @@ service UserService {
};
}
// Generate single-use recovery codes for a user
//
// Generate new single-use recovery codes for the authenticated user. Recovery codes can be used to recover access to the account if other second factors are not available.
rpc GenerateRecoveryCodes (GenerateRecoveryCodesRequest) returns (GenerateRecoveryCodesResponse) {
option (google.api.http) = {
post: "/v2/users/{user_id}/recovery_codes"
body: "*"
};
option (zitadel.protoc_gen_zitadel.v2.options) = {
auth_option: {
permission: "authenticated"
}
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
responses: {
key: "200"
value: {
description: "OK";
}
};
};
}
// Remove recovery codes from a user
//
// Remove all recovery codes from the authenticated user. This will disable the recovery code second factor.
rpc RemoveRecoveryCodes (RemoveRecoveryCodesRequest) returns (RemoveRecoveryCodesResponse) {
option (google.api.http) = {
delete: "/v2/users/{user_id}/recovery_codes"
};
option (zitadel.protoc_gen_zitadel.v2.options) = {
auth_option: {
permission: "authenticated"
}
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
responses: {
key: "200"
value: {
description: "OK";
}
};
};
}
// Add OTP Email for a user
//
// Add a new One-Time Password (OTP) Email factor to the authenticated user. OTP Email will enable the user to verify a OTP with the latest verified email. The email has to be verified to add the second factor..
@@ -2886,6 +2933,46 @@ message RemoveTOTPResponse {
zitadel.object.v2.Details details = 1;
}
message GenerateRecoveryCodesRequest {
string user_id = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
(google.api.field_behavior) = REQUIRED,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
min_length: 1;
max_length: 200;
example: "\"163840776835432705\"";
}
];
int32 count = 2 [
(validate.rules).int32 = {gte: 1, lte: 50},
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
description: "The number of recovery codes to generate"
example: "5";
}
];
}
message GenerateRecoveryCodesResponse {
zitadel.object.v2.Details details = 1;
repeated string recovery_codes = 2;
}
message RemoveRecoveryCodesRequest {
string user_id = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
(google.api.field_behavior) = REQUIRED,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
min_length: 1;
max_length: 200;
example: "\"163840776835432705\"";
}
];
}
message RemoveRecoveryCodesResponse {
zitadel.object.v2.Details details = 1;
}
message AddOTPSMSRequest {
string user_id = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
@@ -3262,6 +3349,7 @@ enum AuthenticationMethodType {
AUTHENTICATION_METHOD_TYPE_U2F = 5;
AUTHENTICATION_METHOD_TYPE_OTP_SMS = 6;
AUTHENTICATION_METHOD_TYPE_OTP_EMAIL = 7;
AUTHENTICATION_METHOD_TYPE_RECOVERY_CODE = 8;
}
message ListAuthenticationFactorsRequest{