Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #142 from trailimage/develop
Browse files Browse the repository at this point in the history
add requireSSL method
  • Loading branch information
Jason-Abbott authored Jul 11, 2018
2 parents 59a670f + 47494f7 commit aa81be0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.1.0

- Add option to require an SSL connection

## 4.0.2

- Fix unhandled error for missing GPX
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@trail-image/blog",
"description": "Trail Image blog",
"version": "4.0.2",
"version": "4.1.0",
"private": false,
"author": {
"name": "Jason Abbott"
Expand Down
5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { postProvider } from '@trailimage/flickr-provider';
import { mapProvider } from '@trailimage/google-provider';
import { config as modelConfig, blog } from '@trailimage/models';
import { config } from './config';
import { Layout, addTemplateMethods } from './views/';
import { Layout, addTemplateMethods, requireSSL } from './views/';
import { route } from './routes';

const root = path.join(__dirname, '..');
Expand Down Expand Up @@ -47,6 +47,9 @@ async function createWebService() {
app.listen(port);
log.info(`Listening for authentication on port ${port}`);
} else {
if (config.requireSSL) {
app.use(requireSSL);
}
app.use(blockSpamReferers);
// https://github.com/expressjs/compression/blob/master/README.md
app.use(compress());
Expand Down
2 changes: 2 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const posts = {
export const config = {
env,
domain,
/** Whether to redirect `HTTP` requests to `HTTPS`. */
requireSSL: env('REQUIRE_SSL', '') == 'true',

/** Whether any provider needs authorization tokens */
// get needsAuth(): boolean {
Expand Down
1 change: 1 addition & 0 deletions src/views/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Page, Layout, addTemplateMethods } from './template';
export { view, Renderer } from './view';
export { requireSSL } from './require-ssl';
18 changes: 18 additions & 0 deletions src/views/require-ssl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { HttpStatus } from '@toba/tools';
import { Request, Response, NextFunction } from 'express';

/**
* Middleware to require an SSL connection.
*/
export function requireSSL(req: Request, res: Response, next: NextFunction) {
if (req.secure) {
next();
} else if (req.method == 'GET' || req.method == 'HEAD') {
res.redirect(
HttpStatus.TempRedirect,
'https://' + req.header('Host') + req.originalUrl
);
} else {
res.status(HttpStatus.Forbidden).send('Data must be submitted securely');
}
}

0 comments on commit aa81be0

Please sign in to comment.