-
Notifications
You must be signed in to change notification settings - Fork 13.7k
chore: migrate 2FA TOTP DDP methods to /v1/users.totp.* REST endpoints #40734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ggazzo
wants to merge
2
commits into
develop
Choose a base branch
from
chore/ddp-migrate-batch5-2fa
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+369
−189
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Migrated the `TwoFactorTOTP` account settings page from the five `2fa:*` DDP methods to the new `/v1/users.totp.*` REST endpoints. DDP methods stay registered for external SDK/mobile clients with deprecation logs pointing at the new routes until 9.0.0. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| '@rocket.chat/rest-typings': minor | ||
| '@rocket.chat/meteor': minor | ||
| --- | ||
|
|
||
| Added five new REST endpoints under `/v1/users.totp.*` covering the TOTP 2FA flows that previously only existed as DDP methods: | ||
|
|
||
| - `POST /v1/users.totp.enable` → `{ secret, url }` (replaces `2fa:enable`) | ||
| - `POST /v1/users.totp.disable` body `{ code }` → `{ disabled }` (replaces `2fa:disable`) | ||
| - `POST /v1/users.totp.validate` body `{ code }` → `{ codes }` (replaces `2fa:validateTempToken`; also rotates non-PAT login tokens server-side) | ||
| - `POST /v1/users.totp.regenerateCodes` body `{ code }` → `{ codes }` (replaces `2fa:regenerateCodes`) | ||
| - `GET /v1/users.totp.codesRemaining` → `{ remaining }` (replaces `2fa:checkCodesRemaining`) | ||
|
|
||
| The legacy DDP methods stay registered with deprecation logs pointing at the new routes until 9.0.0 removes them. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { Users } from '@rocket.chat/models'; | ||
| import { Accounts } from 'meteor/accounts-base'; | ||
| import { Meteor } from 'meteor/meteor'; | ||
|
|
||
| import { notifyOnUserChange, notifyOnUserChangeAsync } from '../../../lib/server/lib/notifyListener'; | ||
| import { TOTP } from '../lib/totp'; | ||
|
|
||
| const requireUser = async (userId: string | null) => { | ||
| if (!userId) { | ||
| throw new Meteor.Error('not-authorized'); | ||
| } | ||
|
|
||
| const user = await Users.findOneById(userId); | ||
| if (!user) { | ||
| throw new Meteor.Error('error-invalid-user', 'Invalid user'); | ||
| } | ||
|
|
||
| return user; | ||
| }; | ||
|
|
||
| export const enableTotp = async (userId: string | null): Promise<{ secret: string; url: string }> => { | ||
| const user = await requireUser(userId); | ||
|
|
||
| if (!user.username) { | ||
| throw new Meteor.Error('error-invalid-user', 'Invalid user'); | ||
| } | ||
|
|
||
| if (user.services?.totp?.enabled) { | ||
| throw new Meteor.Error('error-2fa-already-enabled'); | ||
| } | ||
|
|
||
| const secret = TOTP.generateSecret(); | ||
|
|
||
| await Users.disable2FAAndSetTempSecretByUserId(user._id, secret.base32); | ||
|
|
||
| return { | ||
| secret: secret.base32, | ||
| url: TOTP.generateOtpauthURL(secret, user.username), | ||
| }; | ||
| }; | ||
|
|
||
| export const disableTotp = async (userId: string | null, code: string): Promise<boolean> => { | ||
| const user = await requireUser(userId); | ||
|
|
||
| if (!user.services?.totp?.enabled) { | ||
| return false; | ||
| } | ||
|
|
||
| const verified = await TOTP.verify({ | ||
| secret: user.services.totp.secret, | ||
| token: code, | ||
| userId: user._id, | ||
| backupTokens: user.services.totp.hashedBackup, | ||
| }); | ||
|
|
||
| if (!verified) { | ||
| return false; | ||
| } | ||
|
|
||
| const { modifiedCount } = await Users.disable2FAByUserId(user._id); | ||
|
|
||
| if (!modifiedCount) { | ||
| return false; | ||
| } | ||
|
|
||
| void notifyOnUserChange({ clientAction: 'updated', id: user._id, diff: { 'services.totp.enabled': false } }); | ||
|
|
||
| return true; | ||
| }; | ||
|
|
||
| export const validateTotpTempToken = async (userId: string | null, userToken: string, authToken?: string): Promise<{ codes: string[] }> => { | ||
| const user = await requireUser(userId); | ||
|
|
||
| if (!user.services?.totp?.tempSecret) { | ||
| throw new Meteor.Error('invalid-totp'); | ||
| } | ||
|
|
||
| const verified = await TOTP.verify({ | ||
| secret: user.services.totp.tempSecret, | ||
| token: userToken, | ||
| }); | ||
| if (!verified) { | ||
| throw new Meteor.Error('invalid-totp'); | ||
| } | ||
|
|
||
| const { codes, hashedCodes } = TOTP.generateCodes(); | ||
|
|
||
| await Users.enable2FAAndSetSecretAndCodesByUserId(user._id, user.services.totp.tempSecret, hashedCodes); | ||
|
|
||
| if (authToken) { | ||
| const hashedToken = Accounts._hashLoginToken(authToken); | ||
|
|
||
| const { modifiedCount } = await Users.removeNonPATLoginTokensExcept(user._id, hashedToken); | ||
|
|
||
| if (modifiedCount > 0) { | ||
| void notifyOnUserChangeAsync(async () => { | ||
| const refreshed = await Users.findOneById(user._id, { | ||
| projection: { 'services.resume.loginTokens': 1, 'services.totp': 1 }, | ||
| }); | ||
| return { | ||
| clientAction: 'updated', | ||
| id: user._id, | ||
| diff: { | ||
| 'services.resume.loginTokens': refreshed?.services?.resume?.loginTokens, | ||
| ...(refreshed?.services?.totp && { 'services.totp.enabled': refreshed.services.totp.enabled }), | ||
| }, | ||
| }; | ||
| }); | ||
| } else { | ||
| void notifyOnUserChange({ clientAction: 'updated', id: user._id, diff: { 'services.totp.enabled': true } }); | ||
| } | ||
| } else { | ||
| void notifyOnUserChange({ clientAction: 'updated', id: user._id, diff: { 'services.totp.enabled': true } }); | ||
| } | ||
|
|
||
| return { codes }; | ||
| }; | ||
|
|
||
| export const regenerateTotpCodes = async (userId: string | null, userToken: string): Promise<{ codes: string[] } | undefined> => { | ||
| const user = await requireUser(userId); | ||
|
|
||
| if (!user.services?.totp?.enabled) { | ||
| throw new Meteor.Error('invalid-totp'); | ||
| } | ||
|
|
||
| const verified = await TOTP.verify({ | ||
| secret: user.services.totp.secret, | ||
| token: userToken, | ||
| userId: user._id, | ||
| backupTokens: user.services.totp.hashedBackup, | ||
| }); | ||
|
|
||
| if (!verified) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const { codes, hashedCodes } = TOTP.generateCodes(); | ||
|
|
||
| await Users.update2FABackupCodesByUserId(user._id, hashedCodes); | ||
|
|
||
| return { codes }; | ||
| }; | ||
|
|
||
| export const codesRemainingTotp = async (userId: string | null): Promise<{ remaining: number }> => { | ||
| const user = await requireUser(userId); | ||
|
|
||
| if (!user.services?.totp?.enabled) { | ||
| throw new Meteor.Error('invalid-totp'); | ||
| } | ||
|
|
||
| return { | ||
| remaining: user.services.totp.hashedBackup?.length ?? 0, | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: REST validation double-hashes the current auth token before excluding it from token revocation. Enabling TOTP via
/users.totp.validatecan revoke the caller’s own login token instead of only other non-PAT sessions.Prompt for AI agents