-
Notifications
You must be signed in to change notification settings - Fork 0
/
ephemeral_keys.js
135 lines (115 loc) · 4.07 KB
/
ephemeral_keys.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
DB = undefined;
const TYPES = [ "CREATE_USER", "DOWNLOAD_FILE" ]
String.prototype.toProperCase = function () {
string = this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
doNotCapitalize = ["the", "and", "as", "if", "at", "but", "by", "for", "from", "if", "in", "of", "once", "onto", "or", "over", "past", "so", "than", "that", "till", "to", "up", "upon", "with", "when", "yet"]
string = string.trim()
strArr = string.split(" ")
for (x in strArr) {
if (doNotCapitalize.indexOf(strArr[x].toLowerCase()) != -1 && x != 0 && x != strArr.length) {
strArr[x] = strArr[x].toLowerCase()
}
}
output = strArr.join(" ")
return output
}
function generateKey () {
var characters = 'ABCDEFGHKMNPRTUVWXYZabcdehkmpqsuvwxyz3456789';
result = ""
// Generate random string of characters
for ( var i = 0; i < 20; i++ ) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
// If the random string already exists, try again else return result
if (DB.prepare("SELECT * FROM ephemeral_keys WHERE key = ?").get(result)) {
return generateKey()
} else {
return result
}
}
function convertKeyInfo (row) {
return {
key: row["key"],
type: row["type"],
data: JSON.parse(row["json_data"]),
remainingUses: row["remaining_uses"],
creator: row["created_by"],
timestamp: row["created_at"]
}
}
/**
* Generates a random key, adds it to the database, and returns it
* @param {String} type What the key should be used for
* @param {Number} uses How many times the key should be allowed to be used
* @param {JSON} jsonData Data for the type of key
* @returns {String} key
*/
function createEphemeralKey ( type = "", uses = 0, jsonData = {}, user = "" ) {
// Convert type index to string value
if (typeof type == "number") {
if ([...Array(TYPES.length).keys()].indexOf(type) == -1) { return false }
else { type = TYPES[type] }
}
if (uses == 0 || TYPES.indexOf(type) == -1) {
return false;
}
newKey = generateKey()
DB.prepare("INSERT INTO ephemeral_keys (key, type, remaining_uses, json_data, created_by, created_at) VALUES (?, ?, ?, ?, ?, ?)").run(newKey, type, uses, JSON.stringify(jsonData), user, Date.now())
return newKey;
}
/**
* Checks if an ephemeral key is valid (found in DB)
* @param {*} key The key to search the database for
* @returns keyInfo if row is found or false if not
*/
function checkEphemeralKey ( key = "" ) {
row = DB.prepare("SELECT * FROM ephemeral_keys WHERE key = ?").get(key)
if (row) {
return convertKeyInfo(row)
} else {
return false
}
}
/**
* Decrements the remaining uses on an ephemeral key or deletes it if remaining uses = 0
* @param {String} key key to decrement
*/
function useEphemeralKey ( key = "" ) {
row = DB.prepare("SELECT * FROM ephemeral_keys WHERE key = ?").get(key)
if (row) {
// Decrement remaining uses
remainingUses = row["remaining_uses"] - 1
if (remainingUses == 0) {
DB.prepare("DELETE FROM ephemeral_keys WHERE key = ?").run(key)
} else {
DB.prepare("UPDATE ephemeral_keys SET remaining_uses = ? WHERE key = ?").run(remainingUses, key)
}
}
}
/**
* Hard removes a key directly from the DB
* @param {String} key key to delete
*/
function deleteEphemeralKey ( key = "" ) {
DB.prepare("DELETE FROM ephemeral_keys WHERE key = ?").run(key)
}
function getKeyList () {
rows = DB.prepare("SELECT * FROM ephemeral_keys").all()
outputRows = []
for (row in rows) {
outputRows[row] = convertKeyInfo(rows[row])
}
return outputRows
}
module.exports = function(database) {
DB = database
var module = {
deleteEphemeralKey,
createEphemeralKey,
checkEphemeralKey,
useEphemeralKey,
getKeyList,
TYPES
};
return module;
};