Skip to content

Commit

Permalink
step 6: UserInfo endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Ballinette committed Oct 12, 2019
1 parent 13e5df6 commit 5db7808
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
const
express = require('express'),
bearerToken = require('express-bearer-token'),
session = require('express-session'),
sessionstore = require('sessionstore'),
bodyParser = require('body-parser');
Expand All @@ -16,12 +17,16 @@ const {
userAuthorize,
loginRedirect,
userToken,
userInfo,
} = require('./controllers/oidcProvider');

const memoryStorage = require('./services/memoryStorage');

const app = express();

// parse request headers and add the bearer token into `res.token` if present:
app.use(bearerToken());

// Note this enable to store user session in memory
// As a consequence, restarting the node process will wipe all sessions data
app.use(session({
Expand Down Expand Up @@ -55,6 +60,7 @@ app.get('/logout', localLogout);
app.get('/user/authorize', userAuthorize);
app.get('/user/loginRedirect', loginRedirect);
app.post('/user/token', userToken);
app.get('/api/user', userInfo);

/**** END OIDC End points ****/

Expand Down
19 changes: 19 additions & 0 deletions controllers/oidcProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,27 @@ const userToken = (req, res) => {
}
};

const userInfo = (req, res) => {
const memoryStorage = req.app.get('memoryStorage');

if (! req.token) {
console.error('Unauthorized: missing access Token');
return res.sendStatus(401);
}

const data = memoryStorage.find('tokens', req.token);

if (! data) {
console.error('Unauthorized: no matching accessToken found');
return res.sendStatus(403);
}

return res.json(data);
};

module.exports = {
userAuthorize,
loginRedirect,
userToken,
userInfo,
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"ejs": "^2.6.1",
"express": "^4.16.3",
"express-bearer-token": "^2.4.0",
"express-session": "^1.15.6",
"njwt": "^1.0.0",
"node-csv-query": "^0.1.0",
Expand Down

0 comments on commit 5db7808

Please sign in to comment.