Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3520057
D7267.id24939.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D7267.id24939.diff
View Options
diff --git a/keyserver/src/push/providers.js b/keyserver/src/push/providers.js
--- a/keyserver/src/push/providers.js
+++ b/keyserver/src/push/providers.js
@@ -5,6 +5,7 @@
import fcmAdmin from 'firebase-admin';
import type { FirebaseApp } from 'firebase-admin';
import invariant from 'invariant';
+import fetch from 'node-fetch';
import webpush from 'web-push';
import type { PlatformDetails } from 'lib/types/device-types';
@@ -121,6 +122,57 @@
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 {
getAPNPushProfileForCodeVersion,
getFCMPushProfileForCodeVersion,
@@ -131,4 +183,5 @@
getAPNsNotificationTopic,
getWebPushConfig,
ensureWebPushInitialized,
+ getWNSToken,
};
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 23, 11:21 PM (18 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2697276
Default Alt Text
D7267.id24939.diff (2 KB)
Attached To
Mode
D7267: [keyserver] Get WNS authorization tokens
Attached
Detach File
Event Timeline
Log In to Comment