Skip to content

Commit

Permalink
Add packages and code changes for running the app in https
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaKattainen committed Jul 15, 2022
1 parent 353855f commit c5e1c58
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ yarn-error.log*
# VS code folder
.vscode

*.pem
# Localhost https certs
certs
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"dotenv": "10.0.0",
"express": "4.17.1",
"express-session": "^1.17.0",
"fs": "^0.0.1-security",
"https": "^1.0.0",
"node-cron": "^3.0.0",
"node-pg-migrate": "5.10.0",
"passport": "0.5.2",
Expand Down
7 changes: 5 additions & 2 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const metadataConfig = {
timeout: 30000,
}

module.exports.configurePassport = async clientUrl => {
module.exports.configurePassport = async (clientUrl) => {
try {
const reader = await metadata.fetch(metadataConfig)

Expand All @@ -23,10 +23,13 @@ module.exports.configurePassport = async clientUrl => {
protocol: 'samlp',
callbackUrl: process.env.PARTIOID_CALLBACK,
logoutCallbackUrl: process.env.PARTIOID_LOGOUT_CALLBACK,
// FIXME: Does not work in development environment
// Because the clientUrl is not configured to partio id as an allowed client logout throws an error
// Might work if we set the staging env app url here instead of the localhost clientUrl?
logoutUrl: process.env.PARTIOID_LOGOUT_URL + clientUrl,
})

const samlStrategy = new SamlStrategy(strategyConfig, async function(
const samlStrategy = new SamlStrategy(strategyConfig, async function (
profile,
done
) {
Expand Down
27 changes: 21 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ import cors from 'cors'
import bodyParser from 'body-parser'
import passport from 'passport'
import * as Sentry from '@sentry/node'
import * as Tracing from '@sentry/tracing'
import {
db,
postTaskEntry,
getTaskEntries,
postTaskGroupEntry,
deleteActiveTask,
postFavouriteTask,
getFavouriteTasks,
deleteFavouriteTask,
getTaskGroupEntries,
addTaskEntryToArchive,
} from './database'
import { getProfile } from './profile'
import { getGroups } from './groups'
Expand All @@ -27,6 +23,8 @@ import 'regenerator-runtime/runtime.js'
import notifications from './notifications'
import taskGroups from './taskGroups'
import { deleteOldNotifications } from './database/notifications'
import https from 'https'
import fs from 'fs'

require('dotenv').config()

Expand All @@ -41,7 +39,12 @@ const clientUrl = process.env.CLIENT_URL || 'http://localhost:3000'
const cookieConfig = {
maxAge: 24 * 60 * 60 * 1000,
}
if (process.env.NODE_ENV !== 'development') {

if (
process.env.NODE_ENV !== 'development' ||
process.env.LOCALHOST_WITH_HTTPS === 'enabled'
) {
console.log('COOKIE CONFIG')
cookieConfig.sameSite = 'none'
cookieConfig.secure = true
}
Expand All @@ -58,6 +61,7 @@ const main = async () => {
extended: true,
})
)

app.set('trust proxy', 1)
app.use(
session({
Expand Down Expand Up @@ -302,7 +306,18 @@ const main = async () => {

app.use('/', router)
const port = process.env.PORT || 3001
app.listen(port, () => console.log(`listening on port ${port}`))
if (
process.env.NODE_ENV !== 'development' ||
process.env.LOCALHOST_WITH_HTTPS !== 'enabled'
) {
app.listen(port, () => console.log(`listening on port ${port}`))
} else {
const key = fs.readFileSync('certs/server.key', 'utf-8')
const cert = fs.readFileSync('certs/server.crt', 'utf-8')

https.createServer({ key, cert }, app).listen(port)
console.log(`HTTPS localhost listening on port ${port}`)
}
}

main().catch((error) => console.error(error))
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,11 @@ fs.realpath@^1.0.0:
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=

fs@^0.0.1-security:
version "0.0.1-security"
resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4"
integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==

fsevents@~2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
Expand Down Expand Up @@ -2668,6 +2673,11 @@ https-proxy-agent@^5.0.0:
agent-base "6"
debug "4"

https@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/https/-/https-1.0.0.tgz#3c37c7ae1a8eeb966904a2ad1e975a194b7ed3a4"
integrity sha512-4EC57ddXrkaF0x83Oj8sM6SLQHAWXw90Skqu2M4AEWENZ3F02dFJE/GARA8igO79tcgYqGrD7ae4f5L3um2lgg==

human-signals@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"
Expand Down

0 comments on commit c5e1c58

Please sign in to comment.