AccessTokens class for generating temporary tokens for retrieving images etc

This commit is contained in:
simojenki
2021-03-12 11:50:50 +11:00
parent 9e16dbd96e
commit f38e4cab88
4 changed files with 163 additions and 0 deletions

50
src/access_tokens.ts Normal file
View File

@@ -0,0 +1,50 @@
import dayjs, { Dayjs } from 'dayjs';
import { v4 as uuid } from 'uuid';
export interface Clock {
now(): Dayjs
}
type AccessToken = {
value: string;
authToken: string;
expiry: Dayjs;
};
export interface AccessTokens {
mint(authToken: string): string;
authTokenFor(value: string): string | undefined;
}
export class ExpiringAccessTokens implements AccessTokens {
tokens = new Map<string, AccessToken>();
clock: Clock;
constructor(clock : Clock = { now: () => dayjs() }) {
this.clock = clock
}
mint(authToken: string): string {
this.clearOutExpired();
const accessToken = {
value: uuid(),
authToken,
expiry: this.clock.now().add(12, 'hours')
};
this.tokens.set(accessToken.value, accessToken);
return accessToken.value;
}
authTokenFor(value: string): string | undefined {
this.clearOutExpired();
return this.tokens.get(value)?.authToken;
}
clearOutExpired() {
Array.from(this.tokens.values()).filter(it => it.expiry.isBefore(this.clock.now())).forEach(expired => {
this.tokens.delete(expired.value);
})
}
count = () => this.tokens.size;
}