I use the following code which works as expected. The code uses open ID connect
to login user. Because I'm pretty new to node and express, it will be great if I can get some tips for the async
usage- e.g. if I’m doing it and the error handling correctly.
The code is doing connect via oidc layer
https://en.wikipedia.org/wiki/OpenID_Connect
which does a user login according to client secret and client id, then the authorization server is calling to the redirect
route of the application and if everything is OK the user logged in to the system.
This is the index.js
const express = require('express');
const logon = require('./logon');
const app = express();
const port = process.env.PORT || 4000;
logon(app)
.then(() => {
console.log('process started');
});
app.use(express.json());
app.listen(port,
() => console.log(`listening on port: ${port}`));
This is the logon.js
const { Issuer, Strategy } = require('openid-client');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const azpi = require('./azpi');
const bodyParser = require('body-parser');
const passport = require('passport');
module.exports = async (app) => {
let oSrv;
const durl = `${process.env.srvurl}/.well-known/openid-configuration`;
try {
oSrv = await Issuer.discover(durl);
} catch (err) {
console.log('error occured', err);
return;
}
app.get('/', prs(), passport.authenticate('oidc'));
const oSrvCli = new oSrv.Client({
client_id: process.env.ci,
client_secret: process.env.cs,
token_endpoint_auth_method: 'client_secret_basic',
});
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
const cfg = {
scope: 'openid',
redirect_uri: process.env.ruri,
response_type: 'code',
response_mode: 'form_post',
};
const prs = () => (req, res, next) => {
passport.use(
'oidc',
new Strategy({ oSrvCli , cfg }, (tokenset, done) => {
const claims = tokenset.claims();
const user = {
name: claims.name,
id: claims.sub,
id_token: tokenset.id_token,
};
return done(null, user);
}),
);
next();
};
app.use(
bodyParser.urlencoded({
extended: false,
}),
);
app.use(cookieParser('csec'));
app.use(
cookieSession({
name: 'zta-auth',
secret: 'csect',
}),
);
app.use(passport.initialize());
app.use(passport.session());
app.get('/redirect', async (req, res, next) => {
passport.authenticate('oidc', async (err, user) => {
if (err) {
console.log(`Authentication failed: ${err}`);
return next(err);
}
if (!user) {
return res.send('no identity');
}
req.login(user, async (e) => {
if (e) {
console.log('not able to login', e);
return next(e);
}
try {
const url = await azpi.GetUsers(user.id_token);
return res.redirect(url);
} catch (er) {
res.send(er.message);
}
});
})(req, res, next);
});
};
Is my async code usage is okay?