Repository navigation
feat: OAuth login using login codes #40783
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
Changes from 1 commit
33eca86
de6d28b
48a1b7f
8fa7e0b
2b48af6
3f2f250
f8726a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { LoginCodes } from '@rocket.chat/models'; | ||
| import { isLoginCodeRedeemParamsPOST } from '@rocket.chat/rest-typings'; | ||
| import { Accounts } from 'meteor/accounts-base'; | ||
|
|
||
| import { API } from '../api'; | ||
|
|
||
| API.v1.addRoute( | ||
|
yash-rajpal marked this conversation as resolved.
Resolved
|
||
| 'loginCode.redeem', | ||
| { | ||
| authRequired: false, | ||
| validateParams: isLoginCodeRedeemParamsPOST, | ||
| rateLimiterOptions: { intervalTimeInMS: 60000, numRequestsAllowed: 10 }, | ||
| }, | ||
| { | ||
| async post() { | ||
| const { code } = this.bodyParams; | ||
|
|
||
| const loginCode = await LoginCodes.findOneNotExpiredByCode(code); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (!loginCode) { | ||
| throw new Meteor.Error('error-invalid-code', 'Invalid or expired login code'); | ||
| } | ||
|
|
||
| //single-use: remove code before issuing the token | ||
| await LoginCodes.removeByCode(code); | ||
|
|
||
| const { userId } = loginCode; | ||
|
|
||
| const stampedToken = Accounts._generateStampedLoginToken(); | ||
| await Accounts._insertLoginToken(userId, stampedToken); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| return API.v1.success({ | ||
| loginToken: stampedToken.token, | ||
| userId, | ||
| }); | ||
| }, | ||
|
yash-rajpal marked this conversation as resolved.
Resolved
|
||
| }, | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ import { useLoginOtherClients } from './hooks/useLoginOtherClients'; | |
| import { useLoginViaQuery } from './hooks/useLoginViaQuery'; | ||
| import { useMessageLinkClicks } from './hooks/useMessageLinkClicks'; | ||
| import { useNotificationPermission } from './hooks/useNotificationPermission'; | ||
| import { useOAuthLogin } from './hooks/useOAuthLogin'; | ||
| import { useRedirectToSetupWizard } from './hooks/useRedirectToSetupWizard'; | ||
| import { useSettingsOnLoadSiteUrl } from './hooks/useSettingsOnLoadSiteUrl'; | ||
| import { useShareSessionWithOtherClients } from './hooks/useShareSessionWithOtherClients'; | ||
|
|
@@ -73,6 +74,7 @@ const AppLayout = () => { | |
| useCodeHighlight(); | ||
| useLoginViaQuery(); | ||
| useLoginOtherClients(); | ||
| useOAuthLogin(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: OAuth login error handling is overridden by an unconditional Prompt for AI agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Adding Prompt for AI agents |
||
| useShareSessionWithOtherClients(); | ||
| useLoadMissedMessages(); | ||
| useDesktopFavicon(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { useEndpoint, useRouter, useSearchParameter, useLoginWithToken } from '@rocket.chat/ui-contexts'; | ||
| import { useEffect } from 'react'; | ||
|
|
||
| import { buildDeepLinkURL } from '../../../lib/buildAuthDeeplinkURL'; | ||
|
|
||
| export const useOAuthLogin = () => { | ||
| const router = useRouter(); | ||
| const loginCode = useSearchParameter('loginCode'); | ||
| const loginClient = useSearchParameter('loginClient'); | ||
| const redeemLoginCode = useEndpoint('POST', '/v1/loginCode.redeem'); | ||
| const loginWithToken = useLoginWithToken(); | ||
|
|
||
| useEffect(() => { | ||
| if (!loginCode) { | ||
| return; | ||
| } | ||
|
|
||
| let timeout: ReturnType<typeof setTimeout>; | ||
|
yash-rajpal marked this conversation as resolved.
Resolved
|
||
|
|
||
| const handleLogin = async () => { | ||
| try { | ||
| const { loginToken, userId } = await redeemLoginCode({ code: loginCode }); | ||
|
|
||
| if (!loginToken || !userId) { | ||
| throw new Error('Invalid response from login code redemption'); | ||
| } | ||
|
|
||
| if (loginClient === 'desktop' || loginClient === 'mobile') { | ||
| const loginURL = buildDeepLinkURL(loginToken, userId); | ||
| window.location.href = loginURL; | ||
| return; | ||
| } | ||
|
|
||
| await loginWithToken(loginToken); | ||
| } catch (error) { | ||
| console.error('Failed to redeem login code for client redirect', error); | ||
| router.navigate('/login', { replace: true }); | ||
| } finally { | ||
| timeout = setTimeout(() => { | ||
| router.navigate('/home', { replace: true }); | ||
| }, 0); | ||
| } | ||
| }; | ||
|
|
||
| handleLogin(); | ||
|
|
||
| return () => clearTimeout(timeout); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| }, [loginCode, router, redeemLoginCode, loginWithToken, loginClient]); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export interface ILoginCode { | ||
| _id: string; | ||
| userId: string; | ||
| createdAt: Date; | ||
| expireAt: Date; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { ILoginCode } from '@rocket.chat/core-typings'; | ||
| import type { DeleteResult } from 'mongodb'; | ||
|
|
||
| import type { IBaseModel } from './IBaseModel'; | ||
|
|
||
| export interface ILoginCodesModel extends IBaseModel<ILoginCode> { | ||
| createCode(userId: string): Promise<string>; | ||
| findOneNotExpiredByCode(code: string): Promise<ILoginCode | null>; | ||
| removeByCode(code: string): Promise<DeleteResult>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { randomBytes } from 'crypto'; | ||
|
|
||
| import type { ILoginCode } from '@rocket.chat/core-typings'; | ||
| import type { ILoginCodesModel } from '@rocket.chat/model-typings'; | ||
| import type { Db, DeleteResult, IndexDescription } from 'mongodb'; | ||
|
|
||
| import { BaseRaw } from './BaseRaw'; | ||
|
|
||
| const CODE_TTL_SECONDS = 60; | ||
|
|
||
| export class LoginCodesRaw extends BaseRaw<ILoginCode> implements ILoginCodesModel { | ||
| constructor(db: Db) { | ||
| super(db, 'login_codes'); | ||
| } | ||
|
|
||
| override modelIndexes(): IndexDescription[] { | ||
| return [{ key: { expireAt: 1 }, expireAfterSeconds: 0 }]; | ||
| } | ||
|
|
||
| async createCode(userId: string): Promise<string> { | ||
| const now = new Date(); | ||
| const code = randomBytes(32).toString('hex'); | ||
| await this.insertOne({ | ||
| _id: code, | ||
| userId, | ||
| createdAt: now, | ||
| expireAt: new Date(now.getTime() + CODE_TTL_SECONDS * 1000), | ||
|
yash-rajpal marked this conversation as resolved.
Resolved
|
||
| }); | ||
| return code; | ||
| } | ||
|
|
||
| findOneNotExpiredByCode(code: string): Promise<ILoginCode | null> { | ||
| return this.findOne({ | ||
| _id: code, | ||
| expireAt: { $gt: new Date() }, | ||
| }); | ||
| } | ||
|
|
||
| removeByCode(code: string): Promise<DeleteResult> { | ||
|
yash-rajpal marked this conversation as resolved.
Resolved
|
||
| return this.deleteOne({ _id: code }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { ajv } from './Ajv'; | ||
|
|
||
| type LoginCodeRedeemParams = { code: string }; | ||
|
|
||
| const LoginCodeRedeemSchema = { | ||
| type: 'object', | ||
| properties: { | ||
| code: { type: 'string' }, | ||
|
yash-rajpal marked this conversation as resolved.
Resolved
|
||
| }, | ||
| required: ['code'], | ||
| additionalProperties: false, | ||
| }; | ||
|
|
||
| export const isLoginCodeRedeemParamsPOST = ajv.compile<LoginCodeRedeemParams>(LoginCodeRedeemSchema); | ||
|
|
||
| export type LoginCodeEndpoints = { | ||
|
yash-rajpal marked this conversation as resolved.
Resolved
|
||
| '/v1/loginCode.redeem': { | ||
| POST: (params: LoginCodeRedeemParams) => { loginToken: string; userId: string }; | ||
| }; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.