Commit ba7d7f21 authored by Lakr Aream's avatar Lakr Aream 👼🏽

完成签名逻辑

parent 7745ecf7
......@@ -159,7 +159,7 @@ TABLE `Candiay`.`users` (
`name` varchar(255) NOT NULL,
`pass` varchar(255) NULL,
`token` varchar(255) NULL,
`guard` date NULL,
`guard` int NULL,
`actived` int(1) NULL DEFAULT 1,
`info` json NULL,
PRIMARY KEY (`name`)
......
......@@ -161,6 +161,14 @@
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
},
"bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"requires": {
"file-uri-to-path": "1.0.0"
}
},
"body-parser": {
"version": "1.19.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
......@@ -265,6 +273,15 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"deasync": {
"version": "0.1.19",
"resolved": "https://registry.npmjs.org/deasync/-/deasync-0.1.19.tgz",
"integrity": "sha512-oh3MRktfnPlLysCPpBpKZZzb4cUC/p0aA3SyRGp15lN30juJBTo/CiD0d4fR+f1kBtUQoJj1NE9RPNWQ7BQ9Mg==",
"requires": {
"bindings": "^1.5.0",
"node-addon-api": "^1.7.1"
}
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
......@@ -358,6 +375,11 @@
"vary": "~1.1.2"
}
},
"file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw=="
},
"finalhandler": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
......@@ -554,6 +576,11 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"node-addon-api": {
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-1.7.1.tgz",
"integrity": "sha512-2+DuKodWvwRTrCfKOeR24KIc5unKjOh8mz17NCzVnHWfjAdDqbfbjqh7gUT+BkXBRQM52+xCHciKWonJ3CbJMQ=="
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
......
......@@ -5,6 +5,7 @@
"main": "dist/main.js",
"dependencies": {
"@types/yaml": "^1.2.0",
"deasync": "^0.1.19",
"express": "^4.17.1",
"mysql": "^2.18.1",
"yaml": "^1.8.3"
......
......@@ -26,9 +26,9 @@ coreApp.get("/users/c", (req, res) => {
res.status(502).send("Application in configuration")
return
}
RequestManager.shared.performSelectorUserManagementRequest(req, res, (code, context) => {
res.status(code).send(context)
})
const ret = RequestManager.shared.performSelectorUserManagementRequest(req, res)
// tslint:disable-next-line: no-string-literal
res.status(ret["code"]).send(ret["context"])
})
// Start servers
......
......@@ -84,7 +84,7 @@ export class DataBase {
cmd += " `name` varchar(255) NOT NULL," + "\n"
cmd += " `pass` varchar(255) NULL," + "\n"
cmd += " `token` varchar(255) NULL," + "\n"
cmd += " `guard` date NULL," + "\n"
cmd += " `guard` int NULL," + "\n"
cmd += " `actived` int(1) NULL DEFAULT 1," + "\n"
cmd += " `info` json NULL," + "\n"
cmd += " PRIMARY KEY (`name`)" + "\n"
......@@ -95,7 +95,7 @@ export class DataBase {
`name` varchar(255) NOT NULL,
`pass` varchar(255) NULL,
`token` varchar(255) NULL,
`guard` date NULL,
`guard` int NULL,
`actived` int(1) NULL DEFAULT 1,
`info` json NULL,
PRIMARY KEY (`name`)
......
......@@ -16,6 +16,11 @@ export class RequestManager {
callback(false)
}
// tslint:disable-next-line: no-string-literal
const clientTimeStamp = params["timestamp"]
if (clientTimeStamp === "" || clientTimeStamp === undefined || clientTimeStamp === null) {
callback(false)
}
let signString = ""
Object.keys(params).sort().forEach((key) => {
signString += key
......@@ -24,6 +29,10 @@ export class RequestManager {
signString += "&"
})
while (signString.endsWith("&")) {
signString = signString.substr(0, signString.length - 1)
}
// tslint:disable-next-line: no-string-literal
let fromUserSName = params["from"]
if (fromUserSName === "" || fromUserSName === undefined || fromUserSName === null) {
......@@ -34,18 +43,36 @@ export class RequestManager {
if (token === "" || token === null || token === undefined) {
callback(false)
} else {
const c = require('crypto')
const hmac = c.createHmac('sha1', token.toLowerCase());
const signed = hmac.update(Buffer.from(signString, 'utf-8')).digest("hex");
if (signed === clientSignResult) {
// Sign is ok, then check timestamp
const current = (new Date()).getTime()
const gap = current - clientTimeStamp
if (gap < 0 || gap > 60) {
callback(false)
} else {
callback(true)
}
} else {
callback(false)
}
}
})
}
public performSelectorUserManagementRequest(req: any, res: any, recall: (code: number, context: any) => void ) {
public performSelectorUserManagementRequest(req: any, res: any): { [key: string] : any } {
const headers: object = req.headers
const params: object = req.query
const from: string = req.host
const beginStamp = (new Date()).getTime()
let contextContaienr: { [key: string] : any } | undefined
this.vaildateRequest(headers, params, from, (vaildated) => {
console.log("-------------------REQUEST-------------------");
console.log((new Date()))
......@@ -58,15 +85,37 @@ export class RequestManager {
console.log("---------------------------------------------");
if (!vaildated) {
recall(401, "Signing Invailed!")
return
const resolveObject: { [key: string] : any } = {
"code": 401,
"context": "Invalid Signature"
}
contextContaienr = resolveObject
} else {
const resolveObject: { [key: string] : any } = {
"code": 888,
"context": "TEST"
}
contextContaienr = resolveObject
}
// Doing ~~Steave~~ Jobs
})
let currentStamp = (new Date()).getTime()
while (contextContaienr === undefined && currentStamp - beginStamp < 10) {
require('deasync').runLoopOnce()
currentStamp = (new Date()).getTime()
}
if (contextContaienr === undefined) {
return {
"code": 504,
"context": "Gateway Timeout"
}
} else {
return contextContaienr
}
}
......
......@@ -5,7 +5,7 @@ export interface User {
name: string
pass: string
token: string
guard: Date
guard: number
actived: boolean
info: { [key: string] : string }
}
......@@ -22,15 +22,17 @@ export class UserManager {
// reload from database
this.userListCacheLastUpdate = -10000
this.reloadUserListFromDataBaseIfNeeded((users) => {
this.userListCache = users
if (callback !== null) {
callback(users)
}
})
} else {
if (callback !== null) {
callback(this.userListCache)
}
this.reloadUserListFromDataBaseIfNeeded()
}
if (callback !== null) {
callback(this.userListCache)
}
this.reloadUserListFromDataBaseIfNeeded()
}
private async reloadUserListFromDataBaseIfNeeded(callback: ((users: User[]) => void) = null ) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment