-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwhoami.js
More file actions
56 lines (51 loc) · 2.43 KB
/
whoami.js
File metadata and controls
56 lines (51 loc) · 2.43 KB
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
'use strict';
const configs = require('../configurations'),
helpers = require('../helpers'),
request = require('superagent'),
express = require('express'),
router = express.Router();
module.exports = function (appContext) {
/* GET /whoami
* Returns the who am I information based on the currently authenticated user.
*/
router.get('/whoami', function (req, res) {
const apiPath = '/d2l/api/lp/1.9/users/whoami';
const accessToken = req.cookies[configs.cookieName].accessToken;
if (accessToken) {
console.log('Attempting to make the who am I call using OAuth 2.0 authentication.');
const whoamiRoute = helpers.createUrl(apiPath, configs);
request
.get( whoamiRoute )
.set('Authorization', `Bearer ${accessToken}`)
.end(function(error, response) {
if (error) {
console.log('Error calling the who am I route', error);
res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
res.status(response.statusCode).send(response.error);
} else {
res.status(200).send(response.text);
}
});
} else {
console.log('Attempting to make the who am I call using ID Key Authentication.');
const userId = req.cookies[configs.cookieName].userId;
const userKey = req.cookies[configs.cookieName].userKey;
const userContext = appContext.createUserContextWithValues(configs.instanceScheme + '//' + configs.instanceUrl, configs.instancePort, userId, userKey);
const apiCallUrl = userContext.createAuthenticatedUrl(apiPath, 'GET');
request
.get( apiCallUrl )
.end(function(error, response) {
if (error) {
console.log('Error calling the who am I route', error);
res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
res.status(response.statusCode).send(response.error);
} else {
res.status(200).send(response.text);
}
});
}
});
return router;
};