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 25 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
16 changes: 15 additions & 1 deletion cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,29 @@ describe('Login page', () => {
cy.get('[data-test="login"]').should('exist');
});

it('should redirect to repo list on valid login', () => {
cy.intercept('GET', '**/api/auth/me').as('getUser');

cy.get('[data-test="username"]').type('admin');
cy.get('[data-test="password"]').type('admin');
cy.get('[data-test="login"]').click();

cy.wait('@getUser');

cy.url().should('include', '/dashboard/repo');
})

describe('OIDC login button', () => {
it('should exist', () => {
cy.get('[data-test="oidc-login"]').should('exist');
});

// 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');
});
});
});
6 changes: 4 additions & 2 deletions cypress/e2e/repo.cy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
describe('Repo', () => {
beforeEach(() => {
cy.visit('/admin/repo');
cy.login('admin', 'admin');

cy.visit('/dashboard/repo');

// prevent failures on 404 request and uncaught promises
cy.on('uncaught:exception', () => false);
Expand All @@ -18,7 +20,7 @@ describe('Repo', () => {

cy
// find the entry for finos/test-repo
.get('a[href="/admin/repo/test-repo"]')
.get('a[href="/dashboard/repo/test-repo"]')
// take it's parent row
.closest('tr')
// find the nearby span containing Code we can click to open the tooltip
Expand Down
6 changes: 5 additions & 1 deletion cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
Cypress.Commands.add('login', (username, password) => {
cy.session([username, password], () => {
cy.visit('/login');
cy.intercept('GET', '**/api/auth/me').as('getUser');

cy.get('[data-test=username]').type(username);
cy.get('[data-test=password]').type(password);
cy.get('[data-test=login]').click();
cy.url().should('contain', '/admin/profile');

cy.wait('@getUser');
cy.url().should('include', '/dashboard/repo');
});
});
24 changes: 13 additions & 11 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,29 @@
throw Error('No database cofigured!');
};

// Gets the configuared data sink, defaults to filesystem
const getAuthentication = () => {
/**
* Get the list of enabled authentication methods
* @return {Array} List of enabled authentication methods
*/
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.");

Check warning on line 85 in src/config/index.js

View check run for this annotation

Codecov / codecov/patch

src/config/index.js#L85

Added line #L85 was not covered by tests
}

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

// Log configuration to console
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())}`);
};

const getAPIs = () => {
Expand Down Expand Up @@ -202,7 +204,7 @@
exports.getAuthorisedList = getAuthorisedList;
exports.getDatabase = getDatabase;
exports.logConfiguration = logConfiguration;
exports.getAuthentication = getAuthentication;
exports.getAuthMethods = getAuthMethods;
exports.getTempPasswordConfig = getTempPasswordConfig;
exports.getCookieSecret = getCookieSecret;
exports.getSessionMaxAgeHours = getSessionMaxAgeHours;
Expand Down
23 changes: 15 additions & 8 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { createBrowserHistory } from 'history';
import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom';
import { AuthProvider } from './ui/auth/AuthProvider';

// core components
import Admin from './ui/layouts/Admin';
import Dashboard from './ui/layouts/Dashboard';
import Login from './ui/views/Login/Login';
import './ui/assets/css/material-dashboard-react.css';
import NotAuthorized from './ui/views/Extras/NotAuthorized';
import NotFound from './ui/views/Extras/NotFound';

const hist = createBrowserHistory();

ReactDOM.render(
<Router history={hist}>
<Routes>
<Route exact path='/admin/*' element={<Admin />} />
<Route exact path='/login' element={<Login />} />
<Route exact path='/' element={<Navigate from='/' to='/admin/repo' />} />
</Routes>
</Router>,
<AuthProvider>
<Router history={hist}>
<Routes>
<Route exact path='/dashboard/*' element={<Dashboard />} />
<Route exact path='/login' element={<Login />} />
<Route exact path='/not-authorized' element={<NotAuthorized />} />
<Route exact path='/' element={<Navigate from='/' to='/dashboard/repo' />} />
<Route path='*' element={<NotFound />} />
</Routes>
</Router>
</AuthProvider>,
document.getElementById('root'),
);
2 changes: 1 addition & 1 deletion src/proxy/processors/push-action/blockForAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const exec = async (req, action) => {
'\n\n\n' +
`\x1B[32mGitProxy has received your push ✅\x1B[0m\n\n` +
'🔗 Shareable Link\n\n' +
`\x1B[34m${url}/admin/push/${action.id}\x1B[0m` +
`\x1B[34m${url}/dashboard/push/${action.id}\x1B[0m` +
'\n\n\n';
step.setAsyncBlock(message);

Expand Down
54 changes: 28 additions & 26 deletions src/routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

*/

import React from 'react';
import PrivateRoute from './ui/components/PrivateRoute/PrivateRoute';
import Person from '@material-ui/icons/Person';
import OpenPushRequests from './ui/views/OpenPushRequests/OpenPushRequests';
import PushDetails from './ui/views/PushDetails/PushDetails';
Expand All @@ -33,58 +35,58 @@ const dashboardRoutes = [
path: '/repo',
name: 'Repositories',
icon: RepoIcon,
component: RepoList,
layout: '/admin',
component: (props) => <PrivateRoute component={RepoList} />,
layout: '/dashboard',
visible: true,
},
{
path: '/repo/:id',
name: 'Repo Details',
icon: Person,
component: (props) => <PrivateRoute component={RepoDetails} />,
layout: '/dashboard',
visible: false,
},
{
path: '/push',
name: 'Dashboard',
icon: Dashboard,
component: OpenPushRequests,
layout: '/admin',
component: (props) => <PrivateRoute component={OpenPushRequests} />,
layout: '/dashboard',
visible: true,
},
{
path: '/push/:id',
name: 'Open Push Requests',
icon: Person,
component: PushDetails,
layout: '/admin',
component: (props) => <PrivateRoute component={PushDetails} />,
layout: '/dashboard',
visible: false,
},
{
path: '/profile',
name: 'My Account',
icon: AccountCircle,
component: User,
layout: '/admin',
component: (props) => <PrivateRoute component={User} />,
layout: '/dashboard',
visible: true,
},
{
path: '/user/:id',
name: 'User',
icon: Person,
component: User,
layout: '/admin',
visible: false,
path: '/admin/user',
name: 'Users',
icon: Group,
component: (props) => <PrivateRoute adminOnly component={UserList} />,
layout: '/dashboard',
visible: true,
},
{
path: '/repo/:id',
name: 'Repo Details',
path: '/admin/user/:id',
name: 'User',
icon: Person,
component: RepoDetails,
layout: '/admin',
component: (props) => <PrivateRoute adminOnly component={User} />,
layout: '/dashboard',
visible: false,
},
{
path: '/user',
name: 'Users',
icon: Group,
component: UserList,
layout: '/admin',
visible: true,
},
];

export default dashboardRoutes;
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 };
Loading
Loading