Created
March 20, 2017 16:21
-
-
Save zamd/d0b71f2ad055e691599e736c2f40af2a to your computer and use it in GitHub Desktop.
ws-fed server webtask
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
const express = require('express'), | |
passport =require('passport'), | |
wsfed = require('wsfed'), | |
ejs = require('ejs'), | |
auth0 = require('auth0'), | |
selfsigned = require('selfsigned'), | |
session = require('express-session'), | |
LocalStrategy = require('passport-local'), | |
cookieParser = require('cookie-parser'), | |
bodyParser = require('body-parser'), | |
ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn, | |
tools = require('auth0-extension-tools'), | |
Webtask = require('webtask-tools'), | |
app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
const views = { | |
login: `<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
</head> | |
<body> | |
<div class="form"> | |
<div class="tab-content"> | |
<div id="login"> | |
<h1>Please sign in!</h1> | |
<form action="/login" method="post"> | |
<div class="field-wrap"> | |
<label> | |
Email Address<span class="req">*</span> | |
</label> | |
<input type="email"required autocomplete="off" name="email"/> | |
</div> | |
<div class="field-wrap"> | |
<label> | |
Password<span class="req">*</span> | |
</label> | |
<input type="password"required autocomplete="off" name="password"/> | |
</div> | |
<button class="button button-block"/>Log In</button> | |
</form> | |
</div> | |
</div><!-- tab-content --> | |
</div> <!-- /form --> | |
</body> | |
</html> | |
` | |
}; | |
const config = { | |
issuerName: "wsfedIdp.sample", //auth.servepics.com | |
callbackUrl: "https://pkr.auth0.com/login/callback", | |
domain: "pkr.auth0.com", | |
clientId: "1wdVlMJc3FoS2qkaP8ryqm5E1uRviEMe", | |
connection: "local" | |
} | |
app.use(session({ | |
resave: true, | |
saveUninitialized: true, | |
secret: 'secret1001' | |
})); | |
passport.serializeUser( (user,done)=> { | |
done(null,user); | |
}); | |
passport.deserializeUser( (user,done) =>{ | |
done(null,user); | |
}); | |
const wsfedOptions = { | |
issuer: config.issuerName, | |
getPostURL: function(wtrealm, wreply, req, cb) { | |
return cb(null, config.callbackUrl); | |
} | |
}; | |
var local = new LocalStrategy({ | |
usernameField: 'email', | |
passwordField: 'password', | |
session: true | |
},function(username,password,done) { | |
var authClient = new auth0.AuthenticationClient({ | |
domain: config.domain, | |
clientId: config.clientId | |
}); | |
authClient.database.signIn({ | |
username: username, | |
password: password, | |
connection: config.connection | |
}).then(data => { | |
authClient.getProfile(data.access_token).then(json => { | |
done(null,JSON.parse(json)) | |
}); | |
}).catch(err=>done(err)) | |
}); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.use(local); | |
// routes | |
app.use((req,res,next)=>{ | |
const storageProvider = req.webtaskContext ? new tools.WebtaskStorageContext(req.webtaskContext.storage) : | |
new tools.FileStorageContext('./db.json'); | |
const db = new tools.BlobRecordProvider(storageProvider); | |
req.db = db; | |
db.getAll('config') | |
.then(c=>c.shift()) | |
.then(cnfg=>{ | |
if (!cnfg) | |
{ | |
const pems = selfsigned.generate({ subj: '/CN=' + config.issuerName , days: 365 }); | |
cnfg = { | |
cert: pems.cert, | |
key: pems.private | |
}; | |
db.create('config',cnfg).then(ok=>{ | |
req.db.config = cnfg; | |
next(); | |
}); | |
} | |
else { | |
req.db.config = cnfg; | |
next(); | |
} | |
}); | |
}); | |
app.get('/login', function(req, res, next) { | |
res.status(200).end(ejs.render(views.login, { title: 'Login' })); | |
}); | |
app.post('/login', passport.authenticate('local', { failureRedirect: '/login', successReturnToOrRedirect: '/' })); | |
// wsfed | |
// app.get('/',ensureLoggedIn('/login'),wsfed.auth(wsfedOptions)); | |
// app.get('/FederationMetadata/2007-06/FederationMetadata.xml',wsfed.metadata(wsfedOptions)); | |
app.get('/',ensureLoggedIn('/login'), (req,res,next)=>{ | |
wsfedOptions.cert = req.db.config.cert; | |
wsfedOptions.key = req.db.config.key; | |
wsfed.auth(wsfedOptions)(req,res,next); | |
}); | |
app.get('/FederationMetadata/2007-06/FederationMetadata.xml',(req,res,next)=>{ | |
wsfedOptions.cert = req.db.config.cert; | |
wsfedOptions.key = req.db.config.key; | |
wsfed.metadata(wsfedOptions)(req,res,next); | |
}); | |
//app.listen(3000); | |
module.exports = Webtask.fromExpress(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment