Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/push/providers.js
// @flow | // @flow | ||||
import apn from '@parse/node-apn'; | import apn from '@parse/node-apn'; | ||||
import type { Provider as APNProvider } from '@parse/node-apn'; | import type { Provider as APNProvider } from '@parse/node-apn'; | ||||
import fcmAdmin from 'firebase-admin'; | import fcmAdmin from 'firebase-admin'; | ||||
import type { FirebaseApp } from 'firebase-admin'; | import type { FirebaseApp } from 'firebase-admin'; | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import fetch from 'node-fetch'; | |||||
import webpush from 'web-push'; | import webpush from 'web-push'; | ||||
import type { PlatformDetails } from 'lib/types/device-types'; | import type { PlatformDetails } from 'lib/types/device-types'; | ||||
import { getCommConfig } from 'lib/utils/comm-config.js'; | import { getCommConfig } from 'lib/utils/comm-config.js'; | ||||
type APNPushProfile = 'apn_config' | 'comm_apn_config'; | type APNPushProfile = 'apn_config' | 'comm_apn_config'; | ||||
function getAPNPushProfileForCodeVersion( | function getAPNPushProfileForCodeVersion( | ||||
platformDetails: PlatformDetails, | platformDetails: PlatformDetails, | ||||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | |||||
async function ensureWebPushInitialized() { | async function ensureWebPushInitialized() { | ||||
if (cachedWebPushConfig) { | if (cachedWebPushConfig) { | ||||
return; | return; | ||||
} | } | ||||
await getWebPushConfig(); | await getWebPushConfig(); | ||||
} | } | ||||
type WNSConfig = { +tenantID: string, +appID: string, +secret: string }; | |||||
type WNSAccessToken = { +token: string, +expires: number }; | |||||
let wnsAccessToken: ?WNSAccessToken = null; | |||||
async function getWNSToken(): Promise<?string> { | |||||
const expiryWindowInMs = 10_000; | |||||
if ( | |||||
wnsAccessToken && | |||||
wnsAccessToken.expires >= Date.now() - expiryWindowInMs | |||||
) { | |||||
return wnsAccessToken.token; | |||||
} | |||||
const config = await getCommConfig<WNSConfig>({ | |||||
folder: 'secrets', | |||||
name: 'wns_config', | |||||
}); | |||||
if (!config) { | |||||
return null; | |||||
} | |||||
const params = new URLSearchParams(); | |||||
params.append('grant_type', 'client_credentials'); | |||||
params.append('client_id', config.appID); | |||||
params.append('client_secret', config.secret); | |||||
params.append('scope', 'https://wns.windows.com/.default'); | |||||
try { | |||||
const response = await fetch( | |||||
`https://login.microsoftonline.com/${config.tenantID}/oauth2/v2.0/token`, | |||||
{ method: 'POST', body: params }, | |||||
); | |||||
if (!response.ok) { | |||||
console.error('Failure when getting the WNS token: ', response); | |||||
return null; | |||||
} | |||||
const responseJson = await response.json(); | |||||
wnsAccessToken = { | |||||
token: responseJson['access_token'], | |||||
expires: Date.now() + responseJson['expires_in'] * 1000, | |||||
}; | |||||
} catch (err) { | |||||
console.error('Failure when getting the WNS token: ', err); | |||||
return null; | |||||
} | |||||
return wnsAccessToken?.token; | |||||
} | |||||
export { | export { | ||||
getAPNPushProfileForCodeVersion, | getAPNPushProfileForCodeVersion, | ||||
getFCMPushProfileForCodeVersion, | getFCMPushProfileForCodeVersion, | ||||
getAPNProvider, | getAPNProvider, | ||||
getFCMProvider, | getFCMProvider, | ||||
endFirebase, | endFirebase, | ||||
endAPNs, | endAPNs, | ||||
getAPNsNotificationTopic, | getAPNsNotificationTopic, | ||||
getWebPushConfig, | getWebPushConfig, | ||||
ensureWebPushInitialized, | ensureWebPushInitialized, | ||||
getWNSToken, | |||||
}; | }; |