-
Notifications
You must be signed in to change notification settings - Fork 16
/
dbscripts.json
66 lines (66 loc) · 63.7 KB
/
dbscripts.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{
"MVC3": {
"change_password": "function changePassword(email, newPassword, callback) {\n const crypto = require('crypto');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n // encrypt: true for Windows Azure enable this\n }\n });\n\n /**\n * hashPassword\n *\n * This function creates a hashed version of the password to store in the database.\n *\n * @password {[string]} the password entered by the user\n * @return {[string]} the hashed password\n */\n function hashPassword(password, salt) {\n // the default implementation uses HMACSHA256 and since Key length is 64\n // and default salt is 16 bytes, Membership will fill the buffer repeating the salt\n const key = Buffer.concat([salt, salt, salt, salt]);\n const hmac = crypto.createHmac('sha256', key);\n hmac.update(Buffer.from(password, 'ucs2'));\n\n return hmac.digest('base64');\n }\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) return callback(err);\n\n updateMembershipUser(email, newPassword, function(err, wasUpdated) {\n if (err) return callback(err); // this will return a 500\n\n callback(null, wasUpdated);\n });\n });\n\n function updateMembershipUser(email, newPassword, callback) {\n const salt = crypto.randomBytes(16);\n const hashedPassword = hashPassword(newPassword, salt);\n const updateMembership =\n 'UPDATE Memberships ' +\n 'SET Password=@NewPassword, PasswordSalt=@NewSalt, LastPasswordChangedDate=GETDATE() ' +\n 'WHERE Email=@Email';\n\n const updateMembershipQuery = new Request(updateMembership, function(membershipErr, membershipCount) {\n if (membershipErr) return callback(membershipErr);\n\n callback(null, membershipCount > 0);\n });\n\n updateMembershipQuery.addParameter('NewPassword', TYPES.VarChar, hashedPassword);\n updateMembershipQuery.addParameter('NewSalt', TYPES.VarChar, salt);\n updateMembershipQuery.addParameter('Email', TYPES.VarChar, email);\n\n connection.execSql(updateMembershipQuery);\n }\n}\n",
"create": "function create(user, callback) {\n const crypto = require('crypto');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true,\n // Required to retrieve userId needed for Membership entity creation\n rowCollectionOnRequestCompletion: true\n }\n });\n\n const applicationId = 'your-application-id-goes-here';\n\n /**\n * hashPassword\n *\n * This function creates a hashed version of the password to store in the database.\n *\n * @password {[string]} the password entered by the user\n * @return {[string]} the hashed password\n */\n function hashPassword(password, salt) {\n // the default implementation uses HMACSHA256 and since Key length is 64\n // and default salt is 16 bytes, Membership will fill the buffer repeating the salt\n const key = Buffer.concat([salt, salt, salt, salt]);\n const hmac = crypto.createHmac('sha256', key);\n hmac.update(Buffer.from(password, 'ucs2'));\n\n return hmac.digest('base64');\n }\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n // console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) {\n return callback(err);\n }\n createMembershipUser(user, function(err, user) {\n if (err) return callback(err); // this will return a 500\n if (!user) return callback(); // this will return a 401\n\n callback(null, user);\n });\n });\n\n function createMembershipUser(user, callback) {\n const userData = {\n UserName: user.email,\n ApplicationId: applicationId\n };\n const createUser =\n 'INSERT INTO Users (UserName, LastActivityDate, ApplicationId, UserId, IsAnonymous) ' +\n 'OUTPUT Inserted.UserId ' +\n 'VALUES (@UserName, GETDATE(), @ApplicationId, NEWID(), \\'false\\')';\n\n const createUserQuery = new Request(createUser, function(err, rowCount, rows) {\n if (err) return callback(err);\n\n // No records added\n if (rowCount === 0) return callback(null);\n\n const userId = rows[0][0].value;\n const salt = crypto.randomBytes(16);\n const membershipData = {\n ApplicationId: applicationId,\n Email: user.email,\n Password: hashPassword(user.password, salt),\n PasswordSalt: salt.toString('base64'),\n UserId: userId\n };\n\n const createMembership =\n 'INSERT INTO Memberships (ApplicationId, UserId, Password, PasswordFormat, ' +\n 'PasswordSalt, Email, isApproved, isLockedOut, CreateDate, LastLoginDate, ' +\n 'LastPasswordChangedDate, LastLockoutDate, FailedPasswordAttemptCount, ' +\n 'FailedPasswordAttemptWindowStart, FailedPasswordAnswerAttemptCount, ' +\n 'FailedPasswordAnswerAttemptWindowsStart) ' +\n 'VALUES ' +\n '(@ApplicationId, @UserId, @Password, 1, @PasswordSalt, ' +\n '@Email, \\'false\\', \\'false\\', GETDATE(), GETDATE(), GETDATE(), GETDATE(), 0, 0, 0, 0)';\n\n const createMembershipQuery = new Request(createMembership, function(err, rowCount) {\n if (err) return callback(err);\n\n if (rowCount === 0) return callback(null);\n\n callback(null, rowCount > 0);\n });\n\n createMembershipQuery.addParameter('ApplicationId', TYPES.VarChar, membershipData.ApplicationId);\n createMembershipQuery.addParameter('Email', TYPES.VarChar, membershipData.Email);\n createMembershipQuery.addParameter('Password', TYPES.VarChar, membershipData.Password);\n createMembershipQuery.addParameter('PasswordSalt', TYPES.VarChar, membershipData.PasswordSalt);\n createMembershipQuery.addParameter('UserId', TYPES.VarChar, membershipData.UserId);\n\n connection.execSql(createMembershipQuery);\n });\n\n createUserQuery.addParameter('UserName', TYPES.VarChar, userData.UserName);\n createUserQuery.addParameter('ApplicationId', TYPES.VarChar, userData.ApplicationId);\n\n connection.execSql(createUserQuery);\n }\n}\n",
"delete": "function remove(id, callback) {\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true,\n // Required to retrieve userId needed for Membership entity creation\n rowCollectionOnRequestCompletion: true\n }\n });\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n // console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) return callback(err);\n\n executeDelete(['Memberships', 'Users'], function(err) {\n if (err) return callback(err);\n\n callback(null);\n });\n });\n\n function executeDelete(tables, callback) {\n const query = tables.map(function(table) {\n return 'DELETE FROM ' + table + ' WHERE UserId = @UserId';\n }).join(';');\n const request = new Request(query, function(err) {\n if (err) return callback(err);\n\n callback(null);\n });\n request.addParameter('UserId', TYPES.VarChar, id);\n connection.execSql(request);\n }\n}\n",
"get_user": "function getByEmail(email, callback) {\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true // for Windows Azure\n }\n });\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) return callback(err);\n\n var user = {};\n const query =\n 'SELECT Memberships.UserId, Email, Users.UserName ' +\n 'FROM Memberships INNER JOIN Users ' +\n 'ON Users.UserId = Memberships.UserId ' +\n 'WHERE Memberships.Email = @Username OR Users.UserName = @Username';\n\n const getMembershipQuery = new Request(query, function(err, rowCount) {\n if (err) return callback(err);\n if (rowCount < 1) return callback();\n\n callback(null, user);\n });\n\n getMembershipQuery.addParameter('Username', TYPES.VarChar, email);\n\n getMembershipQuery.on('row', function(fields) {\n user = {\n user_id: fields.UserId.value,\n nickname: fields.UserName.value,\n email: fields.Email.value\n };\n });\n\n connection.execSql(getMembershipQuery);\n });\n}\n",
"login": "function login(email, password, callback) {\n const crypto = require('crypto');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true // for Windows Azure\n }\n });\n\n /**\n * hashPassword\n *\n * This function creates a hashed version of the password to store in the database.\n *\n * @password {[string]} the password entered by the user\n * @return {[string]} the hashed password\n */\n function hashPassword(password, salt) {\n // the default implementation uses HMACSHA256 and since Key length is 64\n // and default salt is 16 bytes, Membership will fill the buffer repeating the salt\n const key = Buffer.concat([salt, salt, salt, salt]);\n const hmac = crypto.createHmac('sha256', key);\n hmac.update(Buffer.from(password, 'ucs2'));\n\n return hmac.digest('base64');\n }\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) return callback(err);\n\n getMembershipUser(email, function(err, user) {\n if (err || !user || !user.profile || !user.password) return callback(err || new WrongUsernameOrPasswordError(email));\n\n const salt = Buffer.from(user.password.salt, 'base64');\n\n if (hashPassword(password, salt).toString('base64') !== user.password.password) {\n return callback(new WrongUsernameOrPasswordError(email));\n }\n\n callback(null, user.profile);\n });\n });\n\n\n // Membership Provider implementation used on Microsoft.AspNet.Providers NuGet\n\n /**\n * getMembershipUser\n *\n * This function gets a username or email and returns a the user membership provider\n * info, password hashes and salt\n *\n * @usernameOrEmail {[string]} the username or email, the method will do a\n * query on both with an OR\n *\n * @callback {[Function]} first argument will be the Error if any,\n * and second argument will be a user object\n */\n function getMembershipUser(usernameOrEmail, done) {\n var user = null;\n const query =\n 'SELECT Memberships.UserId, Email, Users.UserName, Password ' +\n 'FROM Memberships INNER JOIN Users ' +\n 'ON Users.UserId = Memberships.UserId ' +\n 'WHERE Memberships.Email = @Username OR Users.UserName = @Username';\n\n const getMembershipQuery = new Request(query, function(err, rowCount) {\n if (err || rowCount < 1) return done(err);\n\n done(err, user);\n });\n\n getMembershipQuery.addParameter('Username', TYPES.VarChar, usernameOrEmail);\n\n getMembershipQuery.on('row', function(fields) {\n user = {\n profile: {\n user_id: fields.UserId.value,\n nickname: fields.UserName.value,\n email: fields.Email.value,\n },\n password: {\n password: fields.Password.value,\n salt: fields.PasswordSalt.value\n }\n };\n });\n\n connection.execSql(getMembershipQuery);\n }\n}\n",
"verify": "function verify(email, callback) {\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true,\n // Required to retrieve userId needed for Membership entity creation\n rowCollectionOnRequestCompletion: true\n }\n });\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) return callback(err);\n\n verifyMembershipUser(email, function(err, wasUpdated) {\n if (err) return callback(err); // this will return a 500\n\n callback(null, wasUpdated);\n });\n });\n\n function verifyMembershipUser(email, callback) {\n // isApproved field is the email verification flag\n const updateMembership =\n 'UPDATE Memberships SET isApproved = \\'true\\' ' +\n 'WHERE isApproved = \\'false\\' AND Email = @Email';\n\n const updateMembershipQuery = new Request(updateMembership, function(err, rowCount) {\n if (err) {\n return callback(err);\n }\n callback(null, rowCount > 0);\n });\n\n updateMembershipQuery.addParameter('Email', TYPES.VarChar, email);\n\n connection.execSql(updateMembershipQuery);\n }\n}\n"
},
"MVC4": {
"change_password": "function changePassword(email, newPassword, callback) {\n const crypto = require('crypto');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n // encrypt: true for Windows Azure enable this\n }\n });\n\n /**\n * hashPassword\n *\n * This function hashes a password using HMAC SHA256 algorythm.\n *\n * @password {[string]} password to be hased\n * @salt {[string]} salt to be used in the hashing process\n * @callback {[function]} callback to be called after hashing the password\n */\n function hashPassword(password, salt, callback) {\n const iterations = 1000;\n const passwordHashLength = 32;\n\n crypto.pbkdf2(password, salt, iterations, passwordHashLength, 'sha1', function (err, hashed) {\n if (err) return callback(err);\n\n const result = Buffer.concat([Buffer.from([0], 1), salt, Buffer.from(hashed, 'binary')]);\n const resultBase64 = result.toString('base64');\n\n callback(null, resultBase64);\n });\n }\n\n connection.on('debug', function (text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function (text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n updateMembershipUser(email, newPassword, function (err, wasUpdated) {\n if (err) return callback(err); // this will return a 500\n\n callback(null, wasUpdated);\n });\n });\n\n function findUserId(email, callback) {\n const findUserIdFromEmail =\n 'SELECT UserProfile.UserId FROM ' +\n 'UserProfile INNER JOIN webpages_Membership ' +\n 'ON UserProfile.UserId=webpages_Membership.UserId ' +\n 'WHERE UserName=@Email';\n\n const findUserIdFromEmailQuery = new Request(findUserIdFromEmail, function (err, rowCount, rows) {\n if (err || rowCount < 1) return callback(err);\n\n const userId = rows[0][0].value;\n\n callback(null, userId);\n });\n\n findUserIdFromEmailQuery.addParameter('Email', TYPES.VarChar, email);\n\n connection.execSql(findUserIdFromEmailQuery);\n }\n\n function updateMembershipUser(email, newPassword, callback) {\n findUserId(email, function (err, userId) {\n if (err || !userId) return callback(err);\n\n const salt = crypto.randomBytes(16);\n\n const updateMembership =\n 'UPDATE webpages_Membership ' +\n 'SET Password=@NewPassword, PasswordChangedDate=GETDATE() ' +\n 'WHERE UserId=@UserId';\n\n const updateMembershipQuery = new Request(updateMembership, function (err, rowCount) {\n callback(err, rowCount > 0);\n });\n\n hashPassword(newPassword, salt, function (err, hashedPassword) {\n if (err) return callback(err);\n\n updateMembershipQuery.addParameter('NewPassword', TYPES.VarChar, hashedPassword);\n updateMembershipQuery.addParameter('UserId', TYPES.VarChar, userId);\n\n connection.execSql(updateMembershipQuery);\n });\n });\n }\n}\n",
"create": "function create(user, callback) {\n const crypto = require('crypto');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true,\n // Required to retrieve userId needed for Membership entity creation\n rowCollectionOnRequestCompletion: true\n }\n });\n\n /**\n * hashPassword\n *\n * This function hashes a password using HMAC SHA256 algorythm.\n *\n * @password {[string]} password to be hased\n * @salt {[string]} salt to be used in the hashing process\n * @callback {[function]} callback to be called after hashing the password\n */\n function hashPassword(password, salt, callback) {\n const iterations = 1000;\n const passwordHashLength = 32;\n\n crypto.pbkdf2(password, salt, iterations, passwordHashLength, 'sha1', function (err, hashed) {\n if (err) return callback(err);\n\n const result = Buffer.concat([Buffer.from([0], 1), salt, Buffer.from(hashed, 'binary')]);\n const resultBase64 = result.toString('base64');\n\n callback(null, resultBase64);\n });\n }\n\n connection.on('debug', function (text) {\n // if you have connection issues, uncomment this to get more detailed info\n // console.log(text);\n }).on('errorMessage', function (text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n const createUser =\n 'INSERT INTO UserProfile (UserName) ' +\n 'OUTPUT Inserted.UserId ' +\n 'VALUES (@UserName)';\n\n const createUserQuery = new Request(createUser, function (err, rowCount, rows) {\n if (err || rowCount === 0) return callback(err);\n\n const userId = rows[0][0].value;\n const salt = crypto.randomBytes(16);\n\n const createMembership =\n 'INSERT INTO webpages_Membership ' +\n '(UserId, CreateDate, IsConfirmed, PasswordFailuresSinceLastSuccess, Password, PasswordSalt) ' +\n 'VALUES ' +\n '(@UserId, GETDATE(), \\'false\\', 0, @Password, \\'\\')';\n\n const createMembershipQuery = new Request(createMembership, function (err, rowCount) {\n if (err || rowCount === 0) return callback(err);\n\n callback(null, rowCount > 0);\n });\n\n hashPassword(user.password, salt, function (err, hashedPassword) {\n if (err) return callback(err);\n createMembershipQuery.addParameter('Password', TYPES.VarChar, hashedPassword);\n createMembershipQuery.addParameter('PasswordSalt', TYPES.VarChar, salt.toString('base64'));\n createMembershipQuery.addParameter('UserId', TYPES.VarChar, userId);\n\n connection.execSql(createMembershipQuery);\n });\n });\n\n createUserQuery.addParameter('UserName', TYPES.VarChar, user.email);\n\n connection.execSql(createUserQuery);\n });\n}\n",
"delete": "function remove(id, callback) {\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true,\n // Required to retrieve userId needed for Membership entity creation\n rowCollectionOnRequestCompletion: true\n }\n });\n\n connection.on('debug', function (text) {\n // if you have connection issues, uncomment this to get more detailed info\n // console.log(text);\n }).on('errorMessage', function (text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n executeDelete(['webpages_Membership', 'UserProfile'], function (err) {\n if (err) return callback(err);\n callback(null);\n });\n });\n\n function executeDelete(tables, callback) {\n const query = tables.map(function (table) {\n return 'DELETE FROM ' + table + ' WHERE UserId = @UserId';\n }).join(';');\n const request = new Request(query, function (err) {\n if (err) return callback(err);\n callback(null);\n });\n request.addParameter('UserId', TYPES.VarChar, id);\n connection.execSql(request);\n }\n}\n",
"get_user": "function getByEmail(email, callback) {\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true // for Windows Azure\n }\n });\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) return callback(err);\n\n var user = {};\n const query =\n 'SELECT webpages_Membership.UserId, UserName, UserProfile.UserName from webpages_Membership ' +\n 'INNER JOIN UserProfile ON UserProfile.UserId = webpages_Membership.UserId ' +\n 'WHERE UserProfile.UserName = @Username';\n\n const getMembershipQuery = new Request(query, function (err, rowCount) {\n if (err) return callback(err);\n if (rowCount < 1) return callback();\n\n callback(null, user);\n });\n\n getMembershipQuery.addParameter('Username', TYPES.VarChar, email);\n\n getMembershipQuery.on('row', function (fields) {\n user = {\n user_id: fields.UserId.value,\n nickname: fields.UserName.value,\n email: fields.UserName.value\n };\n });\n\n connection.execSql(getMembershipQuery);\n });\n}\n",
"login": "function login(email, password, callback) {\n const crypto = require('crypto');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true // for Windows Azure\n }\n });\n\n function fixedTimeComparison(a, b) {\n var mismatch = (a.length === b.length ? 0 : 1);\n if (mismatch) {\n b = a;\n }\n\n for (var i = 0, il = a.length; i < il; ++i) {\n const ac = a.charCodeAt(i);\n const bc = b.charCodeAt(i);\n mismatch += (ac === bc ? 0 : 1);\n }\n\n return (mismatch === 0);\n }\n\n\n /**\n * validatePassword\n *\n * This function gets the password entered by the user, and the original password\n * hash and salt from database and performs an HMAC SHA256 hash.\n *\n * @password {[string]} the password entered by the user\n * @originalHash {[string]} the original password hashed from the database\n * (including the salt).\n * @return {[bool]} true if password validates\n */\n function validatePassword(password, originalHash, callback) {\n const iterations = 1000;\n const hashBytes = Buffer.from(originalHash, 'base64');\n const salt = hashBytes.slice(1, 17);\n const hash = hashBytes.slice(17, 49);\n crypto.pbkdf2(password, salt, iterations, hash.length, 'sha1', function(err, hashed) {\n if (err) return callback(err);\n\n const hashedBase64 = Buffer.from(hashed, 'binary').toString('base64');\n const isValid = fixedTimeComparison(hash.toString('base64'), hashedBase64);\n\n return callback(null, isValid);\n });\n }\n\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function(err) {\n if (err) return callback(err);\n getMembershipUser(email, function(err, user) {\n if (err || !user || !user.profile) return callback(err || new WrongUsernameOrPasswordError(email));\n\n validatePassword(password, user.password, function(err, isValid) {\n if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));\n\n callback(null, user.profile);\n });\n });\n });\n\n\n // Membership Provider implementation used on Microsoft.AspNet.Providers NuGet\n\n /**\n * getMembershipUser\n *\n * This function gets a username or email and returns a user info, password hashes and salt\n *\n * @usernameOrEamil {[string]} the username or email, the method will do a query\n * on both with an OR\n * @callback {[Function]} first argument will be the Error if any, and second\n * argument will be a user object\n */\n function getMembershipUser(usernameOrEmail, done) {\n var user = null;\n const query =\n 'SELECT webpages_Membership.UserId, UserName, UserProfile.UserName, Password from webpages_Membership ' +\n 'INNER JOIN UserProfile ON UserProfile.UserId = webpages_Membership.UserId ' +\n 'WHERE UserProfile.UserName = @Username';\n\n const getMembershipQuery = new Request(query, function(err, rowCount) {\n if (err || rowCount < 1) return done(err);\n\n done(err, user);\n });\n\n getMembershipQuery.addParameter('Username', TYPES.VarChar, usernameOrEmail);\n\n getMembershipQuery.on('row', function(fields) {\n user = {\n profile: {\n user_id: fields.UserId.value,\n nickname: fields.UserName.value,\n email: fields.UserName.value,\n },\n password: fields.Password.value\n };\n });\n\n connection.execSql(getMembershipQuery);\n }\n}\n",
"verify": "function verify (email, callback) {\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'the username',\n password: 'the password',\n server: 'the server',\n options: {\n database: 'the db name',\n encrypt: true,\n // Required to retrieve userId needed for Membership entity creation\n rowCollectionOnRequestCompletion: true\n }\n });\n\n connection.on('debug', function(text) {\n // if you have connection issues, uncomment this to get more detailed info\n //console.log(text);\n }).on('errorMessage', function(text) {\n // this will show any errors when connecting to the SQL database or with the SQL statements\n console.log(JSON.stringify(text));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n verifyMembershipUser(email, function(err, wasUpdated) {\n if (err) return callback(err); // this will return a 500\n\n callback(null, wasUpdated);\n });\n });\n\n function findUserId(email, callback) {\n const findUserIdFromEmail =\n 'SELECT UserProfile.UserId FROM ' +\n 'UserProfile INNER JOIN webpages_Membership ' +\n 'ON UserProfile.UserId = webpages_Membership.UserId ' +\n 'WHERE UserName = @Username';\n\n const findUserIdFromEmailQuery = new Request(findUserIdFromEmail, function (err, rowCount, rows) {\n if (err || rowCount < 1) return callback(err);\n\n const userId = rows[0][0].value;\n\n callback(null, userId);\n });\n\n findUserIdFromEmailQuery.addParameter('Username', TYPES.VarChar, email);\n\n connection.execSql(findUserIdFromEmailQuery);\n }\n\n function verifyMembershipUser(email, callback) {\n findUserId(email, function (err, userId) {\n if (err || !userId) return callback(err);\n\n // isConfirmed field is the email verification flag\n const updateMembership =\n 'UPDATE webpages_Membership SET isConfirmed = \\'true\\' ' +\n 'WHERE isConfirmed = \\'false\\' AND UserId = @UserId';\n\n const updateMembershipQuery = new Request(updateMembership, function (err, rowCount) {\n return callback(err, rowCount > 0);\n });\n\n updateMembershipQuery.addParameter('UserId', TYPES.VarChar, userId);\n\n connection.execSql(updateMembershipQuery);\n });\n }\n}\n"
},
"bare": {
"change_password": "function changePassword(email, newPassword, callback) {\n // This script should change the password stored for the current user in your\n // database. It is executed when the user clicks on the confirmation link\n // after a reset password request.\n // The content and behavior of password confirmation emails can be customized\n // here: https://manage.auth0.com/#/emails\n // The `newPassword` parameter of this function is in plain text. It must be\n // hashed/salted to match whatever is stored in your database.\n //\n // There are three ways that this script can finish:\n // 1. The user's password was updated successfully:\n // callback(null, true);\n // 2. The user's password was not updated:\n // callback(null, false);\n // 3. Something went wrong while trying to reach your database:\n // callback(new Error(\"my error message\"));\n //\n // If an error is returned, it will be passed to the query string of the page\n // where the user is being redirected to after clicking the confirmation link.\n // For example, returning `callback(new Error(\"error\"))` and redirecting to\n // https://example.com would redirect to the following URL:\n // https://example.com?email=alice%40example.com&message=error&success=false\n\n const msg = 'Please implement the Change Password script for this database ' +\n 'connection at https://manage.auth0.com/#/connections/database';\n return callback(new Error(msg));\n}\n",
"create": "function create(user, callback) {\n // This script should create a user entry in your existing database. It will\n // be executed when a user attempts to sign up, or when a user is created\n // through the Auth0 dashboard or API.\n // When this script has finished executing, the Login script will be\n // executed immediately afterwards, to verify that the user was created\n // successfully.\n //\n // The user object will always contain the following properties:\n // * email: the user's email\n // * password: the password entered by the user, in plain text\n // * tenant: the name of this Auth0 account\n // * client_id: the client ID of the application where the user signed up, or\n // API key if created through the API or Auth0 dashboard\n // * connection: the name of this database connection\n //\n // There are three ways this script can finish:\n // 1. A user was successfully created\n // callback(null);\n // 2. This user already exists in your database\n // callback(new ValidationError(\"user_exists\", \"my error message\"));\n // 3. Something went wrong while trying to reach your database\n // callback(new Error(\"my error message\"));\n\n const msg = 'Please implement the Create script for this database connection ' +\n 'at https://manage.auth0.com/#/connections/database';\n return callback(new Error(msg));\n}\n",
"delete": "function remove(id, callback) {\n // This script remove a user from your existing database.\n // It is executed whenever a user is deleted from the API or Auth0 dashboard.\n //\n // There are two ways that this script can finish:\n // 1. The user was removed successfully:\n // callback(null);\n // 2. Something went wrong while trying to reach your database:\n // callback(new Error(\"my error message\"));\n\n const msg = 'Please implement the Delete script for this database ' +\n 'connection at https://manage.auth0.com/#/connections/database';\n return callback(new Error(msg));\n}\n",
"get_user": "function getByEmail(email, callback) {\n // This script should retrieve a user profile from your existing database,\n // without authenticating the user.\n // It is used to check if a user exists before executing flows that do not\n // require authentication (signup and password reset).\n //\n // There are three ways this script can finish:\n // 1. A user was successfully found. The profile should be in the following\n // format: https://auth0.com/docs/users/normalized/auth0/normalized-user-profile-schema.\n // callback(null, profile);\n // 2. A user was not found\n // callback(null);\n // 3. Something went wrong while trying to reach your database:\n // callback(new Error(\"my error message\"));\n\n const msg = 'Please implement the Get User script for this database connection ' +\n 'at https://manage.auth0.com/#/connections/database';\n return callback(new Error(msg));\n}\n",
"login": "function login(email, password, callback) {\n // This script should authenticate a user against the credentials stored in\n // your database.\n // It is executed when a user attempts to log in or immediately after signing\n // up (as a verification that the user was successfully signed up).\n //\n // Everything returned by this script will be set as part of the user profile\n // and will be visible by any of the tenant admins. Avoid adding attributes\n // with values such as passwords, keys, secrets, etc.\n //\n // The `password` parameter of this function is in plain text. It must be\n // hashed/salted to match whatever is stored in your database. For example:\n //\n // var bcrypt = require('[email protected]');\n // bcrypt.compare(password, dbPasswordHash, function(err, res)) { ... }\n //\n // There are three ways this script can finish:\n // 1. The user's credentials are valid. The returned user profile should be in\n // the following format: https://auth0.com/docs/users/normalized/auth0/normalized-user-profile-schema\n // var profile = {\n // user_id: ..., // user_id is mandatory\n // email: ...,\n // [...]\n // };\n // callback(null, profile);\n // 2. The user's credentials are invalid\n // callback(new WrongUsernameOrPasswordError(email, \"my error message\"));\n //\n // Note: Passing no arguments or a falsey first argument to\n // `WrongUsernameOrPasswordError` will result in the error being logged as\n // an `fu` event (invalid username/email) with an empty string for a user_id.\n // Providing a truthy first argument will result in the error being logged\n // as an `fp` event (the user exists, but the password is invalid) with a\n // user_id value of \"auth0|<first argument>\". See the `Log Event Type Codes`\n // documentation for more information about these event types:\n // https://auth0.com/docs/deploy-monitor/logs/log-event-type-codes\n // 3. Something went wrong while trying to reach your database\n // callback(new Error(\"my error message\"));\n //\n // A list of Node.js modules which can be referenced is available here:\n //\n // https://tehsis.github.io/webtaskio-canirequire/\n\n const msg = 'Please implement the Login script for this database connection ' +\n 'at https://manage.auth0.com/#/connections/database';\n return callback(new Error(msg));\n}\n",
"verify": "function verify(email, callback) {\n // This script should mark the current user's email address as verified in\n // your database.\n // It is executed whenever a user clicks the verification link sent by email.\n // These emails can be customized at https://manage.auth0.com/#/emails.\n // It is safe to assume that the user's email already exists in your database,\n // because verification emails, if enabled, are sent immediately after a\n // successful signup.\n //\n // There are two ways that this script can finish:\n // 1. The user's email was verified successfully\n // callback(null, true);\n // 2. Something went wrong while trying to reach your database:\n // callback(new Error(\"my error message\"));\n //\n // If an error is returned, it will be passed to the query string of the page\n // where the user is being redirected to after clicking the verification link.\n // For example, returning `callback(new Error(\"error\"))` and redirecting to\n // https://example.com would redirect to the following URL:\n // https://example.com?email=alice%40example.com&message=error&success=false\n\n const msg = 'Please implement the Verify script for this database connection ' +\n 'at https://manage.auth0.com/#/connections/database';\n return callback(new Error(msg));\n}\n"
},
"mongo": {
"change_password": "function changePassword(email, newPassword, callback) {\n const bcrypt = require('bcrypt');\n const MongoClient = require('[email protected]').MongoClient;\n const client = new MongoClient('mongodb://user:pass@localhost');\n\n client.connect(function (err) {\n if (err) return callback(err);\n\n const db = client.db('db-name');\n const users = db.collection('users');\n\n bcrypt.hash(newPassword, 10, function (err, hash) {\n if (err) {\n client.close();\n return callback(err);\n }\n\n users.update({ email: email }, { $set: { password: hash } }, function (err, result) {\n client.close();\n if (err) return callback(err);\n callback(null, result.result.n > 0);\n });\n });\n });\n}\n",
"create": "function create(user, callback) {\n const bcrypt = require('bcrypt');\n const MongoClient = require('[email protected]').MongoClient;\n const client = new MongoClient('mongodb://user:pass@localhost');\n\n client.connect(function (err) {\n if (err) return callback(err);\n\n const db = client.db('db-name');\n const users = db.collection('users');\n\n users.findOne({ email: user.email }, function (err, withSameMail) {\n if (err || withSameMail) {\n client.close();\n return callback(err || new Error('the user already exists'));\n }\n\n bcrypt.hash(user.password, 10, function (err, hash) {\n if (err) {\n client.close();\n return callback(err);\n }\n\n user.password = hash;\n users.insert(user, function (err, inserted) {\n client.close();\n\n if (err) return callback(err);\n callback(null);\n });\n });\n });\n });\n}\n",
"delete": "function remove(id, callback) {\n const MongoClient = require('[email protected]').MongoClient;\n const client = new MongoClient('mongodb://user:pass@localhost');\n\n client.connect(function (err) {\n if (err) return callback(err);\n\n const db = client.db('db-name');\n const users = db.collection('users');\n\n users.remove({ _id: id }, function (err) {\n client.close();\n\n if (err) return callback(err);\n callback(null);\n });\n });\n\n}\n",
"get_user": "function getByEmail(email, callback) {\n const MongoClient = require('[email protected]').MongoClient;\n const client = new MongoClient('mongodb://user:pass@localhost');\n\n client.connect(function (err) {\n if (err) return callback(err);\n\n const db = client.db('db-name');\n const users = db.collection('users');\n\n users.findOne({ email: email }, function (err, user) {\n client.close();\n\n if (err) return callback(err);\n if (!user) return callback(null, null);\n\n return callback(null, {\n user_id: user._id.toString(),\n nickname: user.nickname,\n email: user.email\n });\n });\n });\n}\n",
"login": "function login(email, password, callback) {\n const bcrypt = require('bcrypt');\n const MongoClient = require('[email protected]').MongoClient;\n const client = new MongoClient('mongodb://user:pass@localhost');\n\n client.connect(function (err) {\n if (err) return callback(err);\n\n const db = client.db('db-name');\n const users = db.collection('users');\n\n users.findOne({ email: email }, function (err, user) {\n if (err || !user) {\n client.close();\n return callback(err || new WrongUsernameOrPasswordError(email));\n }\n\n bcrypt.compare(password, user.password, function (err, isValid) {\n client.close();\n\n if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));\n\n return callback(null, {\n user_id: user._id.toString(),\n nickname: user.nickname,\n email: user.email\n });\n });\n });\n });\n}\n",
"verify": "function verify (email, callback) {\n const MongoClient = require('[email protected]').MongoClient;\n const client = new MongoClient('mongodb://user:pass@localhost');\n\n client.connect(function (err) {\n if (err) return callback(err);\n\n const db = client.db('db-name');\n const users = db.collection('users');\n const query = { email: email, email_verified: false };\n\n users.update(query, { $set: { email_verified: true } }, function (err, result) {\n client.close();\n\n if (err) return callback(err);\n callback(null, result.result.n > 0);\n });\n });\n}\n"
},
"mysql": {
"change_password": "function changePassword(email, newPassword, callback) {\n const mysql = require('mysql');\n const bcrypt = require('bcrypt');\n\n const connection = mysql.createConnection({\n host: 'localhost',\n user: 'me',\n password: 'secret',\n database: 'mydb'\n });\n\n connection.connect();\n\n const query = 'UPDATE users SET password = ? WHERE email = ?';\n\n bcrypt.hash(newPassword, 10, function(err, hash) {\n if (err) return callback(err);\n\n connection.query(query, [ hash, email ], function(err, results) {\n if (err) return callback(err);\n callback(null, results.length > 0);\n });\n });\n}\n",
"create": "function create(user, callback) {\n const mysql = require('mysql');\n const bcrypt = require('bcrypt');\n\n const connection = mysql.createConnection({\n host: 'localhost',\n user: 'me',\n password: 'secret',\n database: 'mydb'\n });\n\n connection.connect();\n\n const query = 'INSERT INTO users SET ?';\n\n bcrypt.hash(user.password, 10, function(err, hash) {\n if (err) return callback(err);\n\n const insert = {\n password: hash,\n email: user.email\n };\n\n connection.query(query, insert, function(err, results) {\n if (err) return callback(err);\n if (results.length === 0) return callback();\n callback(null);\n });\n });\n}\n",
"delete": "function remove(id, callback) {\n const mysql = require('mysql');\n\n const connection = mysql.createConnection({\n host: 'localhost',\n user: 'me',\n password: 'secret',\n database: 'mydb'\n });\n\n connection.connect();\n\n const query = 'DELETE FROM users WHERE id = ?';\n\n connection.query(query, [ id ], function(err) {\n if (err) return callback(err);\n callback(null);\n });\n}\n",
"get_user": "function getByEmail(email, callback) {\n const mysql = require('mysql');\n\n const connection = mysql.createConnection({\n host: 'localhost',\n user: 'me',\n password: 'secret',\n database: 'mydb'\n });\n\n connection.connect();\n\n const query = 'SELECT id, nickname, email FROM users WHERE email = ?';\n\n connection.query(query, [ email ], function(err, results) {\n if (err || results.length === 0) return callback(err || null);\n\n const user = results[0];\n callback(null, {\n user_id: user.id.toString(),\n nickname: user.nickname,\n email: user.email\n });\n });\n}\n",
"login": "function login(email, password, callback) {\n const mysql = require('mysql');\n const bcrypt = require('bcrypt');\n\n const connection = mysql.createConnection({\n host: 'localhost',\n user: 'me',\n password: 'secret',\n database: 'mydb'\n });\n\n connection.connect();\n\n const query = 'SELECT id, nickname, email, password FROM users WHERE email = ?';\n\n connection.query(query, [ email ], function(err, results) {\n if (err) return callback(err);\n if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));\n const user = results[0];\n\n bcrypt.compare(password, user.password, function(err, isValid) {\n if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));\n\n callback(null, {\n user_id: user.id.toString(),\n nickname: user.nickname,\n email: user.email\n });\n });\n });\n}\n",
"verify": "function verify(email, callback) {\n const mysql = require('mysql');\n\n const connection = mysql.createConnection({\n host: 'localhost',\n user: 'me',\n password: 'secret',\n database: 'mydb'\n });\n\n connection.connect();\n\n const query = 'UPDATE users SET email_Verified = true WHERE email_Verified = false AND email = ?';\n\n connection.query(query, [ email ], function(err, results) {\n if (err) return callback(err);\n\n callback(null, results.length > 0);\n });\n\n}\n"
},
"postgres": {
"change_password": "function changePassword (email, newPassword, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n const bcrypt = require('bcrypt');\n const postgres = require('pg');\n\n const conString = 'postgres://user:pass@localhost/mydb';\n postgres.connect(conString, function (err, client, done) {\n if (err) return callback(err);\n\n bcrypt.hash(newPassword, 10, function (err, hash) {\n if (err) return callback(err);\n\n const query = 'UPDATE users SET password = $1 WHERE email = $2';\n client.query(query, [hash, email], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n return callback(err, result && result.rowCount > 0);\n });\n });\n });\n}\n",
"create": "function create(user, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n const bcrypt = require('bcrypt');\n const postgres = require('pg');\n\n const conString = 'postgres://user:pass@localhost/mydb';\n postgres.connect(conString, function (err, client, done) {\n if (err) return callback(err);\n\n bcrypt.hash(user.password, 10, function (err, hashedPassword) {\n if (err) return callback(err);\n\n const query = 'INSERT INTO users(email, password) VALUES ($1, $2)';\n client.query(query, [user.email, hashedPassword], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n return callback(err);\n });\n });\n });\n}\n",
"delete": "function remove(id, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n const postgres = require('pg');\n\n const conString = 'postgres://user:pass@localhost/mydb';\n postgres.connect(conString, function (err, client, done) {\n if (err) return callback(err);\n\n const query = 'DELETE FROM users WHERE id = $1';\n client.query(query, [id], function (err) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n return callback(err);\n });\n });\n\n}\n",
"get_user": "function loginByEmail(email, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n const postgres = require('pg');\n\n const conString = 'postgres://user:pass@localhost/mydb';\n postgres.connect(conString, function (err, client, done) {\n if (err) return callback(err);\n\n const query = 'SELECT id, nickname, email FROM users WHERE email = $1';\n client.query(query, [email], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n if (err || result.rows.length === 0) return callback(err);\n\n const user = result.rows[0];\n\n return callback(null, {\n user_id: user.id,\n nickname: user.nickname,\n email: user.email\n });\n });\n });\n}\n",
"login": "function login(email, password, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n const bcrypt = require('bcrypt');\n const postgres = require('pg');\n\n const conString = 'postgres://user:pass@localhost/mydb';\n postgres.connect(conString, function (err, client, done) {\n if (err) return callback(err);\n\n const query = 'SELECT id, nickname, email, password FROM users WHERE email = $1';\n client.query(query, [email], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n if (err || result.rows.length === 0) return callback(err || new WrongUsernameOrPasswordError(email));\n\n const user = result.rows[0];\n\n bcrypt.compare(password, user.password, function (err, isValid) {\n if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));\n\n return callback(null, {\n user_id: user.id,\n nickname: user.nickname,\n email: user.email\n });\n });\n });\n });\n}\n",
"verify": "function verify (email, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n const postgres = require('pg');\n\n const conString = 'postgres://user:pass@localhost/mydb';\n postgres.connect(conString, function (err, client, done) {\n if (err) return callback(err);\n\n const query = 'UPDATE users SET email_Verified = true WHERE email_Verified = false AND email = $1';\n client.query(query, [email], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n return callback(err, result && result.rowCount > 0);\n });\n });\n}\n"
},
"request": {
"change_password": "function changePassword(email, newPassword, callback) {\n const request = require('request');\n\n request.put({\n url: 'https://localhost/users',\n json: { email: email, password: newPassword }\n //for more options check:\n //https://github.com/mikeal/request#requestoptions-callback\n }, function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode === 401) return callback();\n callback(null, body);\n });\n}\n",
"create": "function create(user, callback) {\n const request = require('request');\n\n request.post({\n url: 'https://localhost/users',\n json: user\n //for more options check:\n //https://github.com/mikeal/request#requestoptions-callback\n }, function(err, response, body) {\n if (err) return callback(err);\n\n callback(null);\n });\n}\n",
"delete": "function remove(id, callback) {\n const request = require('request');\n\n request.del({\n url: 'https://localhost/users/' + id\n // for more options check:\n // https://github.com/mikeal/request#requestoptions-callback\n }, function(err, response, body) {\n if (err) return callback(err);\n\n callback(null);\n });\n}\n",
"get_user": "function loginByEmail(email, callback) {\n const request = require('request');\n\n request.get({\n url: 'https://localhost/users-by-email/' + email\n //for more options check:\n //https://github.com/mikeal/request#requestoptions-callback\n }, function(err, response, body) {\n if (err) return callback(err);\n\n const user = JSON.parse(body);\n\n callback(null, {\n user_id: user.user_id.toString(),\n nickname: user.nickname,\n email: user.email\n });\n });\n}\n",
"login": "function login(email, password, callback) {\n const request = require('request');\n\n request.get({\n url: 'https://localhost/profile',\n auth: {\n username: email,\n password: password\n }\n //for more options check:\n //https://github.com/mikeal/request#requestoptions-callback\n }, function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode === 401) return callback();\n const user = JSON.parse(body);\n\n callback(null, {\n user_id: user.user_id.toString(),\n nickname: user.nickname,\n email: user.email\n });\n });\n}\n",
"verify": "function verify(email, callback) {\n const request = require('request');\n\n request.put({\n url: 'https://localhost/users',\n json: { email: email }\n //for more options check:\n //https://github.com/mikeal/request#requestoptions-callback\n }, function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode === 401) return callback();\n\n callback(null, body);\n });\n}\n"
},
"sqlserver": {
"change_password": "function changePassword (email, newPassword, callback) {\n //this example uses the \"tedious\" library\n //more info here: http://tediousjs.github.io/tedious/\n const bcrypt = require('bcrypt');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'test',\n password: 'test',\n server: 'localhost',\n options: {\n database: 'mydb'\n }\n });\n\n const query = 'UPDATE dbo.Users SET Password = @NewPassword WHERE Email = @Email';\n\n connection.on('debug', function(text) {\n console.log(text);\n }).on('errorMessage', function(text) {\n console.log(JSON.stringify(text, null, 2));\n }).on('infoMessage', function(text) {\n console.log(JSON.stringify(text, null, 2));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n const request = new Request(query, function (err, rows) {\n if (err) return callback(err);\n // console.log('rows: ' + rows);\n callback(null, rows > 0);\n });\n\n bcrypt.hash(newPassword, 10, function (err, hash) {\n if (err) return callback(err);\n request.addParameter('NewPassword', TYPES.VarChar, hash);\n request.addParameter('Email', TYPES.VarChar, email);\n connection.execSql(request);\n });\n });\n}\n",
"create": "function create(user, callback) {\n //this example uses the \"tedious\" library\n //more info here: http://pekim.github.io/tedious/index.html\n const bcrypt = require('bcrypt');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'test',\n password: 'test',\n server: 'localhost',\n options: {\n database: 'mydb'\n }\n });\n\n const query = 'INSERT INTO dbo.Users SET Email = @Email, Password = @Password';\n\n connection.on('debug', function(text) {\n console.log(text);\n }).on('errorMessage', function(text) {\n console.log(JSON.stringify(text, null, 2));\n }).on('infoMessage', function(text) {\n console.log(JSON.stringify(text, null, 2));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n const request = new Request(query, function (err, rows) {\n if (err) return callback(err);\n // console.log('rows: ' + rows);\n callback(null);\n });\n\n bcrypt.hash(user.password, 10, function(err, hash) {\n if (err) return callback(err);\n request.addParameter('Email', TYPES.VarChar, user.email);\n request.addParameter('Password', TYPES.VarChar, hash);\n connection.execSql(request);\n });\n });\n}\n",
"delete": "function remove(id, callback) {\n // this example uses the \"tedious\" library\n // more info here: http://pekim.github.io/tedious/index.html\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'test',\n password: 'test',\n server: 'localhost',\n options: {\n database: 'mydb'\n }\n });\n\n const query = 'DELETE FROM dbo.Users WHERE id = @UserId';\n\n connection.on('debug', function (text) {\n console.log(text);\n }).on('errorMessage', function (text) {\n console.log(JSON.stringify(text, null, 2));\n }).on('infoMessage', function (text) {\n console.log(JSON.stringify(text, null, 2));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n const request = new Request(query, function (err) {\n if (err) return callback(err);\n callback(null);\n });\n\n request.addParameter('UserId', TYPES.VarChar, id);\n\n connection.execSql(request);\n });\n}\n",
"get_user": "function getByEmail(email, callback) {\n //this example uses the \"tedious\" library\n //more info here: http://pekim.github.io/tedious/index.html\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'test',\n password: 'test',\n server: 'localhost',\n options: {\n database: 'mydb'\n }\n });\n\n const query = 'SELECT Id, Nickname, Email FROM dbo.Users WHERE Email = @Email';\n\n connection.on('debug', function (text) {\n console.log(text);\n }).on('errorMessage', function (text) {\n console.log(JSON.stringify(text, null, 2));\n }).on('infoMessage', function (text) {\n console.log(JSON.stringify(text, null, 2));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n const request = new Request(query, function (err, rowCount, rows) {\n if (err) return callback(err);\n\n callback(null, {\n user_id: rows[0][0].value,\n nickname: rows[0][1].value,\n email: rows[0][2].value\n });\n });\n\n request.addParameter('Email', TYPES.VarChar, email);\n connection.execSql(request);\n });\n}\n",
"login": "function login(email, password, callback) {\n //this example uses the \"tedious\" library\n //more info here: http://pekim.github.io/tedious/index.html\n const bcrypt = require('bcrypt');\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'test',\n password: 'test',\n server: 'localhost',\n options: {\n database: 'mydb'\n }\n });\n\n const query = 'SELECT Id, Nickname, Email, Password FROM dbo.Users WHERE Email = @Email';\n\n connection.on('debug', function (text) {\n console.log(text);\n }).on('errorMessage', function (text) {\n console.log(JSON.stringify(text, null, 2));\n }).on('infoMessage', function (text) {\n console.log(JSON.stringify(text, null, 2));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n const request = new Request(query, function (err, rowCount, rows) {\n if (err || rowCount < 1) return callback(err || new WrongUsernameOrPasswordError(email));\n\n bcrypt.compare(password, rows[0][3].value, function (err, isValid) {\n if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));\n\n callback(null, {\n user_id: rows[0][0].value,\n nickname: rows[0][1].value,\n email: rows[0][2].value\n });\n });\n });\n\n request.addParameter('Email', TYPES.VarChar, email);\n connection.execSql(request);\n });\n}\n",
"verify": "function verify (email, callback) {\n //this example uses the \"tedious\" library\n //more info here: http://pekim.github.io/tedious/index.html\n const sqlserver = require('[email protected]');\n\n const Connection = sqlserver.Connection;\n const Request = sqlserver.Request;\n const TYPES = sqlserver.TYPES;\n\n const connection = new Connection({\n userName: 'test',\n password: 'test',\n server: 'localhost',\n options: {\n database: 'mydb'\n }\n });\n\n const query = 'UPDATE dbo.Users SET Email_Verified = true WHERE Email_Verified = false AND Email = @Email';\n\n connection.on('debug', function(text) {\n console.log(text);\n }).on('errorMessage', function(text) {\n console.log(JSON.stringify(text, null, 2));\n }).on('infoMessage', function(text) {\n console.log(JSON.stringify(text, null, 2));\n });\n\n connection.on('connect', function (err) {\n if (err) return callback(err);\n\n const request = new Request(query, function (err, rows) {\n if (err) return callback(err);\n // console.log('rows: ' + rows);\n callback(null, rows > 0);\n });\n\n request.addParameter('Email', TYPES.VarChar, email);\n\n connection.execSql(request);\n });\n}\n"
}
}