Commit 2cf77737 authored by Lakr Aream's avatar Lakr Aream 👼🏽

完成完整签名逻辑

parent ba7d7f21
......@@ -51,7 +51,7 @@ database:
Any request with root ticket signed will be accept even from unauthorized ip address, changed it to 00000 to disable root sign after setup is a good way to reduce attack surface. You can create an admin account use root ticket thu it can take over all requests.
- TIMESTAMP:
Time stamp should be time intercal since 1970 to now.
Time stamp should be time intercal since 1970 to now, if you are using Date().getTime() then you need to / 1000
> var timestamp = Int(Date().timeIntervalSince1970)
### Signing Request With Ticket
......
......@@ -6,7 +6,7 @@ required:
# Application Config For Backend
app:
port: 3030 # listening at
root: "591F4FF9-D355-44E6-9817-1196F452BB30" # root signing ticket
root: "591f4ff9-d355-44e6-9817-1196f452bb30" # root signing ticket
# changed it to 00000 to disable root sign after setup
accept: # accept connection from
- 0.0.0.0
......
......@@ -17,12 +17,15 @@ export class RequestManager {
}
// tslint:disable-next-line: no-string-literal
const clientTimeStamp = params["timestamp"]
if (clientTimeStamp === "" || clientTimeStamp === undefined || clientTimeStamp === null) {
const clientTimeStamp:number = Number(params["timestamp"])
if (clientTimeStamp === 0 || clientTimeStamp === undefined || clientTimeStamp === null) {
callback(false)
}
let signString = ""
Object.keys(params).sort().forEach((key) => {
if (key.toLowerCase() === "timestamp") {
return
}
signString += key
signString += "="
signString += params[key]
......@@ -33,6 +36,9 @@ export class RequestManager {
signString = signString.substr(0, signString.length - 1)
}
signString += "&timestamp="
signString += clientTimeStamp
// tslint:disable-next-line: no-string-literal
let fromUserSName = params["from"]
if (fromUserSName === "" || fromUserSName === undefined || fromUserSName === null) {
......@@ -43,12 +49,10 @@ 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");
const signed = require('crypto').createHmac('sha1', token.toLowerCase()).update(Buffer.from(signString, 'utf-8')).digest("hex");
if (signed === clientSignResult) {
// Sign is ok, then check timestamp
const current = (new Date()).getTime()
const current = (new Date()).getTime() / 1000
const gap = current - clientTimeStamp
if (gap < 0 || gap > 60) {
callback(false)
......@@ -70,7 +74,7 @@ export class RequestManager {
const params: object = req.query
const from: string = req.host
const beginStamp = (new Date()).getTime()
const beginStamp = (new Date()).getTime() / 1000
let contextContaienr: { [key: string] : any } | undefined
this.vaildateRequest(headers, params, from, (vaildated) => {
......@@ -102,10 +106,10 @@ export class RequestManager {
})
let currentStamp = (new Date()).getTime()
let currentStamp = (new Date()).getTime() / 1000
while (contextContaienr === undefined && currentStamp - beginStamp < 10) {
require('deasync').runLoopOnce()
currentStamp = (new Date()).getTime()
currentStamp = (new Date()).getTime() / 1000
}
if (contextContaienr === undefined) {
......
......@@ -36,7 +36,7 @@ export class UserManager {
}
private async reloadUserListFromDataBaseIfNeeded(callback: ((users: User[]) => void) = null ) {
const gap = (new Date()).getTime() - this.userListCacheLastUpdate
const gap = (new Date()).getTime() / 1000 - this.userListCacheLastUpdate
if (gap > 60) {
DataBase.shared.readObjectsFromTable("users", (err: any, ret: any) => {
const obj = Object(ret)
......@@ -58,7 +58,7 @@ export class UserManager {
ulist.push(u)
}
this.userListCache = ulist
this.userListCacheLastUpdate = (new Date()).getTime()
this.userListCacheLastUpdate = (new Date()).getTime() / 1000
if (callback != null) {
callback(ulist)
}
......
import UIKit
var str = "Hello, playground"
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
......@@ -9,3 +9,4 @@
import Foundation
let url = "127.0.0.1"
print(Date().timeIntervalSince1970)
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