#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const https = require('https');
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const {Strategy} = require('passport-saml');
const ws = require('uws');
const connection = require('./server/connection');
// const index = path.resolve(__dirname, 'web', 'index.html');
const index = path.resolve(__dirname, 'web', 'dev.html');
const children = {};

const key = fs.readFileSync(path.resolve(__dirname, 'certs', 'lambda-key.pem'));
const cert = fs.readFileSync(path.resolve(__dirname, 'certs', 'lambda_mit_edu.cer'));
const credentials = {key, cert};

const idpCert = fs.readFileSync(path.resolve(__dirname, 'certs', 'cert_idp.pem'));
const spKey = fs.readFileSync(path.resolve(__dirname, 'certs', 'sp-key.pem'));

const config = {
    entryPoint: 'https://idp.mit.edu/idp/profile/SAML2/Redirect/SSO',
    cert: idpCert,
    identifierFormat: null,
    issuer: 'https://lambda.mit.edu/shibboleth',
    callbackUrl: 'https://lambda.mit.edu/auth/callback',
    decryptionPvk: spKey,
    privateCert: spKey,
    acceptedClockSkewMs: 180000,
    disableRequestedAuthnContext: true,
    // passReqToCallback: true,
};
const strategy = new Strategy(config, (profile, done) => done(null, profile));
const metadata = strategy.generateServiceProviderMetadata(fs.readFileSync(__dirname + '/certs/sp-cert.pem', 'utf8'));

passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
passport.use(strategy);

const app = express();
app.use(express.static(path.resolve(__dirname, 'web')));

app.use(passport.initialize());
app.use(passport.session());

app.get('/', (req, res) => res.sendFile(index));

// app.get('/:user', (req, res) => res.sendFile(index));
const checkAuth = (req, res, next) => req.isAuthenticated() ? next() : res.redirect('/auth');
app.get('/u/:user', checkAuth, (req, res) => res.sendFile(index));

const auth = passport.authenticate('saml', {failureRedirect: '/auth/fail'});
app.get('/auth', auth, (req, res) => res.redirect('/'));
app.post('/auth/callback', auth, (req, res) => res.redirect('/'));
app.get('/auth/fail', (req, res) => res.status(401).send('Login failed'));
app.get('/shibboleth', (req, res) => res.type('application/xml').status(200).send(metadata));

// app.listen(server_port);
// console.log('server listening on port', server_port);
https.createServer(credentials, app).listen(443);
console.log('server listening on port 443');


// WebSocket Server
// const socket_port = 1947;
// const socket = new ws.Server({port: socket_port});
// socket.on('connection', socket => connection(socket, children));
// console.log('socket listening on port', socket_port);
//
// process.on('SIGINT', e => process.exit()).on('SIGTERM', e => process.exit());
// process.on('exit', function(signal, code) {
//     console.log('process exiting');
//     Object.keys(children).forEach(pid => {
//         console.log('killing', pid);
//         process.kill(pid, 'SIGTERM');
//     });
//     process.kill(process.pid, 'SIGKILL');
// });

@@ -644,16 +644,21 @@
 _mit_edu
+_cert
 .cer'));

my hands are typing words