Skip to content

Commit

Permalink
Merge pull request #5053 from sensu/change-password-feature
Browse files Browse the repository at this point in the history
condition change to support password reset
  • Loading branch information
chavakula authored Oct 22, 2024
2 parents 71af877 + a3e6388 commit d0cd112
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 2 additions & 3 deletions backend/apid/middlewares/authorization_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ func (a AuthorizationAttributes) Then(next http.Handler) http.Handler {
attrs.Resource = types.LocalSelfUserResource
}

// Change the resource to LocalSelfUserResource if a user tries to change
// its own password
if attrs.Verb == "update" && vars["subresource"] == "password" {
switch vars["subresource"] {
case "password", "change_password":
attrs.Resource = types.LocalSelfUserResource
}
}
Expand Down
32 changes: 32 additions & 0 deletions backend/apid/routers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (r *UsersRouter) Mount(parent *mux.Router) {
// Password change & reset
routes.Path("{id}/{subresource:password}", r.updatePassword).Methods(http.MethodPut)
routes.Path("{id}/{subresource:reset_password}", r.resetPassword).Methods(http.MethodPut)

// password update from web ui
routes.Path("{id}/{subresource:change_password}", r.changePasswordFromWeb).Methods(http.MethodPut)
}

func (r *UsersRouter) get(req *http.Request) (interface{}, error) {
Expand Down Expand Up @@ -153,6 +156,35 @@ func (r *UsersRouter) updatePassword(req *http.Request) (interface{}, error) {
return nil, err
}

// changePasswordFromWeb updates user password when requests are sent from web UI
func (r *UsersRouter) changePasswordFromWeb(req *http.Request) (interface{}, error) {
params := map[string]string{}
if err := UnmarshalBody(req, &params); err != nil {
return nil, err
}

vars := mux.Vars(req)
username, err := url.PathUnescape(vars["id"])
if err != nil {
return nil, err
}
newPassword := params["password_new"]
oldPassword := params["password"]

user, err := r.controller.AuthenticateUser(req.Context(), username, oldPassword)
if err != nil {
return nil, err
}

// set new password for updating into store
user.Password = newPassword

// Remove any old password hash
user.PasswordHash = ""
err = r.controller.CreateOrReplace(req.Context(), user)
return nil, err
}

// resetPassword updates a user password without any kind of verification
func (r *UsersRouter) resetPassword(req *http.Request) (interface{}, error) {
params := map[string]string{}
Expand Down
15 changes: 15 additions & 0 deletions backend/apid/routers/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ func TestUsersRouter(t *testing.T) {
},
wantStatusCode: http.StatusCreated,
},
{
name: "update password from web ui",
method: http.MethodPut,
path: path.Join(fixture.URIPath(), "change_password"),
body: []byte(`{"username":"foo","password":"admin123","password_new":"admin123"}`),
controllerFunc: func(c *mockUserController) {
c.On("AuthenticateUser", mock.Anything, mock.Anything, mock.Anything).
Return(&corev2.User{Username: "foo", Password: "admin123", PasswordHash: "admin123_hash"}, nil).
Once()
c.On("CreateOrReplace", mock.Anything, mock.Anything).
Return(nil).
Once()
},
wantStatusCode: http.StatusCreated,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit d0cd112

Please sign in to comment.