github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/api_update_user_password.go (about)

     1  // Copyright 2024 Paul Greenberg greenpau@outlook.com
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package authn
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/http"
    21  	"strings"
    22  
    23  	"github.com/greenpau/go-authcrunch/pkg/authn/enums/operator"
    24  	"github.com/greenpau/go-authcrunch/pkg/ids"
    25  	"github.com/greenpau/go-authcrunch/pkg/requests"
    26  	"github.com/greenpau/go-authcrunch/pkg/user"
    27  )
    28  
    29  // UpdateUserPassword updates user password.
    30  func (p *Portal) UpdateUserPassword(
    31  	ctx context.Context,
    32  	w http.ResponseWriter,
    33  	r *http.Request,
    34  	rr *requests.Request,
    35  	parsedUser *user.User,
    36  	resp map[string]interface{},
    37  	usr *user.User,
    38  	backend ids.IdentityStore,
    39  	bodyData map[string]interface{}) error {
    40  
    41  	if v, exists := bodyData["old_password"]; exists {
    42  		switch exp := v.(type) {
    43  		case string:
    44  			rr.User.OldPassword = strings.TrimSpace(exp)
    45  		default:
    46  			resp["message"] = "Profile API did find key old_password in the request payload, but it is malformed"
    47  			return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
    48  		}
    49  	} else {
    50  		resp["message"] = "Profile API did not find key old_password in the request payload"
    51  		return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
    52  	}
    53  
    54  	if v, exists := bodyData["new_password"]; exists {
    55  		switch exp := v.(type) {
    56  		case string:
    57  			rr.User.Password = strings.TrimSpace(exp)
    58  		default:
    59  			resp["message"] = "Profile API did find key new_password in the request payload, but it is malformed"
    60  			return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
    61  		}
    62  	} else {
    63  		resp["message"] = "Profile API did not find key new_password in the request payload"
    64  		return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
    65  	}
    66  
    67  	if err := backend.Request(operator.ChangePassword, rr); err != nil {
    68  		var errMsg string = fmt.Sprintf("the Profile API failed to change user password in identity store: %v", err)
    69  		resp["message"] = errMsg
    70  		return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
    71  	}
    72  
    73  	resp["entry"] = "Updated"
    74  	return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
    75  }