Last active
October 2, 2016 18:41
-
-
Save ChrisBAshton/ad772f616a64fa7d33b1ea875c844aea to your computer and use it in GitHub Desktop.
Security in JavaScript
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
var customRequest; | |
(function () { | |
function CustomRequestModule () { | |
function isSafeUrl (url) { | |
return url === 'https://safe-site.com'; | |
} | |
function getAccessToken () { | |
return 'my top secret access token'; | |
} | |
return { | |
post: function (params, callback) { | |
var paramsToSend = JSON.parse(JSON.stringify(params)); // copy the JSON | |
if (isSafeUrl(paramsToSend.url)) { | |
paramsToSend.headers.Authorization = 'token ' + getAccessToken(); | |
} | |
request(paramsToSend, callback); | |
} | |
} | |
}; | |
customRequest = new CustomRequestModule(); | |
})(); | |
require('./your-custom-module.js')(customRequest); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And put the secret token in a variable that is outside the closure so if you cast the closured function to a string all it outputs is the variable name.