forked from quavedev/apple-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
72 lines (66 loc) · 1.77 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* global Base64 */
export function getClientIdFromOptions(options, servicesConfig) {
const { shard } = options;
if (shard) {
return servicesConfig[`clientId-${shard}`];
}
return servicesConfig.clientId;
}
export function getAppIdFromOptions(options) {
const { appId } = options;
return appId || null;
}
export function getServiceConfiguration({ appId = null }) {
// eslint-disable-next-line no-undef
const service = ServiceConfiguration.configurations.findOne({
service: 'apple',
});
if (!appId) {
return service;
}
if (
!Array.isArray(service.apps) ||
(Meteor.isServer && !Array.isArray(service.secret))
) {
throw new Error('No service configuration found for multiple apps');
}
const appIdMap = service.apps.reduce(
(acc, { appId: id, ...appConfig }) => ({
...acc,
[id]: appConfig,
}),
{}
);
const secretMap = ((Meteor.isServer && service.secret) || []).reduce(
(acc, { appId: id, secret }) => ({
...acc,
[id]: secret,
}),
{}
);
if (!appIdMap[appId] || (Meteor.isServer && !secretMap[appId])) {
throw new Error(`No service configuration found for ${appId}`);
}
return {
...appIdMap[appId],
secret: secretMap[appId],
};
}
export function stateParam({
loginStyle,
credentialToken,
redirectUrl,
...rest
}) {
const state = {
loginStyle,
credentialToken,
isCordova: Meteor.isCordova,
...rest,
};
if (loginStyle === 'redirect' || loginStyle === 'popup') state.redirectUrl = redirectUrl || `${window.location}`;
// Encode base64 as not all login services URI-encode the state
// parameter when they pass it back to us.
// Use the 'base64' package here because 'btoa' isn't supported in IE8/9.
return Base64.encode(JSON.stringify(state));
}