-
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.
- Loading branch information
1 parent
575b449
commit ed21021
Showing
3 changed files
with
84 additions
and
5 deletions.
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 |
---|---|---|
@@ -1,2 +1,80 @@ | ||
# type-graphql-csrf-middleware | ||
TypeGraphQL middleware for handling csrf tokens | ||
|
||
TypeGraphQL middleware for handling csrf tokens with an express server with express-sessions. | ||
|
||
Required Peer Dependencies: | ||
|
||
- express | ||
- express-session | ||
- crsf | ||
- cookie-parser | ||
- graphql | ||
- type-graphql | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install type-graphql-csrf-middleware | ||
|
||
yarn add type-graphql-csrf-middleware | ||
``` | ||
|
||
```bash | ||
import { ValidAntiForgeryToken } from "type-graphql-csrf-middleware"; | ||
``` | ||
## Getting Started | ||
Your express server will need to add a csrf token as a cookie and a csrf secret to the session. | ||
Below is an example express route middleware to add the tokens. | ||
```javascript | ||
const addCsrf = (req: Request, res: Response, next: NextFunction) => { | ||
const tokens = new Tokens(); | ||
const secret = tokens.secretSync(); | ||
const token = tokens.create(secret); | ||
|
||
res.cookie("csrfToken", token); | ||
req.session.csrfSecret = secret; | ||
|
||
next(); | ||
}; | ||
``` | ||
## Resolver Middleware Use | ||
The type-graphql middleware needs a cookie key and a session key in order to verify the token. | ||
```javascript | ||
import { ValidAntiForgeryToken } from "type-graphql-csrf-middleware"; | ||
|
||
@Resolver(User) | ||
export class UserResolver { | ||
@Query(() => User) | ||
@UseMiddleware(ValidAntiForgeryToken({ cookieKey: "csrfToken", secretKey: "csrfSecret" })) | ||
async me(@Ctx() ctx: MyContext): Promise<User | undefined> { | ||
{...} | ||
} | ||
} | ||
``` | ||
The middleware can also be reusable between resolvers and resolver functions. | ||
```javascript | ||
import { ValidAntiForgeryToken } from "type-graphql-csrf-middleware"; | ||
|
||
const Authorized = ValidAntiForgeryToken({ | ||
cookieKey: "csrfToken", | ||
secretKey: "csrfSecret", | ||
message: "Access Denied!" | ||
}); | ||
|
||
@Resolver(User) | ||
export class UserResolver { | ||
@Query(() => User) | ||
@UseMiddleware(Authorized) | ||
async me(@Ctx() ctx: MyContext): Promise<User | undefined> { | ||
{...} | ||
} | ||
} | ||
``` |
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