diff options
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/users/token.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/server/controllers/api/users/token.ts b/server/controllers/api/users/token.ts index 41aa26769..821429358 100644 --- a/server/controllers/api/users/token.ts +++ b/server/controllers/api/users/token.ts | |||
@@ -4,6 +4,8 @@ import { CONFIG } from '@server/initializers/config' | |||
4 | import * as express from 'express' | 4 | import * as express from 'express' |
5 | import { Hooks } from '@server/lib/plugins/hooks' | 5 | import { Hooks } from '@server/lib/plugins/hooks' |
6 | import { asyncMiddleware, authenticate } from '@server/middlewares' | 6 | import { asyncMiddleware, authenticate } from '@server/middlewares' |
7 | import { ScopedToken } from '@shared/models/users/user-scoped-token' | ||
8 | import { v4 as uuidv4 } from 'uuid' | ||
7 | 9 | ||
8 | const tokensRouter = express.Router() | 10 | const tokensRouter = express.Router() |
9 | 11 | ||
@@ -23,6 +25,16 @@ tokensRouter.post('/revoke-token', | |||
23 | asyncMiddleware(handleTokenRevocation) | 25 | asyncMiddleware(handleTokenRevocation) |
24 | ) | 26 | ) |
25 | 27 | ||
28 | tokensRouter.get('/scoped-tokens', | ||
29 | authenticate, | ||
30 | getScopedTokens | ||
31 | ) | ||
32 | |||
33 | tokensRouter.post('/scoped-tokens', | ||
34 | authenticate, | ||
35 | asyncMiddleware(renewScopedTokens) | ||
36 | ) | ||
37 | |||
26 | // --------------------------------------------------------------------------- | 38 | // --------------------------------------------------------------------------- |
27 | 39 | ||
28 | export { | 40 | export { |
@@ -35,3 +47,22 @@ function tokenSuccess (req: express.Request) { | |||
35 | 47 | ||
36 | Hooks.runAction('action:api.user.oauth2-got-token', { username, ip: req.ip }) | 48 | Hooks.runAction('action:api.user.oauth2-got-token', { username, ip: req.ip }) |
37 | } | 49 | } |
50 | |||
51 | function getScopedTokens (req: express.Request, res: express.Response) { | ||
52 | const user = res.locals.oauth.token.user | ||
53 | |||
54 | return res.json({ | ||
55 | feedToken: user.feedToken | ||
56 | } as ScopedToken) | ||
57 | } | ||
58 | |||
59 | async function renewScopedTokens (req: express.Request, res: express.Response) { | ||
60 | const user = res.locals.oauth.token.user | ||
61 | |||
62 | user.feedToken = uuidv4() | ||
63 | await user.save() | ||
64 | |||
65 | return res.json({ | ||
66 | feedToken: user.feedToken | ||
67 | } as ScopedToken) | ||
68 | } | ||