Created
October 2, 2013 16:01
-
-
Save afuchs/6796060 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script> | |
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script> | |
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script> | |
<style> | |
label { | |
display:block; | |
width: 150px; | |
} | |
textarea { | |
width: 500px; | |
height: 500px; | |
} | |
</style> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<label>apikey:</label><input type="textbox" id="key" /><br /> | |
<label>secret:</label><input type="textbox" id="secret" /><br /> | |
<label>salt:</label><input type="textbox" id="salt" /><br /> | |
<label>timestamp:</label><input type="textbox" id="timestamp" /><br /> | |
<label>encryption method:</label><input type="textbox" id="encryptionMethod" /><br /> | |
<button onclick="generateRequest()">Generate</button><br /> | |
<textarea id="auth"></textarea> | |
</body> | |
</html> |
This file contains hidden or 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
function generateUUID() { | |
var d = new Date().getTime(); | |
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
var r = (d + Math.random()*16)%16 | 0; | |
d = Math.floor(d/16); | |
return (c=='x' ? r : (r&0x7|0x8)).toString(16); | |
}); | |
return uuid; | |
}; | |
function AviaryAuth(apiKey, apiSecret) { | |
this.apiKey = apiKey; | |
this.apiSecret = apiSecret; | |
} | |
AviaryAuth.prototype.getSalt = function() { | |
return generateUUID(); | |
}; | |
AviaryAuth.prototype.getUnixTimestamp = function () { | |
return (new Date()).getTime() / 1000; | |
}; | |
AviaryAuth.prototype.getSignature = function(config) { | |
config = config || {}; | |
var auth = {}; | |
auth.apiKey = config.apiKey || this.apiKey; | |
auth.apiSecret = config.apiSecret || this.apiSecret; | |
auth.salt = config.salt || this.getSalt(); | |
auth.timestamp = config.timestamp || this.getUnixTimestamp(); | |
auth.encryptionMethod = config.encryptionMethod || 'sha1'; | |
var sig = auth.apiKey + auth.apiSecret + auth.timestamp + auth.salt; | |
// TODO: Validate encyrption_method | |
return CryptoJS[auth.encryptionMethod.toUpperCase()](sig).toString(); | |
// return crypto.createHash(auth.encryptionMethod).update(sig).digest('hex'); | |
}; | |
AviaryAuth.prototype.getAuthObject = function(config) { | |
config = config || {}; | |
var authObj = {}; | |
authObj.apiKey = config.apiKey || this.apiKey; | |
authObj.salt = config.salt || this.getSalt(); | |
authObj.timestamp = config.timestamp || this.getUnixTimestamp(); | |
authObj.encryptionMethod = config.encryptionMethod || 'sha1'; | |
authObj.apiSecret = config.apiSecret || this.apiSecret; | |
authObj.signature = this.getSignature(authObj); | |
delete authObj.apiSecret; | |
return authObj; | |
}; | |
function generateRequest() { | |
var key = document.getElementById('key').value; | |
var secret = document.getElementById('secret').value; | |
var timestamp = document.getElementById('timestamp').value; | |
var encryptionMethod = document.getElementById('encryptionMethod').value; | |
var salt = document.getElementById('salt').value; | |
var config = { | |
salt: salt, | |
timestamp: timestamp, | |
encryptionMethod: encryptionMethod | |
}; | |
var auth = new AviaryAuth(key, secret); | |
var fullAuth = auth.getAuthObject(config); | |
var authArea = document.getElementById('auth'); | |
console.log(fullAuth); | |
authArea.value = JSON.stringify(fullAuth, undefined, 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment