Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ import { TelegramData } from '../types'
import { templateNotification } from '../../notification/services/notification'

const MODULE_ID = 'api::telegram-subscription.telegram-subscription'
const telegramSecret = env('TELEGRAM_SECRET') as string

const SEND_MESSAGE_URL = `https://api.telegram.org/bot${env('TELEGRAM_SECRET')}/sendMessage`
const SEND_MESSAGE_URL = `https://api.telegram.org/bot${telegramSecret}/sendMessage`

export default factories.createCoreService(MODULE_ID, ({strapi}) => {
return {
async verifyTgAuthentication(data: TelegramData) {
if (!telegramSecret) {
throw new Error('verifyTgAuthentication - telegram secret is not set!')
}

const dataString = Object.keys(data).reduce((acc, key) => {
if (key === 'hash') return acc

acc.push(`${key}=${data[key]}`)
return acc
}, []).sort().join('\n')

const secretHash = crypto.createHash('sha256').update(env('TELEGRAM_SECRET')).digest('base64')
const secretHash = crypto.createHash('sha256').update(telegramSecret).digest('base64')
const result = crypto.createHmac('sha256', new Buffer(secretHash, 'base64')).update(dataString).digest('hex')

return result === data.hash
Expand Down
Loading