2021-01-17 21:29:08 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
2021-03-05 17:50:45 +00:00
|
|
|
"github.com/go-chi/jwtauth/v5"
|
2021-01-17 21:29:08 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
2021-11-13 12:25:43 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/smtp"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-01-17 21:29:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getAdmins(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-02-04 18:09:43 +00:00
|
|
|
limit, offset, order, err := getSearchFilters(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
admins, err := dataprovider.GetAdmins(limit, offset, order)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.JSON(w, r, admins)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAdminByUsername(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-01-17 21:29:08 +00:00
|
|
|
username := getURLParam(r, "username")
|
|
|
|
renderAdmin(w, r, username, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderAdmin(w http.ResponseWriter, r *http.Request, username string, status int) {
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin.HideConfidentialData()
|
|
|
|
if status != http.StatusOK {
|
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, http.StatusCreated)
|
|
|
|
render.JSON(w, r.WithContext(ctx), admin)
|
|
|
|
} else {
|
|
|
|
render.JSON(w, r, admin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addAdmin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
var admin dataprovider.Admin
|
2021-10-10 11:08:05 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &admin)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.AddAdmin(&admin, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
renderAdmin(w, r, admin.Username, http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func disableAdmin2FA(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(getURLParam(r, "username"))
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin.Filters.RecoveryCodes = nil
|
2022-01-06 09:11:47 +00:00
|
|
|
admin.Filters.TOTPConfig = dataprovider.AdminTOTPConfig{
|
2021-09-04 10:11:04 +00:00
|
|
|
Enabled: false,
|
|
|
|
}
|
2021-10-10 11:08:05 +00:00
|
|
|
if err := dataprovider.UpdateAdmin(&admin, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr)); err != nil {
|
2021-09-04 10:11:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, nil, "2FA disabled", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func updateAdmin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
username := getURLParam(r, "username")
|
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
adminID := admin.ID
|
2021-09-04 10:11:04 +00:00
|
|
|
totpConfig := admin.Filters.TOTPConfig
|
|
|
|
recoveryCodes := admin.Filters.RecoveryCodes
|
2022-01-06 09:11:47 +00:00
|
|
|
admin.Filters.TOTPConfig = dataprovider.AdminTOTPConfig{}
|
2021-09-05 12:10:12 +00:00
|
|
|
admin.Filters.RecoveryCodes = nil
|
2021-01-17 21:29:08 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &admin)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if username == claims.Username {
|
2021-08-17 16:08:32 +00:00
|
|
|
if claims.APIKeyID != "" {
|
|
|
|
sendAPIResponse(w, r, errors.New("updating the admin impersonated with an API key is not allowed"), "",
|
|
|
|
http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
if claims.isCriticalPermRemoved(admin.Permissions) {
|
2021-03-21 18:15:47 +00:00
|
|
|
sendAPIResponse(w, r, errors.New("you cannot remove these permissions to yourself"), "", http.StatusBadRequest)
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if admin.Status == 0 {
|
2021-03-21 18:15:47 +00:00
|
|
|
sendAPIResponse(w, r, errors.New("you cannot disable yourself"), "", http.StatusBadRequest)
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
admin.ID = adminID
|
|
|
|
admin.Username = username
|
2021-09-04 10:11:04 +00:00
|
|
|
admin.Filters.TOTPConfig = totpConfig
|
|
|
|
admin.Filters.RecoveryCodes = recoveryCodes
|
2021-10-10 11:08:05 +00:00
|
|
|
if err := dataprovider.UpdateAdmin(&admin, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr)); err != nil {
|
2021-01-17 21:29:08 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2021-01-18 12:24:38 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Admin updated", http.StatusOK)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteAdmin(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-01-17 21:29:08 +00:00
|
|
|
username := getURLParam(r, "username")
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if username == claims.Username {
|
2021-03-21 18:15:47 +00:00
|
|
|
sendAPIResponse(w, r, errors.New("you cannot delete yourself"), "", http.StatusBadRequest)
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.DeleteAdmin(username, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "Admin deleted", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
func getAdminProfile(w http.ResponseWriter, r *http.Request) {
|
2021-09-06 16:46:35 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
resp := adminProfile{
|
|
|
|
baseProfile: baseProfile{
|
|
|
|
Email: admin.Email,
|
|
|
|
Description: admin.Description,
|
|
|
|
AllowAPIKeyAuth: admin.Filters.AllowAPIKeyAuth,
|
|
|
|
},
|
2021-09-06 16:46:35 +00:00
|
|
|
}
|
|
|
|
render.JSON(w, r, resp)
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
func updateAdminProfile(w http.ResponseWriter, r *http.Request) {
|
2021-09-06 16:46:35 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
var req adminProfile
|
2021-09-06 16:46:35 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &req)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
admin.Email = req.Email
|
|
|
|
admin.Description = req.Description
|
2021-09-06 16:46:35 +00:00
|
|
|
admin.Filters.AllowAPIKeyAuth = req.AllowAPIKeyAuth
|
2021-10-10 11:08:05 +00:00
|
|
|
if err := dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr)); err != nil {
|
2021-09-06 16:46:35 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2021-09-29 16:46:15 +00:00
|
|
|
sendAPIResponse(w, r, err, "Profile updated", http.StatusOK)
|
2021-09-06 16:46:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:25:43 +00:00
|
|
|
func forgotAdminPassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
if !smtp.IsEnabled() {
|
|
|
|
sendAPIResponse(w, r, nil, "No SMTP configuration", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := handleForgotPassword(r, getURLParam(r, "username"), true)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAPIResponse(w, r, err, "Check your email for the confirmation code", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetAdminPassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
|
|
|
var req pwdReset
|
|
|
|
err := render.DecodeJSON(r.Body, &req)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, _, err = handleResetPassword(r, req.Code, req.Password, true)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "Password reset successful", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func changeAdminPassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
var pwd pwdChange
|
2021-01-17 21:29:08 +00:00
|
|
|
err := render.DecodeJSON(r.Body, &pwd)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = doChangeAdminPassword(r, pwd.CurrentPassword, pwd.NewPassword, pwd.NewPassword)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "Password updated", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doChangeAdminPassword(r *http.Request, currentPassword, newPassword, confirmNewPassword string) error {
|
|
|
|
if currentPassword == "" || newPassword == "" || confirmNewPassword == "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("please provide the current password and the new one two times")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
if newPassword != confirmNewPassword {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("the two password fields do not match")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
if currentPassword == newPassword {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("the new password must be different from the current one")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
match, err := admin.CheckPassword(currentPassword)
|
|
|
|
if !match || err != nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("current password does not match")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
admin.Password = newPassword
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
return dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTokenClaims(r *http.Request) (jwtTokenClaims, error) {
|
|
|
|
tokenClaims := jwtTokenClaims{}
|
|
|
|
_, claims, err := jwtauth.FromContext(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
return tokenClaims, err
|
|
|
|
}
|
|
|
|
tokenClaims.Decode(claims)
|
|
|
|
|
|
|
|
return tokenClaims, nil
|
|
|
|
}
|