Skip to content

Commit

Permalink
Fix #452 Issues in Update password
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 6, 2023
1 parent 393488b commit b87f184
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 15 additions & 3 deletions internal/handler/v2/onboard/onboard_reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package onboard

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/iam"
"github.com/bb-consent/api/internal/token"
"github.com/bb-consent/api/internal/user"
)

type resetPasswordReq struct {
Expand All @@ -32,10 +34,20 @@ func OnboardResetPassword(w http.ResponseWriter, r *http.Request) {
common.HandleErrorV2(w, http.StatusBadRequest, err.Error(), err)
return
}
err = iam.ResetPassword(userIamID, resetReq.NewPassword)

// fetch the current user
user, err := user.GetByIamID(userIamID)
if err != nil {
log.Printf("Failed to reset user:%v password ")
common.HandleErrorV2(w, http.StatusBadRequest, err.Error(), err)
m := "Failed to fetch user"
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

// reset user password
err = iam.ResetPassword(userIamID, user.Email, resetReq.CurrentPassword, resetReq.NewPassword)
if err != nil {
m := fmt.Sprintf("Failed to reset user:%v password", userIamID)
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

Expand Down
12 changes: 10 additions & 2 deletions internal/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,23 @@ type IamError struct {
Error string `json:"error_description"`
}

func ResetPassword(userId string, password string) error {
func ResetPassword(userId string, username string, currentPassword string, newPassword string) error {
client := GetClient()

ctx := context.Background()
clientId := IamConfig.ClientId
grantType := "password"
_, err := client.GetToken(ctx, IamConfig.Realm, gocloak.TokenOptions{Username: &username, Password: &currentPassword, ClientID: &clientId, GrantType: &grantType})
if err != nil {
return err
}

token, err := GetAdminToken(IamConfig.AdminUser, IamConfig.AdminPassword, "master", client)
if err != nil {
return err
}

err = client.SetPassword(context.Background(), token.AccessToken, userId, IamConfig.Realm, password, false)
err = client.SetPassword(context.Background(), token.AccessToken, userId, IamConfig.Realm, newPassword, false)
return err
}

Expand Down

0 comments on commit b87f184

Please sign in to comment.