-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfig.js
71 lines (57 loc) · 2.08 KB
/
config.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
import firebaseJson from './firebase.json' with { type: 'json' };
import { getFirestore } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';
const is_localhost = process.env.NODE_ENV === 'localhost';
const env = is_localhost ? 'development' : 'production';
const _config = (await import(`./${env}.config.json`, {with:{type: 'json'}})).default;
function generateApkKeyHash(fingerprint) {
const hexString = fingerprint.replace(/:/g, '');
// Convert hex string to byte array
const bytes = new Uint8Array(hexString.length / 2);
for (let i = 0; i < hexString.length; i += 2) {
bytes[i / 2] = parseInt(hexString.substr(i, 2), 16);
}
// Encode byte array to base64url
const base64url = btoa(String.fromCharCode(...bytes))
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
return `android:apk-key-hash:${base64url}`;
}
const { hostname, port, associated_domains = [], secret, rp_name, project_name } = _config;
const domain = port ? `${hostname}:${port}` : hostname;
const origin = is_localhost ? `http://${domain}` : `https://${domain}`;
const associated_origins = [];
for (let domain of associated_domains) {
const associated_origin = generateApkKeyHash(domain.sha256_cert_fingerprints);
associated_origins.push(associated_origin);
}
const config = {
env,
hostname,
domain,
origin,
secret: 'set your own secret in the config file',
rp_name: rp_name || 'Passkeys Demo',
project_name: project_name || 'passkeys-demo',
associated_domains: [
origin,
...associated_domains
],
associated_origins: [
origin,
...associated_origins
]
};
function initializeFirestore() {
if (is_localhost) {
process.env.GOOGLE_CLOUD_PROJECT = config.project_name;
process.env.FIRESTORE_EMULATOR_HOST = `${firebaseJson.emulators.firestore.host}:${firebaseJson.emulators.firestore.port}`;
}
initializeApp();
const store = getFirestore(process.env.FIRESTORE_DATABASENAME || '');
store.settings({ignoreUndefinedProperties: true});
return store;
}
const store = initializeFirestore();
export { config, store };