Skip to content

feat: enable multiple auth methods #963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
884f0a6
feat(auth): refactor and improve existing auth methods
jescalada Feb 16, 2025
ce6023b
feat(auth): configure passport with all enabled auth methods
jescalada Feb 16, 2025
68fa115
test(auth): fix tests for multiple auth methods
jescalada Feb 16, 2025
0a74501
Merge branch 'oidc-implementation' into enable-multiple-auth-methods
jescalada Feb 21, 2025
627c0ea
fix(auth): refactor how auth strategies are loaded into API route mid…
jescalada Feb 21, 2025
db30bef
Merge branch 'layout-auth-decoupling' into enable-multiple-auth-methods
jescalada Mar 10, 2025
b58d3a8
fix(auth): fix OIDC login e2e test
jescalada Mar 10, 2025
291c179
fix(auth): fix linter issues
jescalada Mar 10, 2025
2f19f82
fix(auth): try to fix ESM issue on openid-client import
jescalada Mar 10, 2025
1397265
Merge branch 'main' into enable-multiple-auth-methods
jescalada Apr 2, 2025
0ddd27b
fix(auth): fix bug when calling createUser on admin creation
jescalada May 21, 2025
3484694
Merge remote-tracking branch 'origin/main' into enable-multiple-auth-…
jescalada May 21, 2025
e32408c
chore(auth): add sample oidc config
jescalada May 21, 2025
c83421d
fix: admin to dashboard rename issues
jescalada May 21, 2025
bab0061
fix: failing Cypress test
jescalada May 21, 2025
70dd346
test(auth): add proxyquire for mocking
jescalada May 21, 2025
71e7e52
test(auth): improve test coverage
jescalada May 21, 2025
678d932
test(auth): fix service close issue
jescalada May 21, 2025
ee8f2c1
test(auth): add extra tests and fix linter issues
jescalada May 21, 2025
e96e876
fix: replaced loading text with actual spinner and removed debug lines
jescalada May 21, 2025
fd962d2
feat: add snackbar for repo fetching errors
jescalada May 21, 2025
304a2ec
fix: revert react missing from PrivateRoute scope
jescalada May 21, 2025
ddc20bf
Merge remote-tracking branch 'origin/main' into enable-multiple-auth-…
jescalada May 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ describe('Login page', () => {

// Validates that OIDC is configured correctly
it('should redirect to /oidc', () => {
// Set intercept first, since redirect on click can be quick
cy.intercept('GET', '/api/auth/oidc').as('oidcRedirect');
cy.get('[data-test="oidc-login"]').click();
cy.url().should('include', '/oidc');
cy.wait('@oidcRedirect');
});
});
});
77 changes: 73 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"mocha": "^10.8.2",
"nyc": "^17.0.0",
"prettier": "^3.0.0",
"proxyquire": "^2.1.3",
"sinon": "^19.0.2",
"ts-mocha": "^11.1.0",
"ts-node": "^10.9.2",
Expand Down
11 changes: 11 additions & 0 deletions proxy.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
"baseDN": "",
"searchBase": ""
}
},
{
"type": "openidconnect",
"enabled": false,
"oidcConfig": {
"issuer": "",
"clientID": "",
"clientSecret": "",
"callbackURL": "",
"scope": ""
}
}
],
"api": {
Expand Down
22 changes: 12 additions & 10 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,29 @@ export const getDatabase = () => {
throw Error('No database cofigured!');
};

// Gets the configured authentication method, defaults to local
export const getAuthentication = () => {
/**
* Get the list of enabled authentication methods
* @return {Array} List of enabled authentication methods
*/
export const getAuthMethods = () => {
if (_userSettings !== null && _userSettings.authentication) {
_authentication = _userSettings.authentication;
}
for (const ix in _authentication) {
if (!ix) continue;
const auth = _authentication[ix];
if (auth.enabled) {
return auth;
}

const enabledAuthMethods = _authentication.filter((auth) => auth.enabled);

if (enabledAuthMethods.length === 0) {
throw new Error("No authentication method enabled");
}

throw Error('No authentication cofigured!');
return enabledAuthMethods;
};

// Log configuration to console
export const logConfiguration = () => {
console.log(`authorisedList = ${JSON.stringify(getAuthorisedList())}`);
console.log(`data sink = ${JSON.stringify(getDatabase())}`);
console.log(`authentication = ${JSON.stringify(getAuthentication())}`);
console.log(`authentication = ${JSON.stringify(getAuthMethods())}`);
console.log(`rateLimit = ${JSON.stringify(getRateLimit())}`);
};

Expand Down
90 changes: 49 additions & 41 deletions src/service/passport/activeDirectory.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const configure = () => {
const passport = require('passport');
const ActiveDirectoryStrategy = require('passport-activedirectory');
const config = require('../../config').getAuthentication();
const adConfig = config.adConfig;
const ActiveDirectoryStrategy = require('passport-activedirectory');
const ldaphelper = require('./ldaphelper');

const configure = (passport) => {
const db = require('../../db');
const userGroup = config.userGroup;
const adminGroup = config.adminGroup;
const domain = config.domain;

// We can refactor this by normalizing auth strategy config and pass it directly into the configure() function,
// ideally when we convert this to TS.
const authMethods = require('../../config').getAuthMethods();
const config = authMethods.find((method) => method.type.toLowerCase() === "activeDirectory");
const adConfig = config.adConfig;

Check warning on line 11 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L9-L11

Added lines #L9 - L11 were not covered by tests

const { userGroup, adminGroup, domain } = config;

Check warning on line 13 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L13

Added line #L13 was not covered by tests

console.log(`AD User Group: ${userGroup}, AD Admin Group: ${adminGroup}`);

const ldaphelper = require('./ldaphelper');
passport.use(
new ActiveDirectoryStrategy(
{
Expand All @@ -19,42 +22,47 @@
ldap: adConfig,
},
async function (req, profile, ad, done) {
profile.username = profile._json.sAMAccountName.toLowerCase();
profile.email = profile._json.mail;
profile.id = profile.username;
req.user = profile;

console.log(
`passport.activeDirectory: resolved login ${
profile._json.userPrincipalName
}, profile=${JSON.stringify(profile)}`,
);
// First check to see if the user is in the usergroups
const isUser = await ldaphelper.isUserInAdGroup(profile.username, domain, userGroup);

if (!isUser) {
const message = `User it not a member of ${userGroup}`;
return done(message, null);
}
try {
profile.username = profile._json.sAMAccountName?.toLowerCase();
profile.email = profile._json.mail;
profile.id = profile.username;
req.user = profile;

Check warning on line 29 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L25-L29

Added lines #L25 - L29 were not covered by tests

// Now check if the user is an admin
const isAdmin = await ldaphelper.isUserInAdGroup(profile.username, domain, adminGroup);
console.log(

Check warning on line 31 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L31

Added line #L31 was not covered by tests
`passport.activeDirectory: resolved login ${
profile._json.userPrincipalName
}, profile=${JSON.stringify(profile)}`,
);
// First check to see if the user is in the usergroups
const isUser = await ldaphelper.isUserInAdGroup(profile.username, domain, userGroup);

Check warning on line 37 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L37

Added line #L37 was not covered by tests

profile.admin = isAdmin;
console.log(`passport.activeDirectory: ${profile.username} admin=${isAdmin}`);
if (!isUser) {
const message = `User it not a member of ${userGroup}`;
return done(message, null);

Check warning on line 41 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L39-L41

Added lines #L39 - L41 were not covered by tests
}

const user = {
username: profile.username,
admin: isAdmin,
email: profile._json.mail,
displayName: profile.displayName,
title: profile._json.title,
};
// Now check if the user is an admin
const isAdmin = await ldaphelper.isUserInAdGroup(profile.username, domain, adminGroup);

Check warning on line 45 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L45

Added line #L45 was not covered by tests

await db.updateUser(user);
profile.admin = isAdmin;
console.log(`passport.activeDirectory: ${profile.username} admin=${isAdmin}`);

Check warning on line 48 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L47-L48

Added lines #L47 - L48 were not covered by tests

return done(null, user);
},
const user = {

Check warning on line 50 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L50

Added line #L50 was not covered by tests
username: profile.username,
admin: isAdmin,
email: profile._json.mail,
displayName: profile.displayName,
title: profile._json.title,
};

await db.updateUser(user);

Check warning on line 58 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L58

Added line #L58 was not covered by tests

return done(null, user);

Check warning on line 60 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L60

Added line #L60 was not covered by tests
} catch (err) {
console.log(`Error authenticating AD user: ${err.message}`);
return done(err, null);

Check warning on line 63 in src/service/passport/activeDirectory.js

View check run for this annotation

Codecov / codecov/patch

src/service/passport/activeDirectory.js#L62-L63

Added lines #L62 - L63 were not covered by tests
}
}
),
);

Expand All @@ -69,4 +77,4 @@
return passport;
};

module.exports.configure = configure;
module.exports = { configure };
49 changes: 26 additions & 23 deletions src/service/passport/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
const passport = require("passport");
const local = require('./local');
const activeDirectory = require('./activeDirectory');
const oidc = require('./oidc');
const config = require('../../config');
const authenticationConfig = config.getAuthentication();
let _passport;

// Allows obtaining strategy config function and type
// Keep in mind to add AuthStrategy enum when refactoring this to TS
const authStrategies = {
local: local,
activedirectory: activeDirectory,
openidconnect: oidc,
};

const configure = async () => {
const type = authenticationConfig.type.toLowerCase();

switch (type) {
case 'activedirectory':
_passport = await activeDirectory.configure();
break;
case 'local':
_passport = await local.configure();
break;
case 'openidconnect':
_passport = await oidc.configure();
break;
default:
throw Error(`uknown authentication type ${type}`);
passport.initialize();

const authMethods = config.getAuthMethods();

for (const auth of authMethods) {
const strategy = authStrategies[auth.type.toLowerCase()];
if (strategy && typeof strategy.configure === "function") {
await strategy.configure(passport);
}
}
if (!_passport.type) {
_passport.type = type;

if (authMethods.some(auth => auth.type.toLowerCase() === "local")) {
await local.createDefaultAdmin();
}
return _passport;
};

module.exports.configure = configure;
module.exports.getPassport = () => {
return _passport;
return passport;
};

const getPassport = () => passport;

module.exports = { authStrategies, configure, getPassport };
Loading
Loading