aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-30 13:27:07 +0100
committerChocobozzz <me@florianbigard.com>2018-01-30 13:27:07 +0100
commitecb4e35f4e6c7304cb274593c13cb47fd5078b75 (patch)
tree1e238002340bc521afde59d52f406e41298a7aac /server/controllers/api
parent80d1057bfcd3582af0dacf5ccd5a7a93ef95410b (diff)
downloadPeerTube-ecb4e35f4e6c7304cb274593c13cb47fd5078b75.tar.gz
PeerTube-ecb4e35f4e6c7304cb274593c13cb47fd5078b75.tar.zst
PeerTube-ecb4e35f4e6c7304cb274593c13cb47fd5078b75.zip
Add ability to reset our password
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/users.ts39
1 files changed, 37 insertions, 2 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts
index 79bb2665d..05639fbec 100644
--- a/server/controllers/api/users.ts
+++ b/server/controllers/api/users.ts
@@ -6,17 +6,23 @@ import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRat
6import { unlinkPromise } from '../../helpers/core-utils' 6import { unlinkPromise } from '../../helpers/core-utils'
7import { retryTransactionWrapper } from '../../helpers/database-utils' 7import { retryTransactionWrapper } from '../../helpers/database-utils'
8import { logger } from '../../helpers/logger' 8import { logger } from '../../helpers/logger'
9import { createReqFiles, getFormattedObjects } from '../../helpers/utils' 9import { createReqFiles, generateRandomString, getFormattedObjects } from '../../helpers/utils'
10import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' 10import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers'
11import { updateActorAvatarInstance } from '../../lib/activitypub' 11import { updateActorAvatarInstance } from '../../lib/activitypub'
12import { sendUpdateUser } from '../../lib/activitypub/send' 12import { sendUpdateUser } from '../../lib/activitypub/send'
13import { Emailer } from '../../lib/emailer'
14import { EmailPayload } from '../../lib/job-queue/handlers/email'
15import { Redis } from '../../lib/redis'
13import { createUserAccountAndChannel } from '../../lib/user' 16import { createUserAccountAndChannel } from '../../lib/user'
14import { 17import {
15 asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setDefaultSort, 18 asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setDefaultSort,
16 setDefaultPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator, 19 setDefaultPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
17 usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator 20 usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
18} from '../../middlewares' 21} from '../../middlewares'
19import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators' 22import {
23 usersAskResetPasswordValidator, usersResetPasswordValidator, usersUpdateMyAvatarValidator,
24 videosSortValidator
25} from '../../middlewares/validators'
20import { AccountVideoRateModel } from '../../models/account/account-video-rate' 26import { AccountVideoRateModel } from '../../models/account/account-video-rate'
21import { UserModel } from '../../models/account/user' 27import { UserModel } from '../../models/account/user'
22import { OAuthTokenModel } from '../../models/oauth/oauth-token' 28import { OAuthTokenModel } from '../../models/oauth/oauth-token'
@@ -106,6 +112,16 @@ usersRouter.delete('/:id',
106 asyncMiddleware(removeUser) 112 asyncMiddleware(removeUser)
107) 113)
108 114
115usersRouter.post('/ask-reset-password',
116 asyncMiddleware(usersAskResetPasswordValidator),
117 asyncMiddleware(askResetUserPassword)
118)
119
120usersRouter.post('/:id/reset-password',
121 asyncMiddleware(usersResetPasswordValidator),
122 asyncMiddleware(resetUserPassword)
123)
124
109usersRouter.post('/token', token, success) 125usersRouter.post('/token', token, success)
110// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route 126// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
111 127
@@ -307,6 +323,25 @@ async function updateUser (req: express.Request, res: express.Response, next: ex
307 return res.sendStatus(204) 323 return res.sendStatus(204)
308} 324}
309 325
326async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
327 const user = res.locals.user as UserModel
328
329 const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id)
330 const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString
331 await Emailer.Instance.addForgetPasswordEmailJob(user.email, url)
332
333 return res.status(204).end()
334}
335
336async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
337 const user = res.locals.user as UserModel
338 user.password = req.body.password
339
340 await user.save()
341
342 return res.status(204).end()
343}
344
310function success (req: express.Request, res: express.Response, next: express.NextFunction) { 345function success (req: express.Request, res: express.Response, next: express.NextFunction) {
311 res.end() 346 res.end()
312} 347}