github.com/greenpau/go-authcrunch@v1.0.50/pkg/authn/handle_http_settings_password.go (about)

     1  // Copyright 2022 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  	"github.com/greenpau/go-authcrunch/pkg/authn/enums/operator"
    21  	"github.com/greenpau/go-authcrunch/pkg/ids"
    22  	"github.com/greenpau/go-authcrunch/pkg/requests"
    23  	"github.com/greenpau/go-authcrunch/pkg/user"
    24  	"net/http"
    25  	"strings"
    26  )
    27  
    28  func (p *Portal) handleHTTPPasswordSettings(
    29  	ctx context.Context, r *http.Request, rr *requests.Request,
    30  	usr *user.User, store ids.IdentityStore, data map[string]interface{},
    31  ) error {
    32  	var action string
    33  	var status bool
    34  	entrypoint := "password"
    35  	data["view"] = entrypoint
    36  	endpoint, err := getEndpoint(r.URL.Path, "/"+entrypoint)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	switch {
    41  	case strings.HasPrefix(endpoint, "/edit") && r.Method == "POST":
    42  		action = "edit"
    43  		if err := validatePasswordChangeForm(r, rr); err != nil {
    44  			attachFailStatus(data, "Bad Request")
    45  			break
    46  		}
    47  		if err = store.Request(operator.ChangePassword, rr); err != nil {
    48  			attachFailStatus(data, fmt.Sprintf("%v", err))
    49  			break
    50  		}
    51  		attachSuccessStatus(data, "Password has been changed")
    52  	}
    53  	attachView(data, entrypoint, action, status)
    54  	return nil
    55  }