-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
117 lines (100 loc) · 3.27 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const axios = require('axios');
const NodeCache = require('node-cache');
const chalk = require('chalk');
const refreshTokenStore = {};
const accessTokenCache = new NodeCache({ deleteOnExpire: true });
//==========================================//
// Exchanging Proof for an Access Token //
//==========================================//
const exchangeForTokens = async (userId, exchangeProof) => {
try {
const token = await axios.request({
url: "/token",
method: "post",
baseURL: "https://login.iee.ihu.gr/",
data: exchangeProof
}).then(function(res) {
return res.data;
});
// Usually, this token data should be persisted in a database and associated with
// a user identity.
refreshTokenStore[userId] = token.refresh_token;
accessTokenCache.set(userId, token.access_token, 110); //token expires after 2 minutes
console.log(chalk.green(' > Received an access token and refresh token'));
return token.access_token;
} catch (e) {
console.error(chalk.red(` > Error exchanging ${exchangeProof.grant_type} for access token`));
return JSON.parse(e.response.body);
}
};
const refreshAccessToken = async (userId) => {
const refreshTokenProof = {
grant_type: 'refresh_token',
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code: refreshTokenStore[userId]
};
return await exchangeForTokens(userId, refreshTokenProof);
};
const getAccessToken = async (userId) => {
// If the access token has expired, retrieve
// a new one using the refresh token
if (!accessTokenCache.get(userId)) {
console.log('Refreshing expired access token');
await refreshAccessToken(userId);
}
return accessTokenCache.get(userId);
};
const isAuthorized = (userId) => {
return refreshTokenStore[userId] ? true : false;
};
//====================================================//
// Using an Access Token to Query the APPS API //
//====================================================//
const getProfile = async (accessToken) => {
try {
const config = {
headers:{
'x-access-token': `${accessToken}`,
'Content-Type': 'application/json'
}
};
console.log('===> request.get(\'https://api.iee.ihu.gr/profile\')');
const profile = await axios.get('https://api.iee.ihu.gr/profile', config).then((res) => {
return res.data;
});
return profile;
} catch (err) {
console.error(chalk.red(' > Unable to retrieve profile'));
return err;
}
};
const getAnnouncements = async (accessToken) => {
try {
const config = {
headers:{
'x-access-token': `${accessToken}`,
'Content-Type': 'application/json'
}
};
//pageSize is part of filtering mentioned in the documentation https://github.com/apavlidi/IT_API/wiki/API-Documentation#filtering
console.log('===> request.get(\'https://api.iee.ihu.gr/announcements?pageSize=1\')');
const announcements = await axios.get('https://api.iee.ihu.gr/announcements?pageSize=1', config).then((res) => {
return res.data;
});
console.log(announcements[0]);
return announcements;
} catch (e) {
console.error(chalk.red(' > Unable to retrieve announcements'));
return JSON.parse(e.response.body);
}
};
//export all functions
module.exports = {
exchangeForTokens,
refreshAccessToken,
getAccessToken,
isAuthorized,
getProfile,
getAnnouncements
};