Implement MultiToken
All checks were successful
Neutral/pipeline/head This commit looks good

This commit is contained in:
CICD - Pipeline 2023-04-01 16:48:13 +02:00
parent f056db13d8
commit 4243d6e651
4 changed files with 66 additions and 26 deletions

View File

@ -31,10 +31,7 @@ module.exports.createUser = (name, password, level, fullname) => {
"uuid": userUUID,
"fullname": fullname,
"permissionLevel":level,
"token":{
},
"tokens":{},
"lastconnexion":0
}
@ -111,13 +108,13 @@ module.exports.checkToken = (req, res) => {
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + userData.uuid + ".json", JSON.stringify(userData, null, 2))
if(userData.token.livableToken == true) {
if(userData.tokens[req.cookies.tokenID].livableToken == true) {
const checkTokenData = {"name":user, "permLevel": userData.permissionLevel, "fullname":userData.fullname, "lastconnexion":userData.lastconnexion};
return checkTokenData;
} else {
const tokenDate = new Date(userData.token.createdAt)
const tokenDate = new Date(userData.tokens[req.cookies.tokenID].createdAt)
const nowDate = new Date(Date.now())
if(tokenDate.getDay() == nowDate.getDay() && tokenDate.getMonth() == nowDate.getMonth()) {
@ -126,7 +123,10 @@ module.exports.checkToken = (req, res) => {
return checkTokenData;
} else {
this.clearSpecificCookie(req.cookies.tokenID);
res.clearCookie('tokenID');
const checkTokenData = {"name":false}
return checkTokenData;
@ -143,6 +143,43 @@ module.exports.checkToken = (req, res) => {
}
module.exports.clearSpecificCookie = (token) => {
const tokens = this.getAllToken()
const user = tokens.get(token)
const users = this.getUsers()
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + users.get(user) + ".json", 'utf-8'))
userData["tokens"].token = null;
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + userData.uuid + ".json", JSON.stringify(userData, null, 2))
}
module.exports.clearCookies = (token) => {
const tokens = this.getAllToken()
const user = tokens.get(token)
const users = this.getUsers()
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + users.get(user) + ".json", 'utf-8'))
userData["tokens"] = {};
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + userData.uuid + ".json", JSON.stringify(userData, null, 2))
}
module.exports.getFullName = (username) => {
@ -166,18 +203,12 @@ module.exports.generateTokenID = (username, userData, req, users) => {
livable = true;
}
Object.defineProperties(newUserData, {
token: {
value: {
"tokenID":tokenID,
"livableToken": livable,
"createdAt": date
},
writable: true
}
})
newUserData.tokens[tokenID] = {
"tokenID":tokenID,
"livableToken": livable,
"createdAt": date
}
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + users.get(username) + ".json", JSON.stringify(newUserData, null, 2))
@ -204,7 +235,13 @@ module.exports.getAllToken = () => {
fs.readdirSync(__dirname + path.sep + "users").forEach(file => {
const fileData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + file, 'utf-8'))
token.set(fileData.token.tokenID, fileData.username)
for(var tokenIDs of Object.keys(fileData.tokens)) {
token.set(fileData.tokens[tokenIDs].tokenID, fileData.username)
console.log(token)
}
})

View File

@ -19,6 +19,7 @@ router.get('/', function(req, res, next) {
} else {
ntr.clearCookies(req.cookies.tokenID)
res.clearCookie('tokenID')
res.redirect(302, "/login")

View File

@ -4,10 +4,12 @@
"uuid": "0cee5da6-6812-49d8-97f5-2df5c1d78191",
"fullname": "Raphael PICOT",
"permissionLevel": 4,
"token": {
"tokenID": "571ecb2b-726f-442f-9ec7-bc19205847b7",
"livableToken": true,
"createdAt": 1679260759114
"tokens": {
"52878cd4-0d6e-4a80-8419-c7154e48f97b": {
"tokenID": "52878cd4-0d6e-4a80-8419-c7154e48f97b",
"livableToken": true,
"createdAt": 1680360332205
}
},
"lastconnexion": 1680117724684
"lastconnexion": 1680360434117
}

View File

@ -4,6 +4,6 @@
"uuid": "c8f353a4-d0df-4c10-bccc-d4f47daf0f51",
"fullname": "Administrateur",
"permissionLevel": 4,
"token": {},
"tokens": {},
"lastconnexion": 1677339336836
}