mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Make Auth modules constants configurable.
This commit is contained in:
parent
23d4749b12
commit
9dc4b348ce
|
@ -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
|
||||
|
|
|
@ -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<string> {
|
||||
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<string>} Signer address.
|
||||
*/
|
||||
public async check(token: string): Promise<string> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user