Last active
September 15, 2021 18:12
-
-
Save purejgleason/40436d3e97a905a01d950c883866ce59 to your computer and use it in GitHub Desktop.
Why is this giving be infinite redirect?
This file contains 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
import express from "express"; | |
import passport from "passport"; | |
import querystring from "querystring"; | |
import dotenv from "dotenv"; | |
import expressSession from "express-session"; | |
import oidc from "passport-auth0-openidconnect"; | |
import cookieParser from "cookie-parser"; | |
import {ensureLoggedIn} from "connect-ensure-login"; | |
import { passportJwtSecret } from "jwks-rsa"; | |
import { ExtractJwt, Strategy as JWTStrategy } from "passport-jwt"; | |
dotenv.config(); | |
const logoutUrlStr = `https://${process.env.AUTH0_DOMAIN}/v2/logout`; | |
class CommunicationsApplication { | |
constructor() { | |
// this.getOpenIdMiddleware(); | |
this.getJWTMiddleware(); | |
this.app = CommunicationsApplication.getExpress(); | |
const session = { | |
secret: process.env.SESSION_SECRET, | |
cookie: {}, | |
name: "patrick", | |
resave: false, | |
saveUninitialized: false, | |
}; | |
this.app.use(expressSession(session)); | |
this.app.use(passport.initialize()); | |
this.app.use(passport.session()); | |
this.app.use(cookieParser()); | |
// this.app.use(cookieParser()); | |
const serDes = (user, done) => { | |
done(null, user); | |
}; | |
passport.serializeUser(serDes); | |
passport.deserializeUser(serDes); | |
// START AUTH | |
this.app.get("/logout", (req, res) => { | |
req.logOut(); | |
const logoutURL = new URL( | |
logoutUrlStr, | |
); | |
// TODO: Get Path to redirect to | |
logoutURL.search = querystring.stringify({ | |
client_id: process.env.CLIENT_ID, | |
returnTo: "http://localhost:4001", | |
}); | |
res.redirect(logoutURL); | |
}); | |
this.app.get("/login", passport.authenticate(this.type, { | |
scope: "openid email profile", | |
})); | |
this.app.get("/callback", | |
passport.authenticate(this.type, { | |
scope: "openid email profile", | |
}), function(req, res) { | |
res.redirect("/user"); | |
}, | |
); | |
this.app.get("/", | |
function(req, res) { | |
res.json({ | |
message: "hello world" | |
}); | |
}); | |
this.app.get("/user", | |
ensureLoggedIn(), | |
function(req, res) { | |
console.log(`User is ${JSON.stringify(req.user)}`); | |
res.json(req.user || {}); | |
}); | |
} | |
getJWTMiddleware(){ | |
const verify = function(payload, cb) { | |
console.log("Verifying"); | |
console.log(payload); | |
return cb(payload); | |
}; | |
const config = { | |
secretOrKeyProvider: passportJwtSecret({ | |
cache: true, | |
rateLimit: true, | |
jwksRequestsPerMinute: 5, | |
jwksUri: `${process.env.ISSUER_BASE_URL}/.well-known/jwks.json`, | |
}), | |
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | |
algorithms: ["RS256"], | |
audience: process.env.AUDIENCE, | |
clientId: process.env.CLIENT_ID, | |
clientSecret: process.env.SECRET, | |
issuer: process.env.ISSUER_BASE_URL, | |
}; | |
console.log(config); | |
passport.use(new JWTStrategy( | |
config, | |
verify, | |
)); | |
this.type = "jwt"; | |
} | |
getOpenIdMiddleware(){ | |
const verify = (issuer, audience, profile, cb)=> cb(null, profile._json); | |
const oidcAuth0Config = { | |
domain: process.env.AUTH0_DOMAIN, | |
clientID: process.env.CLIENT_ID, | |
clientSecret: process.env.SECRET, | |
callbackURL: process.env.AUTH0_CALLBACK_URL, | |
}; | |
console.log(oidcAuth0Config); | |
passport.use(new oidc.Strategy( | |
oidcAuth0Config, | |
verify, | |
)); | |
this.type = "auth0-oidc"; | |
} | |
static getExpress() { | |
return express() | |
} | |
} | |
const port = process.env.PORT || 4001; | |
const server = new CommunicationsApplication(); | |
server.app.listen(port, ()=>{ | |
console.log(`Server started on ${port}`); | |
}) |
This file contains 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
{ | |
"name": "dream", | |
"version": "0.0.1", | |
"description": "Authentication service", | |
"main": "server.mjs", | |
"type": "module", | |
"scripts": { | |
"start": "npx nodemon server.mjs", | |
"test": "c8 --check-coverage true mocha --recursive './lib/**/*.spec.mjs' --require esm" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"connect-ensure-login": "latest", | |
"cookie-parser": "latest", | |
"dotenv": "latest", | |
"express": "latest", | |
"express-session": "latest", | |
"jwks-rsa": "latest", | |
"passport": "latest", | |
"passport-auth0-openidconnect": "latest" | |
}, | |
"devDependencies": { | |
"c8": "latest", | |
"chai": "latest", | |
"depcheck": "latest", | |
"esm": "latest", | |
"mocha": "latest", | |
"nodemon": "latest", | |
"nyc": "latest", | |
"sinon": "latest" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment