diff --git a/controllers/auth/read.auth.controller.js b/controllers/auth/read.auth.controller.js index bf1f667..57aafd7 100644 --- a/controllers/auth/read.auth.controller.js +++ b/controllers/auth/read.auth.controller.js @@ -9,10 +9,10 @@ const { const logger = require('../../service-library/helpers/logger.helpers') const jwtHelpers = require('../../service-library/helpers/jwt.helpers') const authHelpers = require('../../helpers/auth.helpers') -// const k8sHelpers = require('../../service-library/helpers/k8s.helpers') -// const stringHelpers = require('../../service-library/helpers/string.helpers') -// const responseHelpers = require('../../helpers/response.helpers') -// const { k8sConstants } = require('../../service-library/constants') +const k8sHelpers = require('../../service-library/helpers/k8s.helpers') +const stringHelpers = require('../../service-library/helpers/string.helpers') +const responseHelpers = require('../../helpers/response.helpers') +const { k8sConstants } = require('../../service-library/constants') router.get('/guest', async (req, res, next) => { try { @@ -43,102 +43,97 @@ router.get( passport.authenticate('github', { scope: ['user:email'] }) ) -router.get('/github/callback', (req, res, next) => { - // 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 +router.get('/github/callback', async (req, res, next) => { + 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') - - // fetch( - // tokenURL + - // '?client_id=' + - // clientId + - // '&client_secret=' + - // clientSecret + - // '&code=' + - // grantCode, - // { - // method: 'POST', - // headers: { - // Accept: 'application/json' - // } - // } - // ) - // .then((res) => res.json()) - // .then((json) => { - // logger.debug('5') - // logger.debug(json) - // req.session.github_token = json.access_token - // logger.debug('6') - // logger.debug(req.session.github_token) - // }) - // .catch((err) => console.log(err)) - // .then(() => { - // fetch(userProfileURL, { - // method: 'GET', - // headers: { - // Accept: 'application/json', - // Authorization: 'Bearer ' + req.session.github_token - // } - // }) - // .then((res) => res.json()) - // .then((json) => { - // logger.debug('7') - // userInfo.id = json.id - // userInfo.displayName = json.name - // userInfo.username = json.login - // userInfo.email = json.email - // }) - // .catch((err) => console.log(err)) - // }) - - userInfo.id = '1' - userInfo.displayName = 'lb' - userInfo.username = 'userlb' - userInfo.email = 'myemail' + logger.debug('----> new UserInfo') + + fetch( + tokenURL + + '?client_id=' + + clientId + + '&client_secret=' + + clientSecret + + '&code=' + + grantCode, + { + method: 'POST', + headers: { + Accept: 'application/json' + } + } + ) + .then((res) => res.json()) + .then((json) => { + logger.debug('5') + logger.debug(json) + req.session.github_token = json.access_token + logger.debug('6') + logger.debug(req.session.github_token) + }) + .catch((err) => console.log(err)) + .then(() => { + fetch(userProfileURL, { + method: 'GET', + headers: { + Accept: 'application/json', + Authorization: 'Bearer ' + req.session.github_token + } + }) + .then((res) => res.json()) + .then((json) => { + logger.debug('7') + userInfo.id = json.id + userInfo.displayName = json.name + userInfo.username = json.login + userInfo.email = json.email + }) + .catch((err) => console.log(err)) + }) logger.info('8') logger.info(JSON.stringify(userInfo))