Created
March 24, 2012 12:37
-
-
Save lbdremy/2182217 to your computer and use it in GitHub Desktop.
WSSE Auth
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 Client(){ | |
this.host = 'api.cibo.io'; | |
this.path = ''; | |
} | |
Client.prototype.login = function(email,password,callback){ | |
this.email = email; // email can be an username or something else, it's just an unique value about the user. | |
this.password = password; | |
this.headers = { | |
'Authorization' : 'WSSE profile="UsernameToken"', | |
'X-WSSE': Client.getAuthHeader(this.email,this.password) | |
}; | |
var options = { | |
host : this.host, | |
path : this.path + '/auth', | |
headers : this.headers | |
}; | |
http.post(options,function(err,res){ | |
if(!err && res.statusCode !== 200 ){ | |
console.log('Authentication failed!'); | |
callback(err,'failed'); | |
}else{ | |
console.log('Authentication succeed!'); | |
callback(null,'succeed'); | |
} | |
}); | |
} | |
Client.getAuthHeader = function(email,password){ | |
var creationTimestamp = utils.getISODate(new Date()); | |
var nonce = 123456789; | |
var md5password = md5HexDigest(password); | |
var sum = (nonce + creationTimestamp + md5password); | |
// SHA1 Hex | |
var hash = sha1(sum); | |
//B64 | |
var passwordDigest = base64encode(hash); | |
// Build authentication header | |
var authHeader1a = 'UsernameToken Username="' + email + '"'; | |
var authHeader1b = 'PasswordDigest="' + passwordDigest + '"'; | |
var authHeader1c = 'Nonce="' + nonce + '"'; | |
var authHeader1d = 'Created="' + creationTimestamp + '"'; | |
// Authentication header | |
var authHeader = authHeader1a + ", " + authHeader1b + ", " + authHeader1c + ", " + authHeader1d; | |
return authHeader; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment