-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
668 additions
and
50 deletions.
There are no files selected for viewing
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
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,139 @@ | ||
const { randomId } = require("@/utils/stringUtils"); | ||
const Redis = require("ioredis"); | ||
|
||
export async function createApikey(address){ | ||
|
||
let client = new Redis(process.env.REDIS_CONNECTION); | ||
|
||
let promise = new Promise((res) => { | ||
|
||
let newKey = "CS"+randomId(38); | ||
// let newKey="CONVO"; | ||
let dt = new Date(); | ||
let date = String(dt.getMonth()+1).padStart('2','0') + String(dt.getFullYear()).slice(2); | ||
|
||
client.get(`${address}-keys`).then(prev=>{ | ||
|
||
prev = JSON.parse(prev); | ||
if (prev === null){ | ||
let newData = { | ||
"activeKey":newKey, | ||
"pastKeys":[], | ||
"whitelist":[] | ||
}; | ||
// No keys made yet. | ||
client | ||
.multi() | ||
.set(`${address}-keys`, JSON.stringify(newData)) | ||
.set(newKey,true) | ||
.set(`${newKey}-usage-${date}`, 0) | ||
.exec((err)=>{ | ||
if (err) { | ||
return res(err); | ||
} | ||
else { | ||
return res(newData); | ||
} | ||
}); | ||
} | ||
else { | ||
if ( prev['pastKeys'].length >= 5){ | ||
return res({ | ||
success: false, | ||
error: "Limit of 10 Keys/Account exceeded. Please contact [email protected] to increase limit." | ||
}); | ||
} | ||
else{ | ||
// Regenerate key. | ||
let newPastkeys = prev['pastKeys'].concat([prev['activeKey']]) | ||
let newKey = "CS"+randomId(38); | ||
let newData = { | ||
"activeKey":newKey, | ||
"pastKeys":newPastkeys, | ||
"whitelist":[] | ||
}; | ||
client | ||
.multi() | ||
.set(`${address}-keys`, JSON.stringify(newData)) | ||
.del(prev['activeKey']) | ||
.set(newKey, true) | ||
.set(`${newKey}-usage-${date}`, 0) | ||
.exec((err, resp)=>{ | ||
console.log(resp); | ||
if (err) { | ||
return res(err); | ||
} | ||
else { | ||
return res(newData); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
}); | ||
let result = await promise; | ||
return result; | ||
|
||
} | ||
|
||
export async function getApikeyData(address){ | ||
|
||
let client = new Redis(process.env.REDIS_CONNECTION); | ||
let promise = new Promise((res) => { | ||
|
||
client.get(`${address}-keys`).then(data=>{ | ||
if (data === null){ | ||
res({ | ||
'activeKey':null | ||
}) | ||
} | ||
else { | ||
let jsonData = JSON.parse(data); | ||
let recObj = client.multi().keys(`${jsonData['activeKey']}-usage-*`); | ||
for (let index = 0; index < jsonData['pastKeys'].length; index++) { | ||
recObj = recObj.keys(`${jsonData['pastKeys'][index]}-usage-*`) | ||
} | ||
recObj.exec((err, results) => { | ||
if (err){ | ||
return res(err) | ||
} | ||
else { | ||
|
||
let qk = []; | ||
|
||
let recObj2 = client.multi(); | ||
for (let i = 0; i < results.length; i++) { | ||
for (let j = 0; j < results[i][1].length; j++) { | ||
qk.push(results[i][1][j]); | ||
recObj2 = recObj2.get(results[i][1][j]); | ||
} | ||
} | ||
recObj2.exec((err2, results2) => { | ||
if (err){ | ||
return res(err2); | ||
} | ||
else { | ||
let resDb = {} | ||
for (let i = 0; i < results2.length; i++) { | ||
resDb[qk[i]] = results2[i][1]; | ||
} | ||
return res({ | ||
...jsonData, | ||
data:resDb | ||
}); | ||
} | ||
|
||
}); | ||
|
||
} | ||
}) | ||
} | ||
}); | ||
}); | ||
|
||
let result = await promise; | ||
client.quit(); | ||
return result; | ||
|
||
} |
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
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,59 @@ | ||
import validateAuth from "@/lib/validateAuth"; | ||
import { createApikey, getApikeyData } from "@/lib/apikeys"; | ||
|
||
const handler = async(req, res) => { | ||
|
||
try { | ||
|
||
if (req.method === "GET") { | ||
if (validateAuth(req.query?.token, req.query?.signerAddress) === true) { | ||
|
||
// return all apikey data. | ||
let resp = await getApikeyData(req.query.signerAddress); | ||
return res.status(200).json({ | ||
success: true, | ||
...resp | ||
}); | ||
|
||
} | ||
else { | ||
return res.status(401).json({ | ||
success: false, | ||
'error':'Invalid Auth' | ||
}); | ||
} | ||
|
||
} | ||
else if (req.method === "POST" ) { | ||
|
||
if (validateAuth(req.body.token, req.body.signerAddress) === true) { | ||
|
||
// create/regenerate api key. | ||
let data = await createApikey(req.body.signerAddress); | ||
return res.status(200).json({ | ||
success: true, | ||
...data | ||
}); | ||
|
||
} | ||
else { | ||
return res.status(401).json({ | ||
success: false, | ||
'error':'Invalid Auth' | ||
}); | ||
} | ||
|
||
} | ||
else { | ||
return res.status(404).json({ | ||
success: false, | ||
'error':'Invalid request method.' | ||
}); | ||
} | ||
|
||
} catch (error) { | ||
return res.status(500).json({ success: false,'error':error.toString() }); | ||
} | ||
} | ||
|
||
export default handler; |
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.
a1e15a5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: