forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
feat(auth): implement OIDC auth for frontend #7
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
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d3d04a0
feat(auth): add OIDC login button to login page
jescalada d157d96
feat(auth): add OIDC strategy to passport
jescalada 1c19264
feat(auth): add OIDC database handler and endpoint
jescalada 96edf6a
fix(auth): fix null password error on OIDC login
jescalada ddcacee
fix(backend): serve static files only in production
jescalada 5c93210
test(auth): add basic E2E test for OIDC
jescalada c61506a
Merge branch 'main' into oidc-implementation
JamieSlome 8d0453f
chore(auth): remove unnecessary changes
jescalada 49a09b3
Merge branch 'oidc-implementation' of https://github.com/jescalada/gi…
jescalada 52e33d9
feat(auth): refactor to use openid-client instead of passport-openidc…
jescalada 4f71e77
chore(auth): fix lint warnings
jescalada 0a0656d
fix(auth): attempt to fix CI ERR_REQUIRE_ESM
jescalada a096252
Merge branch 'main' into oidc-implementation
jescalada 712938b
fix(auth): fix bug with default admin account
jescalada 6ba8021
chore: bump by patch to 1.8.1
JamieSlome e1a9bd2
Merge pull request #936 from finos/release-1.8.1
JamieSlome 8017b10
Merge branch 'main' into oidc-implementation
JamieSlome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
const { defineConfig } = require('cypress') | ||
const { defineConfig } = require('cypress'); | ||
|
||
module.exports = defineConfig({ | ||
e2e: { | ||
baseUrl: process.env.CYPRESS_BASE_URL ||'http://localhost:3000', | ||
baseUrl: process.env.CYPRESS_BASE_URL || 'http://localhost:3000', | ||
chromeWebSecurity: false, // Required for OIDC testing | ||
}, | ||
}) | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,33 @@ | ||
describe("Display finos UI",()=>{ | ||
|
||
beforeEach(() =>{ | ||
cy.visit('/login') | ||
}) | ||
it('shoud find git proxy logo',() =>{ | ||
cy.get('[data-test="git-proxy-logo"]').should('exist') | ||
}) | ||
it('shoud find username',() =>{ | ||
cy.get('[data-test="username"]').should('exist') | ||
}) | ||
describe('Login page', () => { | ||
beforeEach(() => { | ||
cy.visit('/login'); | ||
}); | ||
|
||
it('shoud find passsword',() =>{ | ||
cy.get('[data-test="password"]').should('exist') | ||
}) | ||
it('shoud find login button',() =>{ | ||
cy.get('[data-test="login"]').should('exist') | ||
}) | ||
}) | ||
it('should have git proxy logo', () => { | ||
cy.get('[data-test="git-proxy-logo"]').should('exist'); | ||
}); | ||
|
||
it('should have username input', () => { | ||
cy.get('[data-test="username"]').should('exist'); | ||
}); | ||
|
||
it('should have passsword input', () => { | ||
cy.get('[data-test="password"]').should('exist'); | ||
}); | ||
|
||
it('should have login button', () => { | ||
cy.get('[data-test="login"]').should('exist'); | ||
}); | ||
|
||
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', () => { | ||
cy.get('[data-test="oidc-login"]').click(); | ||
cy.url().should('include', '/oidc'); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ const configure = async () => { | |
const admin = await db.findUser('admin'); | ||
|
||
if (!admin) { | ||
await db.createUser('admin', 'admin', '[email protected]', 'none', true, true, true, true); | ||
await db.createUser('admin', 'admin', '[email protected]', 'none', true); | ||
} | ||
|
||
passport.type = 'local'; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const passport = require('passport'); | ||
const db = require('../../db'); | ||
|
||
const configure = async () => { | ||
// Temp fix for ERR_REQUIRE_ESM, will be changed when we refactor to ESM | ||
const { discovery, fetchUserInfo } = await import('openid-client'); | ||
const { Strategy } = await import('openid-client/passport'); | ||
const config = require('../../config').getAuthentication(); | ||
const { oidcConfig } = config; | ||
const { issuer, clientID, clientSecret, callbackURL, scope } = oidcConfig; | ||
|
||
if (!oidcConfig || !oidcConfig.issuer) { | ||
throw new Error('Missing OIDC issuer in configuration') | ||
} | ||
|
||
const server = new URL(issuer); | ||
|
||
try { | ||
const config = await discovery(server, clientID, clientSecret); | ||
|
||
const strategy = new Strategy({ callbackURL, config, scope }, async (tokenSet, done) => { | ||
// Validate token sub for added security | ||
const idTokenClaims = tokenSet.claims(); | ||
const expectedSub = idTokenClaims.sub; | ||
const userInfo = await fetchUserInfo(config, tokenSet.access_token, expectedSub); | ||
handleUserAuthentication(userInfo, done); | ||
}); | ||
|
||
// currentUrl must be overridden to match the callback URL | ||
strategy.currentUrl = function (request) { | ||
const callbackUrl = new URL(callbackURL); | ||
const currentUrl = Strategy.prototype.currentUrl.call(this, request); | ||
currentUrl.host = callbackUrl.host; | ||
currentUrl.protocol = callbackUrl.protocol; | ||
return currentUrl; | ||
}; | ||
|
||
passport.use(strategy); | ||
|
||
passport.serializeUser((user, done) => { | ||
done(null, user.oidcId || user.username); | ||
}) | ||
|
||
passport.deserializeUser(async (id, done) => { | ||
try { | ||
const user = await db.findUserByOIDC(id); | ||
done(null, user); | ||
} catch (err) { | ||
done(err); | ||
} | ||
}) | ||
passport.type = server.host; | ||
|
||
return passport; | ||
} catch (error) { | ||
console.error('OIDC configuration failed:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
|
||
module.exports.configure = configure; | ||
|
||
/** | ||
* Handles user authentication with OIDC. | ||
* @param {Object} userInfo the OIDC user info object | ||
* @param {Function} done the callback function | ||
* @return {Promise} a promise with the authenticated user or an error | ||
*/ | ||
const handleUserAuthentication = async (userInfo, done) => { | ||
try { | ||
const user = await db.findUserByOIDC(userInfo.sub); | ||
|
||
if (!user) { | ||
const email = safelyExtractEmail(userInfo); | ||
if (!email) return done(new Error('No email found in OIDC profile')); | ||
|
||
const newUser = { | ||
username: getUsername(email), | ||
email, | ||
oidcId: userInfo.sub, | ||
}; | ||
|
||
await db.createUser(newUser.username, null, newUser.email, 'Edit me', false, newUser.oidcId); | ||
return done(null, newUser); | ||
} | ||
|
||
return done(null, user); | ||
} catch (err) { | ||
return done(err); | ||
} | ||
}; | ||
|
||
/** | ||
* Extracts email from OIDC profile. | ||
* This function is necessary because OIDC providers have different ways of storing emails. | ||
* @param {object} profile the profile object from OIDC provider | ||
* @return {string | null} the email address | ||
*/ | ||
const safelyExtractEmail = (profile) => { | ||
return profile.email || (profile.emails && profile.emails.length > 0 ? profile.emails[0].value : null); | ||
}; | ||
|
||
/** | ||
* Generates a username from email address. | ||
* This helps differentiate users within the specific OIDC provider. | ||
* Note: This is incompatible with multiple providers. Ideally, users are identified by | ||
* OIDC ID (requires refactoring the database). | ||
* @param {string} email the email address | ||
* @return {string} the username | ||
*/ | ||
const getUsername = (email) => { | ||
return email ? email.split('@')[0] : ''; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also if there is a local user with the same username