refactor: satisfy linter requirements

This commit is contained in:
Marcell Fülöp 2023-02-24 21:09:21 +00:00
parent baee4641db
commit 6f98fa61e4
7 changed files with 84 additions and 89 deletions

View File

@ -88,7 +88,7 @@ module.exports = {
* @param {String} [issuer]
* @returns {Promise}
*/
getTokenFromOAuthClaim: (data, issuer) => {
getTokenFromOAuthClaim: (data) => {
let Token = new TokenModel();
data.scope = 'user';
@ -118,14 +118,9 @@ module.exports = {
return Token.create({ iss, attrs, scope, expiresIn })
.then((signed) => {
return {
token: signed.token,
expires: expiry.toISOString()
};
return { token: signed.token, expires: expiry.toISOString() };
});
});
}
);
},
/**

View File

@ -27,7 +27,7 @@ router.get('/', (req, res/*, next*/) => {
router.use('/schema', require('./schema'));
router.use('/tokens', require('./tokens'));
router.use('/oidc', require('./oidc'))
router.use('/oidc', require('./oidc'));
router.use('/users', require('./users'));
router.use('/audit-log', require('./audit-log'));
router.use('/reports', require('./reports'));

View File

@ -29,15 +29,15 @@ router
*
* Retrieve all users
*/
.get(jwtdecode(), async (req, res, next) => {
console.log("oidc: init flow");
.get(jwtdecode(), async (req, res) => {
console.log('oidc: init flow');
settingModel
.query()
.where({id: 'oidc-config'})
.first()
.then( row => getInitParams(req, row))
.then( params => redirectToAuthorizationURL(res, params))
.catch( err => redirectWithError(res, err));
.then((row) => getInitParams(req, row))
.then((params) => redirectToAuthorizationURL(res, params))
.catch((err) => redirectWithError(res, err));
});
@ -58,15 +58,15 @@ router
*
* Retrieve a specific user
*/
.get(jwtdecode(), async (req, res, next) => {
console.log("oidc: callback");
.get(jwtdecode(), async (req, res) => {
console.log('oidc: callback');
settingModel
.query()
.where({id: 'oidc-config'})
.first()
.then( settings => validateCallback(req, settings))
.then( token => redirectWithJwtToken(res, token))
.catch( err => redirectWithError(res, err));
.then((settings) => validateCallback(req, settings))
.then((token) => redirectWithJwtToken(res, token))
.catch((err) => redirectWithError(res, err));
});
/**
@ -74,11 +74,11 @@ router
*
* @param {Setting} row
* */
let getClient = async row => {
let getClient = async (row) => {
let issuer;
try {
issuer = await oidc.Issuer.discover(row.meta.issuerURL);
} catch(err) {
} catch (err) {
throw new error.AuthError(`Discovery failed for the specified URL with message: ${err.message}`);
}
@ -88,7 +88,7 @@ let getClient = async row => {
redirect_uris: [row.meta.redirectURL],
response_types: ['code'],
});
}
};
/**
* Generates state, nonce and authorization url.
@ -98,18 +98,18 @@ let getClient = async row => {
* @return { {String}, {String}, {String} } state, nonce and url
* */
let getInitParams = async (req, row) => {
let client = await getClient(row);
let state = crypto.randomUUID();
let nonce = crypto.randomUUID();
let url = client.authorizationUrl({
let client = await getClient(row),
state = crypto.randomUUID(),
nonce = crypto.randomUUID(),
url = client.authorizationUrl({
scope: 'openid email profile',
resource: `${req.protocol}://${req.get('host')}${req.originalUrl}`,
state,
nonce,
})
});
return { state, nonce, url };
}
};
/**
* Parses state and nonce from cookie during the callback phase.
@ -117,13 +117,13 @@ let getInitParams = async (req, row) => {
* @param {Request} req
* @return { {String}, {String} } state and nonce
* */
let parseStateFromCookie = req => {
let parseStateFromCookie = (req) => {
let state, nonce;
let cookies = req.headers.cookie.split(';');
for (cookie of cookies) {
for (let cookie of cookies) {
if (cookie.split('=')[0].trim() === 'npm_oidc') {
let raw = cookie.split('=')[1];
let val = raw.split('--');
let raw = cookie.split('=')[1],
val = raw.split('--');
state = val[0].trim();
nonce = val[1].trim();
break;
@ -131,7 +131,7 @@ let parseStateFromCookie = req => {
}
return { state, nonce };
}
};
/**
* Executes validation of callback parameters.
@ -149,24 +149,24 @@ let validateCallback = async (req, settings) => {
let claims = tokenSet.claims();
console.log('oidc: authentication successful for email', claims.email);
return internalToken.getTokenFromOAuthClaim({ identity: claims.email })
}
return internalToken.getTokenFromOAuthClaim({ identity: claims.email });
};
let redirectToAuthorizationURL = (res, params) => {
console.log('oidc: init flow > url > ', params.url);
res.cookie("npm_oidc", params.state + '--' + params.nonce);
res.cookie('npm_oidc', params.state + '--' + params.nonce);
res.redirect(params.url);
}
};
let redirectWithJwtToken = (res, token) => {
res.cookie('npm_oidc', token.token + '---' + token.expires);
res.redirect('/login');
}
};
let redirectWithError = (res, error) => {
console.log('oidc: callback error: ', error);
res.cookie('npm_oidc_error', error.message);
res.redirect('/login');
}
};
module.exports = router;

View File

@ -71,14 +71,14 @@ router
.then((row) => {
if (row.id === 'oidc-config') {
// redact oidc configuration via api
let m = row.meta
let m = row.meta;
row.meta = {
name: m.name,
enabled: m.enabled === true && !!(m.clientID && m.clientSecret && m.issuerURL && m.redirectURL && m.name)
};
// remove these temporary cookies used during oidc authentication
res.clearCookie('npm_oidc')
res.clearCookie('npm_oidc_error')
res.clearCookie('npm_oidc');
res.clearCookie('npm_oidc_error');
}
res.status(200)
.send(row);

View File

@ -60,7 +60,7 @@ function fetch(verb, path, data, options) {
beforeSend: function (xhr) {
// allow unauthenticated access to OIDC configuration
if (path === "settings/oidc-config") return;
if (path === 'settings/oidc-config') return;
xhr.setRequestHeader('Authorization', 'Bearer ' + (token ? token.t : null));
},

View File

@ -28,16 +28,16 @@ module.exports = Mn.View.extend({
let data = this.ui.form.serializeJSON();
data.id = this.model.get('id');
if (data.meta.enabled) {
data.meta.enabled = data.meta.enabled === "on" || data.meta.enabled === "true";
data.meta.enabled = data.meta.enabled === 'on' || data.meta.enabled === 'true';
}
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
App.Api.Settings.update(data)
.then(result => {
.then((result) => {
view.model.set(result);
App.UI.closeModal();
})
.catch(err => {
.catch((err) => {
alert(err.message);
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
});

View File

@ -31,12 +31,12 @@ module.exports = Mn.View.extend({
.then(() => {
window.location = '/';
})
.catch(err => {
.catch((err) => {
this.ui.error.text(err.message).show();
this.ui.button.removeClass('btn-loading').prop('disabled', false);
});
},
'click @ui.oidcButton': function(e) {
'click @ui.oidcButton': function() {
this.ui.identity.prop('disabled', true);
this.ui.secret.prop('disabled', true);
this.ui.button.prop('disabled', true);
@ -80,7 +80,7 @@ module.exports = Mn.View.extend({
}
// fetch oidc configuration and show alternative action button if enabled
let response = await Api.Settings.getById("oidc-config");
let response = await Api.Settings.getById('oidc-config');
if (response && response.meta && response.meta.enabled === true) {
this.ui.oidcProvider.html(response.meta.name);
this.ui.oidcLogin.show();