Improve token validation.

This commit is contained in:
Pedro Gutiérrez 2019-05-16 15:42:49 +02:00
parent 3abe15a023
commit b32f708385
4 changed files with 15 additions and 16 deletions

View File

@ -28,7 +28,7 @@ export class OceanAccounts extends Instantiable {
const ethAccounts: string[] = await this.web3.eth.getAccounts()
const accountPromises = ethAccounts
.map(address => new Account(address, this.instanceConfig))
.map((address) => new Account(address, this.instanceConfig))
return Promise.all(accountPromises)
}

View File

@ -27,7 +27,7 @@ export class OceanAuth extends Instantiable {
* @return {Promise<string>} Token
*/
public async get(account: Account): Promise<string> {
const time = Date.now()
const time = Math.floor(Date.now() / 1000)
const message = `${this.getMessage()}\n${time}`
try {
@ -51,16 +51,16 @@ export class OceanAuth extends Instantiable {
*/
public async check(token: string): Promise<string> {
const expiration = this.getExpiration()
const [signature, timestamp] = token.split('-')
const [signature, timestamp] = token.split("-")
const message = `${this.getMessage()}\n${timestamp}`
if ((+timestamp + expiration) < Date.now()) {
if (((+timestamp * 1000) + expiration) < Date.now()) {
return `0x${"0".repeat(40)}`
}
return this.web3.utils.toChecksumAddress(
await this.ocean.utils.signature.verifyText(message, signature)
await this.ocean.utils.signature.verifyText(message, signature),
)
}

View File

@ -43,12 +43,12 @@ export class SubscribablePromise<T extends any, P extends any> {
Promise.resolve(execution as any)
.then((result) => {
if (Promise.resolve(execution as any) === execution) {
if (typeof (execution as any).then === "function") {
this.observer.complete(result)
}
})
.catch((result) => {
if (Promise.resolve(execution as any) === execution) {
if (typeof (execution as any).then === "function") {
this.observer.error(result)
}
})

View File

@ -55,7 +55,7 @@ describe("OceanAuth", () => {
describe("#store()", () => {
it("should sign and store the token", async () => {
const writeTokenSpy = spy.on(oceanAuth as any, 'writeToken', () => {})
const writeTokenSpy = spy.on(oceanAuth as any, "writeToken", () => {})
await oceanAuth.store(account)
@ -65,17 +65,17 @@ describe("OceanAuth", () => {
describe("#restore()", () => {
it("should return a stored token", async () => {
spy.on(oceanAuth as any, 'readToken', () => 'token')
spy.on(oceanAuth as any, 'check', () => account.getId())
spy.on(oceanAuth as any, "readToken", () => "token")
spy.on(oceanAuth as any, "check", () => account.getId())
const token = await oceanAuth.restore(account)
assert.equal(token, 'token')
assert.equal(token, "token")
})
it("should not return values if there is any error", async () => {
spy.on(oceanAuth as any, 'readToken', () => 'token')
spy.on(oceanAuth as any, 'check', () => '0x...')
spy.on(oceanAuth as any, "readToken", () => "token")
spy.on(oceanAuth as any, "check", () => "0x...")
const token = await oceanAuth.restore(account)
@ -85,7 +85,7 @@ describe("OceanAuth", () => {
describe("#isStored()", () => {
it("should know if the token is stored", async () => {
spy.on(oceanAuth as any, 'restore', () => account.getId())
spy.on(oceanAuth as any, "restore", () => account.getId())
const isStored = await oceanAuth.isStored(account)
@ -93,7 +93,7 @@ describe("OceanAuth", () => {
})
it("should know if the token is not stored", async () => {
spy.on(oceanAuth as any, 'restore', () => undefined)
spy.on(oceanAuth as any, "restore", () => undefined)
const isStored = await oceanAuth.isStored(account)
@ -101,4 +101,3 @@ describe("OceanAuth", () => {
})
})
})