Initial commit
commit
4a1c99dc6d
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
.env
|
||||
@ -0,0 +1,11 @@
|
||||
FROM node:26-alpine3.23
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json package.json
|
||||
COPY package-lock.json package-lock.json
|
||||
|
||||
COPY index.js index.js
|
||||
|
||||
RUN npm install
|
||||
|
||||
CMD ["node", "index.js"]
|
||||
@ -0,0 +1,105 @@
|
||||
var proxy = require('express-http-proxy');
|
||||
var app = require('express')();
|
||||
const OAuth2Strategy = require("passport-oauth2")
|
||||
const passport = require("passport");
|
||||
var session = require('express-session')
|
||||
const superagent = require('superagent');
|
||||
require('dotenv').config();
|
||||
passport.use(new OAuth2Strategy({
|
||||
authorizationURL: 'https://auth.edwardpeterson.dev/application/o/authorize/',
|
||||
tokenURL: 'https://auth.edwardpeterson.dev/application/o/token/',
|
||||
clientID: 'NwAsa84xs6q0mgWZj0eTawTpvkctMLz9ZZyjkO23',
|
||||
clientSecret: process.env.OAUTH_SECRET,
|
||||
callbackURL: process.env.OAUTH_CALLBACKURL
|
||||
},
|
||||
function(accessToken, refreshToken, profile, cb) {
|
||||
getUserInfoFromAuth(accessToken).then((response) => {
|
||||
console.log(response);
|
||||
cb(null, response);
|
||||
})
|
||||
}
|
||||
));
|
||||
passport.serializeUser(function(user, done) {
|
||||
done(null, user);
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(user, done) {
|
||||
done(null, user);
|
||||
});
|
||||
app.use(session({
|
||||
secret: 'definitely not keyboard cat',
|
||||
resave: false,
|
||||
saveUninitialized: true,
|
||||
// cookie: function(req) {
|
||||
// var match = req.url.match(/^\/([^/]+)/);
|
||||
// return {
|
||||
// path: match ? '/' + match[1] : '/',
|
||||
// httpOnly: true,
|
||||
// secure: req.secure || false,
|
||||
// maxAge: 60000
|
||||
// }
|
||||
// }
|
||||
}))
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
const isLoggedIn = () => {
|
||||
return (req, res, next) => {
|
||||
if(req.isAuthenticated()){
|
||||
return next();
|
||||
}
|
||||
return res.status(401).json({message: "Not authenticated"});
|
||||
|
||||
}
|
||||
}
|
||||
function getUserInfoFromAuth(accessToken ) {
|
||||
return superagent('https://auth.edwardpeterson.dev/application/o/userinfo/').set('Authorization', 'Bearer ' + accessToken).then(response => {
|
||||
return response.body;
|
||||
}).catch(error => {
|
||||
log.error('superagent error' + error);
|
||||
})
|
||||
}
|
||||
|
||||
app.get('/auth/login',
|
||||
passport.authenticate('oauth2'));
|
||||
|
||||
app.get('/oauth',
|
||||
passport.authenticate('oauth2', { failureRedirect: '/notnpm' }),
|
||||
function(req, res) {
|
||||
// Successful authentication, redirect home.
|
||||
superagent.post(`http://${process.env.PROXY_URL}/api/tokens`)
|
||||
.send({"identity": req.user.email,secret:process.env.PASSWORD})
|
||||
.set('accept', 'json')
|
||||
.end((err, authResponse) =>{
|
||||
const jsonResponse = JSON.parse(authResponse.text)
|
||||
req.session.npmJWT = jsonResponse.token;
|
||||
res.redirect("/");
|
||||
|
||||
})
|
||||
|
||||
|
||||
});
|
||||
|
||||
app.use((req, res, next) =>{
|
||||
if(req.session.npmJWT) {
|
||||
req.headers.authorization = `Bearer ${req.session.npmJWT}`
|
||||
}
|
||||
next();
|
||||
})
|
||||
app.get('/', (req, res, next) =>{
|
||||
if(req.session.npmJWT) {
|
||||
next();
|
||||
} else {
|
||||
res.redirect("/auth/login")
|
||||
}
|
||||
})
|
||||
app.get('/login', (req, res, next) => {
|
||||
req.session.destroy(error => {
|
||||
res.clearCookie('connect.sid');
|
||||
res.redirect('/')
|
||||
// res.status(200).json({message: 'Logged out'})
|
||||
})
|
||||
|
||||
})
|
||||
app.use('/', isLoggedIn(), proxy(process.env.PROXY_URL));
|
||||
|
||||
app.listen(3000);
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "npmauthproxy",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"dotenv": "^17.4.2",
|
||||
"express": "^5.2.1",
|
||||
"express-http-proxy": "^2.1.2",
|
||||
"express-session": "^1.19.0",
|
||||
"passport": "^0.7.0",
|
||||
"passport-oauth2": "^1.8.0",
|
||||
"superagent": "^10.3.0"
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue