2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2022-08-31 07:29:39 +00:00
|
|
|
"errors"
|
2021-09-04 10:11:04 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/mfa"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2021-09-04 10:11:04 +00:00
|
|
|
)
|
|
|
|
|
2022-08-31 07:29:39 +00:00
|
|
|
var (
|
|
|
|
errRecoveryCodeForbidden = errors.New("recovery codes are not available with two-factor authentication disabled")
|
|
|
|
)
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
type generateTOTPRequest struct {
|
|
|
|
ConfigName string `json:"config_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type generateTOTPResponse struct {
|
|
|
|
ConfigName string `json:"config_name"`
|
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
QRCode []byte `json:"qr_code"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type validateTOTPRequest struct {
|
|
|
|
ConfigName string `json:"config_name"`
|
|
|
|
Passcode string `json:"passcode"`
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type recoveryCode struct {
|
|
|
|
Code string `json:"code"`
|
|
|
|
Used bool `json:"used"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTOTPConfigs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
render.JSON(w, r, mfa.GetAvailableTOTPConfigs())
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTOTPSecret(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
var accountName string
|
|
|
|
if claims.hasUserAudience() {
|
|
|
|
accountName = fmt.Sprintf("User %#v", claims.Username)
|
|
|
|
} else {
|
|
|
|
accountName = fmt.Sprintf("Admin %#v", claims.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
var req generateTOTPRequest
|
|
|
|
err = render.DecodeJSON(r.Body, &req)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configName, issuer, secret, qrCode, err := mfa.GenerateTOTPSecret(req.ConfigName, accountName)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.JSON(w, r, generateTOTPResponse{
|
|
|
|
ConfigName: configName,
|
|
|
|
Issuer: issuer,
|
|
|
|
Secret: secret,
|
|
|
|
QRCode: qrCode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveTOTPConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
2022-01-06 09:11:47 +00:00
|
|
|
recoveryCodes := make([]dataprovider.RecoveryCode, 0, 12)
|
2021-09-04 10:11:04 +00:00
|
|
|
for i := 0; i < 12; i++ {
|
|
|
|
code := getNewRecoveryCode()
|
2022-01-06 09:11:47 +00:00
|
|
|
recoveryCodes = append(recoveryCodes, dataprovider.RecoveryCode{Secret: kms.NewPlainSecret(code)})
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
if claims.hasUserAudience() {
|
|
|
|
if err := saveUserTOTPConfig(claims.Username, r, recoveryCodes); err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-03-06 15:57:13 +00:00
|
|
|
if claims.MustSetTwoFactorAuth {
|
|
|
|
// force logout
|
|
|
|
defer func() {
|
|
|
|
c := jwtTokenClaims{}
|
|
|
|
c.removeCookie(w, r, webBaseClientPath)
|
|
|
|
}()
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
} else {
|
|
|
|
if err := saveAdminTOTPConfig(claims.Username, r, recoveryCodes); err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAPIResponse(w, r, nil, "TOTP configuration saved", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateTOTPPasscode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
var req validateTOTPRequest
|
|
|
|
err := render.DecodeJSON(r.Body, &req)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
match, err := mfa.ValidateTOTPPasscode(req.ConfigName, req.Passcode, req.Secret)
|
|
|
|
if !match || err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid passcode", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, nil, "Passcode successfully validated", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRecoveryCodes(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
recoveryCodes := make([]recoveryCode, 0, 12)
|
2022-01-06 09:11:47 +00:00
|
|
|
var accountRecoveryCodes []dataprovider.RecoveryCode
|
2021-09-04 10:11:04 +00:00
|
|
|
if claims.hasUserAudience() {
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.UserExists(claims.Username, "")
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-08-31 07:29:39 +00:00
|
|
|
if !user.Filters.TOTPConfig.Enabled {
|
|
|
|
sendAPIResponse(w, r, errRecoveryCodeForbidden, "", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
accountRecoveryCodes = user.Filters.RecoveryCodes
|
|
|
|
} else {
|
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-08-31 07:29:39 +00:00
|
|
|
if !admin.Filters.TOTPConfig.Enabled {
|
|
|
|
sendAPIResponse(w, r, errRecoveryCodeForbidden, "", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
accountRecoveryCodes = admin.Filters.RecoveryCodes
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, code := range accountRecoveryCodes {
|
|
|
|
if err := code.Secret.Decrypt(); err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "Unable to decrypt recovery codes", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
recoveryCodes = append(recoveryCodes, recoveryCode{
|
|
|
|
Code: code.Secret.GetPayload(),
|
|
|
|
Used: code.Used,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
render.JSON(w, r, recoveryCodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRecoveryCodes(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
recoveryCodes := make([]string, 0, 12)
|
2022-01-06 09:11:47 +00:00
|
|
|
accountRecoveryCodes := make([]dataprovider.RecoveryCode, 0, 12)
|
2021-09-04 10:11:04 +00:00
|
|
|
for i := 0; i < 12; i++ {
|
|
|
|
code := getNewRecoveryCode()
|
|
|
|
recoveryCodes = append(recoveryCodes, code)
|
2022-01-06 09:11:47 +00:00
|
|
|
accountRecoveryCodes = append(accountRecoveryCodes, dataprovider.RecoveryCode{Secret: kms.NewPlainSecret(code)})
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
if claims.hasUserAudience() {
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.UserExists(claims.Username, "")
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-08-31 07:29:39 +00:00
|
|
|
if !user.Filters.TOTPConfig.Enabled {
|
|
|
|
sendAPIResponse(w, r, errRecoveryCodeForbidden, "", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
user.Filters.RecoveryCodes = accountRecoveryCodes
|
2022-12-03 10:45:27 +00:00
|
|
|
if err := dataprovider.UpdateUser(&user, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr), user.Role); err != nil {
|
2021-09-04 10:11:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2022-08-31 07:29:39 +00:00
|
|
|
if !admin.Filters.TOTPConfig.Enabled {
|
|
|
|
sendAPIResponse(w, r, errRecoveryCodeForbidden, "", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
admin.Filters.RecoveryCodes = accountRecoveryCodes
|
2022-12-03 10:45:27 +00:00
|
|
|
if err := dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr), admin.Role); err != nil {
|
2021-09-04 10:11:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, recoveryCodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNewRecoveryCode() string {
|
2021-11-06 13:13:20 +00:00
|
|
|
return fmt.Sprintf("RC-%v", strings.ToUpper(util.GenerateUniqueID()))
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
func saveUserTOTPConfig(username string, r *http.Request, recoveryCodes []dataprovider.RecoveryCode) error {
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.UserExists(username, "")
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
currentTOTPSecret := user.Filters.TOTPConfig.Secret
|
2021-09-05 12:10:12 +00:00
|
|
|
user.Filters.TOTPConfig.Secret = nil
|
2021-09-04 10:11:04 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &user.Filters.TOTPConfig)
|
|
|
|
if err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("unable to decode JSON body: %v", err))
|
|
|
|
}
|
2022-03-06 15:57:13 +00:00
|
|
|
if !user.Filters.TOTPConfig.Enabled && len(user.Filters.TwoFactorAuthProtocols) > 0 {
|
|
|
|
return util.NewValidationError("two-factor authentication must be enabled")
|
|
|
|
}
|
|
|
|
for _, p := range user.Filters.TwoFactorAuthProtocols {
|
2022-05-19 17:49:51 +00:00
|
|
|
if !util.Contains(user.Filters.TOTPConfig.Protocols, p) {
|
2022-03-06 15:57:13 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("totp: the following protocols are required: %#v",
|
|
|
|
strings.Join(user.Filters.TwoFactorAuthProtocols, ", ")))
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 12:10:12 +00:00
|
|
|
if user.Filters.TOTPConfig.Secret == nil || !user.Filters.TOTPConfig.Secret.IsPlain() {
|
2021-09-04 10:11:04 +00:00
|
|
|
user.Filters.TOTPConfig.Secret = currentTOTPSecret
|
|
|
|
}
|
2022-08-31 07:29:39 +00:00
|
|
|
if user.Filters.TOTPConfig.Enabled {
|
|
|
|
if user.CountUnusedRecoveryCodes() < 5 && user.Filters.TOTPConfig.Enabled {
|
|
|
|
user.Filters.RecoveryCodes = recoveryCodes
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Filters.RecoveryCodes = nil
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
return dataprovider.UpdateUser(&user, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr), user.Role)
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
func saveAdminTOTPConfig(username string, r *http.Request, recoveryCodes []dataprovider.RecoveryCode) error {
|
2021-09-04 10:11:04 +00:00
|
|
|
admin, err := dataprovider.AdminExists(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
currentTOTPSecret := admin.Filters.TOTPConfig.Secret
|
2021-09-05 12:10:12 +00:00
|
|
|
admin.Filters.TOTPConfig.Secret = nil
|
2021-09-04 10:11:04 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &admin.Filters.TOTPConfig)
|
|
|
|
if err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("unable to decode JSON body: %v", err))
|
|
|
|
}
|
2022-08-31 07:29:39 +00:00
|
|
|
if admin.Filters.TOTPConfig.Enabled {
|
|
|
|
if admin.CountUnusedRecoveryCodes() < 5 && admin.Filters.TOTPConfig.Enabled {
|
|
|
|
admin.Filters.RecoveryCodes = recoveryCodes
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
admin.Filters.RecoveryCodes = nil
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
2021-09-05 12:10:12 +00:00
|
|
|
if admin.Filters.TOTPConfig.Secret == nil || !admin.Filters.TOTPConfig.Secret.IsPlain() {
|
2021-09-04 10:11:04 +00:00
|
|
|
admin.Filters.TOTPConfig.Secret = currentTOTPSecret
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
return dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSelf, util.GetIPFromRemoteAddress(r.RemoteAddr), admin.Role)
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|