Merge pull request #4153 from omnivore-app/fix/intercom

have separate intercom secret in env var and return the hash based on the client
This commit is contained in:
Hongbo Wu
2024-07-05 18:00:05 +08:00
committed by GitHub
2 changed files with 28 additions and 6 deletions

View File

@ -4,6 +4,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { createHmac } from 'crypto'
import * as httpContext from 'express-http-context2'
import { isError } from 'lodash'
import { Highlight } from '../entity/highlight'
import { LibraryItem, LibraryItemState } from '../entity/library_item'
@ -355,14 +356,26 @@ export const functionResolvers = {
},
User: {
async intercomHash(user: User) {
if (env.intercom.secretKey) {
const userIdentifier = user.id.toString()
let secret: string
return createHmac('sha256', env.intercom.secretKey)
.update(userIdentifier)
.digest('hex')
const client = httpContext.get('client') as string
switch (client.toLowerCase()) {
case 'ios':
secret = env.intercom.iosSecret
break
case 'android':
secret = env.intercom.androidSecret
break
default:
secret = env.intercom.webSecret
}
return undefined
if (!secret) {
return undefined
}
const userIdentifier = user.id
return createHmac('sha256', secret).update(userIdentifier).digest('hex')
},
async features(_: User, __: Record<string, unknown>, ctx: ResolverContext) {
if (!ctx.claims?.uid) {

View File

@ -54,6 +54,9 @@ export interface BackendEnv {
intercom: {
token: string
secretKey: string
webSecret: string
iosSecret: string
androidSecret: string
}
sentry: {
dsn: string
@ -193,6 +196,9 @@ const nullableEnvVars = [
'PG_REPLICA_USER',
'PG_REPLICA_PASSWORD',
'PG_REPLICA_DB',
'INTERCOM_WEB_SECRET',
'INTERCOM_IOS_SECRET',
'INTERCOM_ANDROID_SECRET',
] // Allow some vars to be null/empty
const envParser =
@ -268,6 +274,9 @@ export function getEnv(): BackendEnv {
const intercom = {
token: parse('INTERCOM_TOKEN'),
secretKey: parse('INTERCOM_SECRET_KEY'),
webSecret: parse('INTERCOM_WEB_SECRET'),
iosSecret: parse('INTERCOM_IOS_SECRET'),
androidSecret: parse('INTERCOM_ANDROID_SECRET'),
}
const sentry = {
dsn: parse('SENTRY_DSN'),