Skip to content

Instantly share code, notes, and snippets.

@buroz
Last active September 27, 2019 13:50
Show Gist options
  • Save buroz/2caf957a076913390b69ab63943f122e to your computer and use it in GitHub Desktop.
Save buroz/2caf957a076913390b69ab63943f122e to your computer and use it in GitHub Desktop.
CSRF checker on memory with Typescript
import crypto from "crypto";
class CSRF {
private store: string[] = [];
constructor() {}
createToken() {
const token = crypto.randomBytes(64).toString("hex");
this.store.push(token);
return token;
}
checkToken(token: string): Promise<boolean> {
return new Promise((resolve, reject) => {
let i = this.store.indexOf(token);
i > -1
? (() => {
delete this.store[i];
resolve(true);
})()
: reject(false);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment