diff options
author | Chocobozzz <me@florianbigard.com> | 2020-04-23 11:36:50 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-05-04 16:21:39 +0200 |
commit | e1c5503114deef954731904695cd40dccfcef555 (patch) | |
tree | 72cec4ee691a3362a7d024dc830d215a6b2c800a /server/controllers/api/users/token.ts | |
parent | 8dc8a34ee8428e7657414115d1c137592efa174d (diff) | |
download | PeerTube-e1c5503114deef954731904695cd40dccfcef555.tar.gz PeerTube-e1c5503114deef954731904695cd40dccfcef555.tar.zst PeerTube-e1c5503114deef954731904695cd40dccfcef555.zip |
Support logout and add id and pass tests
Diffstat (limited to 'server/controllers/api/users/token.ts')
-rw-r--r-- | server/controllers/api/users/token.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/server/controllers/api/users/token.ts b/server/controllers/api/users/token.ts new file mode 100644 index 000000000..9694f9e5e --- /dev/null +++ b/server/controllers/api/users/token.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | import { handleIdAndPassLogin, handleTokenRevocation } from '@server/lib/auth' | ||
2 | import * as RateLimit from 'express-rate-limit' | ||
3 | import { CONFIG } from '@server/initializers/config' | ||
4 | import * as express from 'express' | ||
5 | import { Hooks } from '@server/lib/plugins/hooks' | ||
6 | import { asyncMiddleware, authenticate } from '@server/middlewares' | ||
7 | |||
8 | const tokensRouter = express.Router() | ||
9 | |||
10 | const loginRateLimiter = RateLimit({ | ||
11 | windowMs: CONFIG.RATES_LIMIT.LOGIN.WINDOW_MS, | ||
12 | max: CONFIG.RATES_LIMIT.LOGIN.MAX | ||
13 | }) | ||
14 | |||
15 | tokensRouter.post('/token', | ||
16 | loginRateLimiter, | ||
17 | handleIdAndPassLogin, | ||
18 | tokenSuccess | ||
19 | ) | ||
20 | |||
21 | tokensRouter.post('/revoke-token', | ||
22 | authenticate, | ||
23 | asyncMiddleware(handleTokenRevocation), | ||
24 | tokenSuccess | ||
25 | ) | ||
26 | |||
27 | // --------------------------------------------------------------------------- | ||
28 | |||
29 | export { | ||
30 | tokensRouter | ||
31 | } | ||
32 | // --------------------------------------------------------------------------- | ||
33 | |||
34 | function tokenSuccess (req: express.Request) { | ||
35 | const username = req.body.username | ||
36 | |||
37 | Hooks.runAction('action:api.user.oauth2-got-token', { username, ip: req.ip }) | ||
38 | } | ||