-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsrf.js
53 lines (41 loc) · 837 Bytes
/
csrf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* 没搞明白
* csrf是网络攻击方式,可以用token方法对应
*/
const koa = require('koa')
const csrf = require('koa-csrf')
const bodyparser = require('koa-bodyparser')
const router = require('koa-router')
const app = module.exports = new Koa()
aoo.use(router())
app.use(bodyparser())
app.use(session());
app.use(csrf())
/**
* csrf need session
*/
app.keys = ['session key', 'csrf example'];
/**
* maybe a bodyparser
*/
app.use(async(ctx, next) => {
if (ctx.is('application/json')) {
ctx.body = ctx.request.body;
}
await next();
});
/**
* csrf middleware
*/
/**
* route
*/
router.get('/token', token);
router.post('/post', post);
async function token(ctx) {
ctx.body = ctx.csrf;
}
async function post(ctx) {
ctx.body = { ok: true };
}
if (!module.parent) app.listen(3000);