-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from two-factor/zep/pgVerify
Zep/pg verify
- Loading branch information
Showing
21 changed files
with
547 additions
and
50 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
describe('', () => { | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const client = require('../../../../index')(process.env.SID, process.env.AUTH, { | ||
isPostgres: true, | ||
appName: 'testApp', | ||
connectionURI: 'postgres://postgres:yellowjacket@localhost/twoauthtests', | ||
}); | ||
|
||
// client.create("ian", "+17604207520"); | ||
|
||
client | ||
.create('ian', '+12016750593') | ||
.then(res => console.log(res)) | ||
.catch(err => console.log(err)); | ||
// describe("Tests for Postgres Send", () => { | ||
|
||
// }); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// const client = require("../../../../index")(process.env.SID, process.env.AUTH, { | ||
// isPostgres: true, | ||
// connectionURI: "postgres://student:ilovetesting@localhost/twoauthtests" | ||
// }); | ||
|
||
// // client.create("ian", "+17604207520"); | ||
|
||
// client | ||
// .send("ian") | ||
// .then(res => console.log(res)) | ||
// .catch(err => console.log(err)); | ||
// // describe("Tests for Postgres Send", () => { | ||
|
||
// // }); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
describe('', () => { | ||
|
||
} |
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
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
File renamed without changes.
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var parse = require("pg-connection-string").parse; | ||
const { Pool } = require("pg"); | ||
|
||
//function takes in a URI, returns a PG Pool | ||
|
||
function generatePool(connectionURI) { | ||
const { user, port, host, database, password } = parse(connectionURI); | ||
|
||
return new Pool({ | ||
user, | ||
host, | ||
database, | ||
password, | ||
port: port || 5432 | ||
}); | ||
} | ||
|
||
module.exports = generatePool; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint-disable func-names */ | ||
// eslint-disable-next-line func-names | ||
|
||
module.exports = function (userID, phone) { | ||
const { client, appName } = this; | ||
return new Promise((resolve, reject) => { | ||
this.pgConnect() | ||
.then(({ database, done }) => { | ||
if (typeof phone !== 'string') { | ||
reject(new Error('typeof phone must be string')); | ||
} | ||
if (phone.substring(0, 2) !== '+1') { | ||
reject(new Error('phone must be string formatted as such: +1XXXXXXXXXX')); | ||
} | ||
client.verify.services | ||
.create({ friendlyName: `${appName}` }) | ||
.then((service) => { | ||
const { sid } = service; | ||
database.query('INSERT INTO twoauthusers(userID, sid, phone) VALUES($1, $2, $3) RETURNING *', [userID, sid, phone]) | ||
.then((user) => { | ||
done(); | ||
resolve(user); | ||
}) | ||
.catch((err) => { | ||
done(); | ||
reject(err); | ||
}); | ||
}) | ||
.catch((err) => { | ||
done(); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = | ||
"CREATE TABLE IF NOT EXISTS twoauthusers (userID VARCHAR(255), sid VARCHAR(255), phone VARCHAR(255))"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module.exports = function(userID) { | ||
const { pgConnect, client } = this; | ||
return new Promise((resolve, reject) => { | ||
pgConnect() | ||
.then(({ database, done }) => { | ||
const query = "SELECT * FROM twoauthusers WHERE userID=$1"; | ||
const values = [String(userID)]; | ||
database.query(query, values, (err, res) => { | ||
if (err) reject(err); | ||
const { sid, phone } = res.rows[0]; | ||
if (!sid) | ||
reject(new Error("SID Error: No SID exists for this user.")); | ||
if (!phone) | ||
reject( | ||
new Error( | ||
"Phone Number Error: No phone number exists for this user." | ||
) | ||
); | ||
//invoke done before your resolve this promise | ||
client.verify | ||
.services(sid) | ||
.verifications.create({ | ||
to: phone, | ||
channel: "sms" | ||
}) | ||
.then(verification => { | ||
done(); | ||
resolve(verification); | ||
}) | ||
.catch(err => { | ||
done(); | ||
reject(err); | ||
}); | ||
}); | ||
}) | ||
.catch(err => { | ||
done(); | ||
reject(err); | ||
//"userID Error: This userID has not been created yet." | ||
}); | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module.exports = function(userID, code) { | ||
const { pgConnect } = this; | ||
return new Promise((resolve, reject) => { | ||
pgConnect() | ||
.then(({ database, done }) => { | ||
//Query to just check if the user was created | ||
const query = "SELECT * FROM twoauthusers WHERE userid=$1"; | ||
const values = [String(userID)]; | ||
|
||
database | ||
.query(query, values) | ||
.then(res => { | ||
const { sid, phone } = res.rows[0]; | ||
|
||
if (!sid) | ||
reject(new Error("SID Error: No SID exists for this user.")); | ||
if (!phone) | ||
reject( | ||
new Error( | ||
"Phone Number Error: No phone number exists for this user." | ||
) | ||
); | ||
|
||
return this.client.verify | ||
.services(sid) | ||
.verificationChecks.create({ | ||
to: phone, | ||
code | ||
}) | ||
.then(verification => { | ||
if (verification.status === "approved") resolve(true); | ||
resolve(false); | ||
}) | ||
.catch(err => { | ||
done(); | ||
resolve(false) | ||
}); | ||
}) | ||
.catch(err => { | ||
done(); | ||
reject(new Error("Could not find Database at Connection URI.")); | ||
}); | ||
}) | ||
.catch(err => { | ||
done(); | ||
reject(new Error("Could not find Database at Connection URI.")); | ||
}); | ||
}); | ||
//invoke done before you reject | ||
}; |
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
Oops, something went wrong.