Skip to content

Commit

Permalink
step 1 : userAuthorize - init
Browse files Browse the repository at this point in the history
  • Loading branch information
Ballinette committed Oct 12, 2019
1 parent d6a1277 commit 2b6bc99
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const {
localLogout
} = require('./controllers/localAuthController');

const {
userAuthorize,
} = require('./controllers/oidcProvider');

const app = express();

// Note this enable to store user session in memory
Expand Down Expand Up @@ -41,6 +45,11 @@ app.post('/login', localLogin);

app.get('/logout', localLogout);

/**** OIDC End points ****/
app.get('/user/authorize', userAuthorize);

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

// Setting app port
const port = process.env.PORT || '4000';

Expand Down
11 changes: 11 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
issuer: `${process.env.ISSUER || 'http://localhost:4000'}`,
clients: [
{
client_id: '09a1a257648c1742c74d6a3d84b31943',
client_secret: '7ae4fef2aab63fb78d777fe657b7536f',
redirect_uri: 'http://localhost:3000/login-callback',
redirect_logout_uri: 'http://localhost:3000/logout-callback',
},
],
};
43 changes: 43 additions & 0 deletions controllers/oidcProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const config = require('../config');

// local helper
const findClient = client_id => config.clients.find(item => item.client_id === client_id);

const userAuthorize = (req, res) => {
// check the required parameters
for (const field of ['scope', 'response_type', 'client_id', 'redirect_uri']) {
if (!req.query[field]) {
console.error(`missing "${field}" parameter`);
return res.sendStatus(400);
}
}
if (! req.query.scope.includes('openid')) {
console.error('missing "openid" scope');
return res.sendStatus(400);
}
if (req.query.response_type !== 'code') {
console.error('only Authorization Code supported: response_type MUST be equal to "code"');
return res.sendStatus(400);
}

// check client config:
const client = findClient(req.query.client_id);
if (! client) {
console.error('Client ID not found');
return res.sendStatus(400);
}
if (client.redirect_uri !== req.query.redirect_uri) {
console.error('Mismatch redirect_uri');
return res.sendStatus(400);
}

// store input request parameters into session to be used after authentification
req.session.oidc_query = req.query;

// redirect to login form
return res.redirect('/login');
};

module.exports = {
userAuthorize,
}

0 comments on commit 2b6bc99

Please sign in to comment.