-
Notifications
You must be signed in to change notification settings - Fork 88
/
password_change.go
85 lines (73 loc) · 2.52 KB
/
password_change.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package handlers
import (
"encoding/json"
"net/http"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/handlers/types"
"github.com/replicatedhq/kots/pkg/identity"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/password"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
)
// PasswordChangeRequest - request body for the password change endpoint
type PasswordChangeRequest struct {
CurrentPassword string `json:"current_password"`
NewPassword string `json:"new_password"`
}
// PasswordChangeResponse - response body for the password change endpoint
type PasswordChangeResponse struct {
Success bool `json:"success"`
}
// ChangePassword - change password for kots
func (h *Handler) ChangePassword(w http.ResponseWriter, r *http.Request) {
var passwordChangeRequest PasswordChangeRequest
if err := json.NewDecoder(r.Body).Decode(&passwordChangeRequest); err != nil {
logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err := password.ValidatePasswordInput(passwordChangeRequest.CurrentPassword, passwordChangeRequest.NewPassword); err != nil {
logger.Error(err)
JSON(w, http.StatusBadRequest, types.NewErrorResponse(err))
return
}
identityConfig, err := identity.GetConfig(r.Context(), util.PodNamespace)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if identityConfig.Spec.Enabled && identityConfig.Spec.DisablePasswordAuth {
err := errors.New("password authentication disabled")
JSON(w, http.StatusForbidden, types.NewErrorResponse(err))
return
}
clientset, err := k8sutil.GetClientset()
if err != nil {
logger.Error(err)
JSON(w, http.StatusInternalServerError, types.NewErrorResponse(err))
return
}
if err := password.ValidateCurrentPassword(store.GetStore(), passwordChangeRequest.CurrentPassword); err != nil {
logger.Error(err)
if errors.Is(err, password.ErrCurrentPasswordDoesNotMatch) {
JSON(w, http.StatusBadRequest, types.NewErrorResponse(err))
return
}
w.WriteHeader(http.StatusInternalServerError)
return
}
// change password
if err := password.ChangePassword(clientset, util.PodNamespace, passwordChangeRequest.NewPassword); err != nil {
logger.Error(err)
JSON(w, http.StatusInternalServerError, types.NewErrorResponse(err))
return
}
passwordChangeResponse := PasswordChangeResponse{
Success: true,
}
logger.Info("password changed successfully")
JSON(w, http.StatusOK, passwordChangeResponse)
}