@incodelang/accounts
Advanced tools
Comparing version 1.0.15 to 1.0.16
{ | ||
"name": "@incodelang/accounts", | ||
"version": "1.0.15", | ||
"version": "1.0.16", | ||
"description": "An API for simple Account Management", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -99,3 +99,3 @@ | ||
| `username` | `string` | The name of the user | | ||
| `password` | `string` | The password of the user | | ||
| `password` | `string` | The password of the user (or a token)| | ||
@@ -326,3 +326,15 @@ #### Check if a User exists | ||
### Tokens | ||
#### Create a Token | ||
```http | ||
POST /api/v1/user/tokens/create | ||
``` | ||
| Parameter | Type | Description | | ||
| :-------- | :------- | :-------------------------------- | | ||
| `username` | `string` | The name of the user | | ||
| `password` | `string` | The password of the user | | ||
## Authors | ||
@@ -329,0 +341,0 @@ |
@@ -312,2 +312,14 @@ /** | ||
} | ||
if(!options.disable.createToken) { | ||
options.app.post("/api/v1/user/tokens/create", (req, res) => { | ||
if (req.body.username && req.body.password) { | ||
res.end(JSON.stringify( | ||
users.createToken(req.body.username, req.body.password) | ||
)) | ||
}else { | ||
res.end("{\"error\": true, \"message\": \"Invalid Request body.\"}") | ||
} | ||
}) | ||
} | ||
} |
@@ -11,3 +11,2 @@ /** | ||
checkfile(path.join(process.env.ACC_PRIV_PATH, "postboxes.json"), "{}") | ||
@@ -14,0 +13,0 @@ |
@@ -25,2 +25,16 @@ /** | ||
checkfile(path.join(process.env.ACC_PRIV_PATH, "tokens.json"), "{}") | ||
let tokens = {} | ||
const reloadTokens = () => { | ||
tokens = JSON.parse(fs.readFileSync(path.join(process.env.ACC_PRIV_PATH, "tokens.json")).toString()) | ||
} | ||
const saveTokens = () => { | ||
fs.writeFileSync(path.join(process.env.ACC_PRIV_PATH, "tokens.json"), JSON.stringify(tokens)) | ||
} | ||
reloadTokens(); | ||
function existsUser(username) { | ||
@@ -79,3 +93,2 @@ return Object.keys(users).includes(username); | ||
function login(username, password) { | ||
password = crypto.createHash("sha256").update(password).digest("base64") | ||
if (!existsUser(username)) { | ||
@@ -87,2 +100,4 @@ return { | ||
} else { | ||
let origPassword = password; | ||
password = crypto.createHash("sha256").update(password).digest("base64") | ||
if (users[username].password === password) { | ||
@@ -94,5 +109,12 @@ return { | ||
} else { | ||
return { | ||
error: true, | ||
message: "The password does not match." | ||
if (!isTokenValid(username, origPassword).error) { | ||
return { | ||
error: false, | ||
message: "The token is valid" | ||
} | ||
} else { | ||
return { | ||
error: true, | ||
message: "The password does not match." | ||
} | ||
} | ||
@@ -203,2 +225,45 @@ } | ||
function createToken(username, password) { | ||
if (login(username, password)) { | ||
let t = generateToken(32); | ||
while(tokens[t]) { | ||
t = generateToken(32); | ||
} | ||
tokens[t] = username; | ||
saveTokens(); | ||
return { | ||
error: false, | ||
message: t | ||
}; | ||
} else { | ||
return login(username, password); | ||
} | ||
} | ||
function isTokenValid(username, token) { | ||
if(tokens[token] === username){ | ||
return { | ||
error: false, | ||
message: "Valid." | ||
} | ||
}else { | ||
return { | ||
error: true, | ||
message: "Not valid." | ||
} | ||
} | ||
} | ||
function generateToken(length) { | ||
let result = ''; | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
const charactersLength = characters.length; | ||
for (let i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * | ||
charactersLength)); | ||
} | ||
return result; | ||
} | ||
module.exports = { | ||
@@ -213,3 +278,5 @@ existsUser, | ||
getData, | ||
getAllData | ||
getAllData, | ||
createToken, | ||
isTokenValid | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79877
26
983
347
17