/
istok
/
idp_server
Обзор
Документация
Войти
/
istok
/
idp_server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
dev
apps/server/test/utils/telegram.ts
54 строки
1 KB
ArtemAstakhov
Move client to nextjs
22 ноя 2025, 22:42
22 ноя 2025, 22:42
35e7508
Код
Авторство
О чём код?
import * as crypto from 'crypto'; import { assign } from 'lodash-es'; import { DateTime } from 'luxon'; import { TelegramWidgetData } from 'src/application/auth/dto/telegram-auth.dto'; export const TELEGRAM_BOT_TOKEN = 'test-bot-token-12345'; const secretKey = crypto .createHash('sha256') .update(TELEGRAM_BOT_TOKEN) .digest(); export function createWidgetData( overrides?: Partial<TelegramWidgetData>, ): TelegramWidgetData { const baseData: TelegramWidgetData = assign( { id: 123456789, first_name: 'John', last_name: 'Doe', username: 'johndoe', photo_url: 'https://example.com/photo.jpg', auth_date: Math.floor(DateTime.now().toSeconds()), hash: '', }, overrides, ); const dataCheckArray: string[] = []; dataCheckArray.push(`auth_date=${baseData.auth_date}`); dataCheckArray.push(`first_name=${baseData.first_name}`); dataCheckArray.push(`id=${baseData.id}`); if (baseData.last_name) { dataCheckArray.push(`last_name=${baseData.last_name}`); } if (baseData.photo_url) { dataCheckArray.push(`photo_url=${baseData.photo_url}`); } if (baseData.username) { dataCheckArray.push(`username=${baseData.username}`); } dataCheckArray.sort(); const dataCheckString = dataCheckArray.join('\n'); const hash = crypto .createHmac('sha256', secretKey) .update(dataCheckString) .digest('hex'); return { ...baseData, hash: overrides?.hash ?? hash, }; }