From 9dc4b348ce069d832c68d163cd28221c47e8bcea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Wed, 8 May 2019 17:26:23 +0200 Subject: [PATCH] Make Auth modules constants configurable. --- src/models/Config.ts | 12 ++++++++++++ src/ocean/OceanAuth.ts | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/models/Config.ts b/src/models/Config.ts index e7a2c35..d7d3f00 100644 --- a/src/models/Config.ts +++ b/src/models/Config.ts @@ -41,6 +41,18 @@ export class Config { * @type {boolean | LogLevel} */ public verbose?: boolean | LogLevel + + /** + * Message shown when the user creates its own token. + * @type {string} + */ + public authMessage?: string + + /** + * Token expiration time in ms. + * @type {number} + */ + public authTokenExpiration?: number } export default Config diff --git a/src/ocean/OceanAuth.ts b/src/ocean/OceanAuth.ts index 316cd3f..708680c 100644 --- a/src/ocean/OceanAuth.ts +++ b/src/ocean/OceanAuth.ts @@ -1,9 +1,8 @@ import Account from "./Account" import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" -// TODO: be able to read it from config const defaultAuthMessage = "Ocean Protocol Authentication" - +const defaultExpirationTime = 30 * 24 * 60 * 60 * 1000 // 30 days const localStorageKey = "SquidTokens" /** @@ -29,7 +28,7 @@ export class OceanAuth extends Instantiable { */ public async get(account: Account): Promise { const time = Date.now() - const message = `${defaultAuthMessage}\n${time}` + const message = `${this.getMessage()}\n${time}` try { const signature = await this.ocean.utils.signature @@ -51,10 +50,10 @@ export class OceanAuth extends Instantiable { * @return {Promise} Signer address. */ public async check(token: string): Promise { - const expiration = 30 * 24 * 60 * 60 * 1000 // 30 days + const expiration = this.getExpiration() const [signature, timestamp] = token.split('-') - const message = `${defaultAuthMessage}\n${timestamp}` + const message = `${this.getMessage()}\n${timestamp}` if ((+timestamp + expiration) < Date.now()) { return `0x${"0".repeat(40)}` @@ -131,4 +130,12 @@ export class OceanAuth extends Instantiable { } return localStorage } + + private getMessage() { + return this.config.authMessage || defaultAuthMessage + } + + private getExpiration() { + return this.config.authTokenExpiration || defaultExpirationTime + } }