Skip to content

Commit

Permalink
feat(token): generate and verify token without expiration time
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jan 8, 2023
1 parent 1a77cd1 commit 889eff8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 8 additions & 3 deletions core/token/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ globalAlwatr.registeredList.push({

export class AlwatrTokenGenerator {
protected _logger = createLogger('alwatr-token-generator');
private _duration: number;
private _duration: number | null;

get epoch(): number {
return Math.floor(Date.now() / this._duration);
return this._duration == null
? 0
: Math.floor(Date.now() / this._duration);
}

constructor(public config: TokenGeneratorConfig) {
this._logger.logMethodArgs('constructor', config);
this._duration = parseDuration(config.duration);
this._duration = config.duration == null ? null : parseDuration(config.duration);
}

protected _generate(data: string, epoch: number): string {
Expand All @@ -40,6 +42,9 @@ export class AlwatrTokenGenerator {
if (token === this._generate(data, epoch)) {
return 'valid';
}
else if (this._duration == null) {
return 'invalid';
}
else if (token === this._generate(data, epoch - 1)) {
return 'expired';
}
Expand Down
4 changes: 3 additions & 1 deletion core/token/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export type TokenGeneratorConfig = {

/**
* Token expiration time.
*
* `null` mean without expiration time
*/
duration: DurationString;
duration: DurationString | null;

/**
* OpenSSl digest algorithm.
Expand Down

0 comments on commit 889eff8

Please sign in to comment.