forked from mozilla/send
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfxa.js
54 lines (50 loc) · 1.24 KB
/
fxa.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
const fetch = require('node-fetch');
const config = require('./config');
const KEY_SCOPE = config.fxa_key_scope;
let fxaConfig = null;
let lastConfigRefresh = 0;
async function getFxaConfig() {
if (fxaConfig && Date.now() - lastConfigRefresh < 1000 * 60 * 5) {
return fxaConfig;
}
try {
const res = await fetch(
`${config.fxa_url}/.well-known/openid-configuration`,
{ timeout: 3000 }
);
fxaConfig = await res.json();
fxaConfig.key_scope = KEY_SCOPE;
lastConfigRefresh = Date.now();
} catch (e) {
// continue with previous fxaConfig
}
return fxaConfig;
}
module.exports = {
getFxaConfig,
verify: async function(token) {
if (!token) {
return null;
}
const c = await getFxaConfig();
try {
const verifyUrl = c.jwks_uri.replace('jwks', 'verify'); //HACK
const result = await fetch(verifyUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token })
});
const info = await result.json();
if (
info.scope &&
Array.isArray(info.scope) &&
info.scope.includes(KEY_SCOPE)
) {
return info.user;
}
} catch (e) {
// gulp
}
return null;
}
};