Created
August 18, 2017 04:13
-
-
Save alejandrade/4b4f3377140385c874e3479ec7fbb3ec 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
class User { | |
constructor(user){ | |
user = user; | |
dynamoUser = { | |
Item: { | |
"password": { | |
S: user.password | |
}, | |
"email": { | |
S: user.email | |
}, | |
"salt": { | |
S: user.salt | |
}, | |
"createdOn": { | |
S :getCreatedOn() | |
} | |
}, | |
ReturnConsumedCapacity: "TOTAL", | |
TableName: "source.user" | |
}; | |
} | |
} | |
function getCreatedOn(){ | |
var now = new Date; | |
var utc_timestamp = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() , | |
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds()); | |
return utc_timestamp.toLocaleString(); | |
} | |
exports.User = function(user){return new User(user);} |
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
'use strict' | |
const AWS = require('aws-sdk'); | |
const model = require('./model/user.js') | |
const crypto = require('crypto'); | |
exports.handler = function (user, context, callback) { | |
console.log(user); | |
user = sha512(user); | |
console.log(user); | |
var dynamodb = new AWS.DynamoDB(); | |
var dynamoUser = model.User(user); | |
console.log("testing this") | |
console.log(dynamoUser); | |
/* | |
dynamodb.putItem(dynamoUser, function(err, data) { | |
if (err){ | |
console.log(err, err.stack); // an error occurred | |
context.fail('Failed!') | |
}else{ | |
console.log(data); | |
context.succeed('Success!') | |
} | |
}); | |
*/ | |
} | |
var sha512 = function(user){ | |
var salt = genRandomString(32); | |
var hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */ | |
hash.update(user.password); | |
user.password = hash.digest('hex'); | |
user.salt = salt; | |
return user; | |
}; | |
var genRandomString = function(length){ | |
return crypto.randomBytes(Math.ceil(length/2)) | |
.toString('hex') /** convert to hexadecimal format */ | |
.slice(0,length); /** return required number of characters */ | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment