-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from blinkafrica/main
Release v0.7.0
- Loading branch information
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import crypto from "crypto"; | ||
|
||
import { Redis } from "ioredis"; | ||
import ms from "ms"; | ||
|
||
import { dateReviver } from "../strings"; | ||
import { AsyncNullable, TokenStore } from "./store"; | ||
|
||
export class RedisStore implements TokenStore { | ||
constructor(private secret: string, private redis: Redis) {} | ||
|
||
async commision<T = any>(key: string, val: T, time: string): Promise<string> { | ||
const token = crypto.createHmac("sha256", this.secret).update(key).digest("hex"); | ||
|
||
const content = JSON.stringify(val); | ||
await this.redis.set(token, content, "PX", ms(time)); | ||
|
||
return token; | ||
} | ||
|
||
async peek<T = any>(token: string): AsyncNullable<T> { | ||
const result = await this.redis.get(token); | ||
if (!result) { | ||
return null; | ||
} | ||
|
||
return JSON.parse(result, dateReviver); | ||
} | ||
|
||
async extend<T = any>(token: string, time: string): AsyncNullable<T> { | ||
const result = await this.redis.get(token); | ||
if (!result) { | ||
return null; | ||
} | ||
|
||
await this.redis.pexpire(token, ms(time)); | ||
|
||
return JSON.parse(result, dateReviver); | ||
} | ||
|
||
async reset<T = any>(key: string, newVal: T): Promise<void> { | ||
const token = crypto.createHmac("sha256", this.secret).update(key).digest("hex"); | ||
|
||
// make sure the token exists | ||
const result = await this.redis.get(token); | ||
if (!result) return; | ||
|
||
const content = JSON.stringify(newVal); | ||
const ttl = await this.redis.pttl(token); | ||
|
||
await this.redis.set(token, content, "PX", ttl); | ||
} | ||
|
||
async decommission<T = any>(token: string): AsyncNullable<T> { | ||
const result = await this.redis.get(token); | ||
if (!result) { | ||
return null; | ||
} | ||
|
||
await this.redis.del(token); | ||
|
||
return JSON.parse(result, dateReviver); | ||
} | ||
|
||
async revoke(key: string): Promise<void> { | ||
const token = crypto.createHmac("sha256", this.secret).update(key).digest("hex"); | ||
|
||
await this.redis.del(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export type AsyncNullable<T> = Promise<T | null>; | ||
|
||
/** | ||
* Contract of stores used to manage single use tokens. What's most important of | ||
* said tokens(vs JWT for instance) is the ability to revoke said token. | ||
*/ | ||
export interface TokenStore { | ||
/** | ||
* Create a single use token that expires after the given timeout | ||
* @param key key to enabled reset and revoke | ||
* @param val value the token will refer to | ||
* @param time timeout before expiry | ||
*/ | ||
commision<T = any>(key: string, val: T, time: string): Promise<string>; | ||
/** | ||
* Get the data the token references without changing its lifetime | ||
* @param token token to check for | ||
*/ | ||
peek<T = any>(token: string): AsyncNullable<T>; | ||
/** | ||
* Set the new duration before an existing token times out. Note that it doesn't | ||
* take into account how long the old token had to expire, as it uses the new duration | ||
* entirely. | ||
* @param token generated token | ||
* @param time the new expiry duration of the token | ||
*/ | ||
extend<T = any>(token: string, time: string): AsyncNullable<T>; | ||
/** | ||
* Change the contents of the token without changing it's TTL | ||
* @param key key used to generate the token | ||
* @param newVal value to replace token content | ||
*/ | ||
reset<T = any>(key: string, newVal: T): Promise<void>; | ||
/** | ||
* Load the value referenced by the token and dispenses of the token, | ||
* making it unvailable for further use. | ||
* @param token token to be decomissioned | ||
*/ | ||
decommission<T = any>(token: string): AsyncNullable<T>; | ||
/** | ||
* Render the token generated for the given key useless. | ||
* @param key key used to generate the token | ||
*/ | ||
revoke(key: string): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters