-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (42 loc) · 1.27 KB
/
index.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
"use strict";
// Helper functions for accessing the mural API. {{clientSecret}}
var Parse = require('parse/node').Parse;
const axios = require('axios');
const MURAL_HOST = 'app.mural.co';
function validateAuthData(authData) {
return verifyIdToken(authData)
} // Returns a promise that fulfills iff this app id is valid.
function validateAppId() {
return Promise.resolve();
} // A promisey wrapper for api requests
const prepareAxiosErrorMessage = (error, additionalMessage) =>
`${error.response.data.error_description || error.message} ${
additionalMessage ? additionalMessage : ""
}`;
const muralPublicMe = async (token) => {
try {
const res = await axios.get(
`https://${MURAL_HOST}/api/public/v1/users/me`,
{ headers: { Authorization: `Bearer ${token}` } },
);
return res.data;
} catch (error) {
throw new Error(prepareAxiosErrorMessage(error));
}
};
async function verifyIdToken({ accessToken, id }) {
let data;
try {
data = await muralPublicMe(accessToken);
} catch(error) {
throw error;
}
if (data.value.id === id) {
return;
}
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Mural auth is invalid for this user.', data);
}
module.exports = {
validateAppId: validateAppId,
validateAuthData: validateAuthData
};