2022-01-12 12:32:14 +01:00
|
|
|
const passport = require("koa-passport")
|
|
|
|
const LocalStrategy = require("passport-local").Strategy
|
|
|
|
const JwtStrategy = require("passport-jwt").Strategy
|
|
|
|
const { getGlobalDB } = require("./tenancy")
|
2022-06-23 15:29:19 +02:00
|
|
|
const refresh = require("passport-oauth2-refresh")
|
|
|
|
const { Configs } = require("./constants")
|
|
|
|
const { getScopedConfig } = require("./db/utils")
|
2022-01-12 12:32:14 +01:00
|
|
|
const {
|
|
|
|
jwt,
|
|
|
|
local,
|
|
|
|
authenticated,
|
|
|
|
google,
|
|
|
|
oidc,
|
|
|
|
auditLog,
|
|
|
|
tenancy,
|
|
|
|
appTenancy,
|
|
|
|
authError,
|
2022-01-25 23:54:50 +01:00
|
|
|
csrf,
|
2022-01-24 11:48:59 +01:00
|
|
|
internalApi,
|
2022-01-12 12:32:14 +01:00
|
|
|
} = require("./middleware")
|
|
|
|
|
|
|
|
// Strategies
|
|
|
|
passport.use(new LocalStrategy(local.options, local.authenticate))
|
|
|
|
passport.use(new JwtStrategy(jwt.options, jwt.authenticate))
|
|
|
|
|
|
|
|
passport.serializeUser((user, done) => done(null, user))
|
|
|
|
|
|
|
|
passport.deserializeUser(async (user, done) => {
|
|
|
|
const db = getGlobalDB()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const user = await db.get(user._id)
|
|
|
|
return done(null, user)
|
|
|
|
} catch (err) {
|
2022-05-23 17:24:29 +02:00
|
|
|
console.error(`User not found`, err)
|
2022-01-12 12:32:14 +01:00
|
|
|
return done(null, false, { message: "User not found" })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-06-23 15:29:19 +02:00
|
|
|
//requestAccessStrategy
|
|
|
|
//refreshOAuthAccessToken
|
|
|
|
|
|
|
|
//configId for google and OIDC??
|
|
|
|
async function reUpToken(refreshToken, configId) {
|
|
|
|
const db = getGlobalDB()
|
|
|
|
console.log(refreshToken, configId)
|
|
|
|
const config = await getScopedConfig(db, {
|
|
|
|
type: Configs.OIDC,
|
|
|
|
group: {}, //ctx.query.group, this was an empty object when authentication initially
|
|
|
|
})
|
|
|
|
|
|
|
|
const chosenConfig = config.configs[0] //.filter((c) => c.uuid === configId)[0]
|
|
|
|
let callbackUrl = await oidc.oidcCallbackUrl(db, chosenConfig)
|
|
|
|
|
|
|
|
//Remote Config
|
|
|
|
const enrichedConfig = await oidc.fetchOIDCStrategyConfig(
|
|
|
|
chosenConfig,
|
|
|
|
callbackUrl
|
|
|
|
)
|
|
|
|
|
|
|
|
const strategy = await oidc.strategyFactory(enrichedConfig, () => {
|
|
|
|
console.log("saveFn RETURN ARGS", JSON.stringify(arguments))
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
refresh.use(strategy, {
|
|
|
|
setRefreshOAuth2() {
|
|
|
|
return strategy._getOAuth2Client(enrichedConfig)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
console.log("Testing")
|
|
|
|
|
|
|
|
// By default, the strat calls itself "openidconnect"
|
|
|
|
|
|
|
|
// refresh.requestNewAccessToken(
|
|
|
|
// 'openidconnect',
|
|
|
|
// refToken,
|
|
|
|
// (err, accessToken, refreshToken) => {
|
|
|
|
// console.log("REAUTH CB", err, accessToken, refreshToken);
|
|
|
|
// })
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
throw new Error("Error constructing OIDC refresh strategy", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("end")
|
|
|
|
}
|
|
|
|
|
2022-01-12 12:32:14 +01:00
|
|
|
module.exports = {
|
|
|
|
buildAuthMiddleware: authenticated,
|
|
|
|
passport,
|
|
|
|
google,
|
|
|
|
oidc,
|
|
|
|
jwt: require("jsonwebtoken"),
|
|
|
|
buildTenancyMiddleware: tenancy,
|
|
|
|
buildAppTenancyMiddleware: appTenancy,
|
|
|
|
auditLog,
|
|
|
|
authError,
|
2022-01-25 23:54:50 +01:00
|
|
|
buildCsrfMiddleware: csrf,
|
2022-01-24 11:48:59 +01:00
|
|
|
internalApi,
|
2022-06-23 15:29:19 +02:00
|
|
|
reUpToken,
|
2022-01-12 12:32:14 +01:00
|
|
|
}
|