2020-06-29 21:14:34 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2020-06-29 21:14:34 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2020-06-29 21:14:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2023-03-08 23:30:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-06-29 21:14:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2022-10-11 22:44:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/server/limiter"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-06-29 21:14:34 +02:00
|
|
|
)
|
|
|
|
|
2022-10-17 19:07:38 +02:00
|
|
|
// UpdateUserPassword changes the password of the currently authenticated user.
|
2022-09-28 09:01:17 +02:00
|
|
|
//
|
2020-06-29 21:14:34 +02:00
|
|
|
// PUT /api/v1/users/:uid/password
|
2022-10-17 19:07:38 +02:00
|
|
|
func UpdateUserPassword(router *gin.RouterGroup) {
|
2020-06-29 21:14:34 +02:00
|
|
|
router.PUT("/users/:uid/password", func(c *gin.Context) {
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-06-30 08:50:44 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// You cannot change any passwords without authentication and settings enabled.
|
2020-12-18 13:05:48 +01:00
|
|
|
if conf.Public() || conf.DisableSettings() {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusForbidden, i18n.ErrPublic)
|
2020-06-30 08:50:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// Check limit for failed auth requests (max. 10 per minute).
|
2022-10-19 05:09:09 +02:00
|
|
|
if limiter.Login.Reject(ClientIP(c)) {
|
2022-10-11 22:44:11 +02:00
|
|
|
limiter.AbortJSON(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Get session.
|
|
|
|
s := Auth(c, acl.ResourcePassword, acl.ActionUpdate)
|
2020-06-29 21:14:34 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Check if the session user is has user management privileges.
|
|
|
|
isPrivileged := acl.Resources.AllowAll(acl.ResourceUsers, s.User().AclRole(), acl.Permissions{acl.AccessAll, acl.ActionManage})
|
|
|
|
isSuperAdmin := isPrivileged && s.User().IsSuperAdmin()
|
|
|
|
uid := clean.UID(c.Param("uid"))
|
|
|
|
|
|
|
|
var u *entity.User
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Users may only change their own password.
|
2023-03-08 23:30:39 +01:00
|
|
|
if !isPrivileged && s.User().UserUID != uid {
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortForbidden(c)
|
2021-08-12 20:19:46 +02:00
|
|
|
return
|
2023-03-08 23:30:39 +01:00
|
|
|
} else if s.User().UserUID == uid {
|
|
|
|
u = s.User()
|
|
|
|
isPrivileged = false
|
|
|
|
isSuperAdmin = false
|
|
|
|
} else if u = entity.FindUserByUID(uid); u == nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrUserNotFound)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f := form.ChangePassword{}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusBadRequest, err, i18n.ErrInvalidPassword)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Verify that the old password is correct.
|
2023-03-08 23:30:39 +01:00
|
|
|
if isSuperAdmin && f.OldPassword == "" {
|
|
|
|
// Do nothing.
|
|
|
|
} else if u.WrongPassword(f.OldPassword) {
|
2022-10-19 05:09:09 +02:00
|
|
|
limiter.Login.Reserve(ClientIP(c))
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrInvalidPassword)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-10 17:16:43 +01:00
|
|
|
// Set new password.
|
|
|
|
if err := u.SetPassword(f.NewPassword); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusBadRequest, err, i18n.ErrInvalidPassword)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-10 17:16:43 +01:00
|
|
|
// Update tokens if user matches with session.
|
|
|
|
if s.User().UserUID == u.UID() {
|
|
|
|
s.SetPreviewToken(u.PreviewToken)
|
|
|
|
s.SetDownloadToken(u.DownloadToken)
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Invalidate all other user sessions to protect the account:
|
|
|
|
// https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html
|
|
|
|
event.AuditInfo([]string{ClientIP(c), "session %s", "password changed", "invalidated %s"}, s.RefID,
|
2022-10-13 22:11:02 +02:00
|
|
|
english.Plural(u.DeleteSessions([]string{s.ID}), "session", "sessions"))
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
AddTokenHeaders(c, s)
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgPasswordChanged))
|
2020-06-29 21:14:34 +02:00
|
|
|
})
|
|
|
|
}
|