mirror of https://github.com/kern/filepizza
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
786 B
TypeScript
35 lines
786 B
TypeScript
import crypto from 'crypto'
|
|
import { getRedisClient } from './redisClient'
|
|
|
|
function generateHMACKey(
|
|
username: string,
|
|
realm: string,
|
|
password: string,
|
|
): string {
|
|
const str = `${username}:${realm}:${password}`
|
|
return crypto.createHash('md5').update(str).digest('hex')
|
|
}
|
|
|
|
export async function setTurnCredentials(
|
|
username: string,
|
|
password: string,
|
|
ttl: number,
|
|
): Promise<void> {
|
|
if (!process.env.COTURN_ENABLED) {
|
|
return
|
|
}
|
|
|
|
const realm = process.env.TURN_REALM || 'file.pizza'
|
|
|
|
if (!realm) {
|
|
throw new Error('TURN_REALM environment variable not set')
|
|
}
|
|
|
|
const redis = getRedisClient()
|
|
|
|
const hmacKey = generateHMACKey(username, realm, password)
|
|
const key = `turn/realm/${realm}/user/${username}/key`
|
|
|
|
await redis.setex(key, ttl, hmacKey)
|
|
}
|