This repository has been archived by the owner on Jan 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from NREL/https-requirements
Implement new HTTPS requirement behavior
- Loading branch information
Showing
9 changed files
with
521 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'), | ||
config = require('api-umbrella-config').global(), | ||
url = require('url'), | ||
utils = require('../utils'); | ||
|
||
var HttpsRequirements = function() { | ||
this.initialize.apply(this, arguments); | ||
}; | ||
|
||
_.extend(HttpsRequirements.prototype, { | ||
initialize: function() { | ||
this.httpsPort = config.get('https_port'); | ||
}, | ||
|
||
handleRequest: function(request, response, next) { | ||
if(request.base.substring(0, 8).toLowerCase() === 'https://') { | ||
// https requests are always okay, so continue. | ||
next(); | ||
} else if(request.base.substring(0, 7).toLowerCase() !== 'http://') { | ||
// If this isn't an http request, then we don't know how to handle it, so | ||
// continue. | ||
next(); | ||
} else { | ||
var mode = request.apiUmbrellaGatekeeper.settings.require_https; | ||
if(mode === 'optional') { | ||
// Continue if https isn't required. | ||
next(); | ||
} else { | ||
this.handleHttpRequest(mode, request, response, next); | ||
} | ||
} | ||
}, | ||
|
||
handleHttpRequest: function(mode, request, response, next) { | ||
var httpUrl = request.base; | ||
if(request.apiUmbrellaGatekeeper && request.apiUmbrellaGatekeeper.originalUrl) { | ||
httpUrl += request.apiUmbrellaGatekeeper.originalUrl; | ||
} else { | ||
httpUrl += request.url; | ||
} | ||
|
||
var urlParts = url.parse(httpUrl); | ||
urlParts.protocol = 'https:'; | ||
delete urlParts.port; | ||
delete urlParts.host; | ||
if(this.httpsPort && this.httpsPort !== 443) { | ||
urlParts.port = this.httpsPort; | ||
} | ||
var httpsUrl = url.format(urlParts); | ||
|
||
if(mode === 'transition_return_error' || mode === 'transition_return_redirect') { | ||
var transitionStartAt = request.apiUmbrellaGatekeeper.settings.require_https_transition_start_at; | ||
var user = request.apiUmbrellaGatekeeper.user; | ||
|
||
// If there is no user, or the user existed prior to the HTTPS transition | ||
// starting, then continue on, allowing http. | ||
if(!user || !user.created_at || user.created_at < transitionStartAt) { | ||
return next(); | ||
} | ||
} | ||
|
||
if(mode === 'required_return_redirect' || mode === 'transition_return_redirect') { | ||
// Return a 301 Moved Permanently redirect for GET requests. | ||
var statusCode = 301; | ||
|
||
// For non-GET requests, return a 307 Temporary Redirect, which instructs | ||
// the request to be made again with the same method (eg, a POST should | ||
// be retried as another POST). Ideally we would return a 308 Permanent | ||
// Redirect for permanent semantics, but that's an experimental RFC and | ||
// some libraries do something else currently with 308s (Resume | ||
// Incomplete): http://stackoverflow.com/q/14144664 | ||
// | ||
// Also for general reference, see Curl's current handling of 301, 302, | ||
// and 303s: http://curl.haxx.se/docs/manpage.html#-L | ||
if(request.method !== 'GET') { | ||
statusCode = 307; | ||
} | ||
|
||
var headers = { | ||
'Access-Control-Allow-Origin': '*', | ||
'Location': httpsUrl, | ||
}; | ||
|
||
var body; | ||
if(request.method !== 'HEAD') { | ||
body = 'Redirecting to ' + httpsUrl; | ||
headers['Content-Type'] = 'text/plain'; | ||
headers['Content-Length'] = Buffer.byteLength(body); | ||
} | ||
|
||
response.writeHead(statusCode, headers); | ||
response.end(body); | ||
} else { | ||
utils.errorHandler(request, response, 'https_required', { | ||
httpsUrl: httpsUrl, | ||
}); | ||
} | ||
}, | ||
}); | ||
|
||
module.exports = function httpsRequirements(proxy) { | ||
var middleware = new HttpsRequirements(proxy); | ||
|
||
return function(request, response, next) { | ||
middleware.handleRequest(request, response, next); | ||
}; | ||
}; |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ apis: | |
url_matches: | ||
- frontend_prefix: / | ||
backend_prefix: / | ||
apiSettings: | ||
require_https: optional |
Oops, something went wrong.