2020-09-24 14:34:58 -07:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-08-27 20:43:33 -04:00
|
|
|
package accounts
|
2020-08-17 09:37:15 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/api"
|
|
|
|
|
)
|
|
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
func (c *Client) ChangePassword(ctx context.Context, accountId, currentPassword, newPassword string, version uint32, opt ...Option) (*AccountUpdateResult, error) {
|
2020-08-17 09:37:15 -07:00
|
|
|
if accountId == "" {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("empty accountId value passed into ChangePassword request")
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
|
|
|
|
if c.client == nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("nil client in ChangePassword request")
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts, apiOpts := getOpts(opt...)
|
|
|
|
|
|
|
|
|
|
if version == 0 {
|
|
|
|
|
if !opts.withAutomaticVersioning {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, errors.New("zero version number passed into Update request and automatic versioning not specified")
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
2020-09-24 14:34:58 -07:00
|
|
|
existingTarget, existingErr := c.Read(ctx, accountId, opt...)
|
2020-08-17 09:37:15 -07:00
|
|
|
if existingErr != nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
if api.AsServerError(existingErr) != nil {
|
|
|
|
|
return nil, fmt.Errorf("error from controller when performing initial check-and-set read: %w", existingErr)
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
|
|
|
|
if existingTarget == nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, errors.New("nil resource found when performing initial check-and-set read")
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
2020-09-15 01:08:21 -04:00
|
|
|
version = existingTarget.Item.Version
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 18:29:38 -05:00
|
|
|
reqBody := map[string]any{
|
2020-09-08 13:27:51 -07:00
|
|
|
"version": version,
|
|
|
|
|
"current_password": currentPassword,
|
|
|
|
|
"new_password": newPassword,
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-08 13:27:51 -07:00
|
|
|
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("accounts/%s:change-password", accountId), reqBody, apiOpts...)
|
2020-08-17 09:37:15 -07:00
|
|
|
if err != nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("error creating ChangePassword request: %w", err)
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
|
if err != nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("error performing client request during ChangePassword call: %w", err)
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-15 01:08:21 -04:00
|
|
|
target := new(AccountUpdateResult)
|
|
|
|
|
target.Item = new(Account)
|
|
|
|
|
apiErr, err := resp.Decode(target.Item)
|
2020-08-17 09:37:15 -07:00
|
|
|
if err != nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("error decoding ChangePassword response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if apiErr != nil {
|
|
|
|
|
return nil, apiErr
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|
2024-04-17 11:13:08 -07:00
|
|
|
target.Response = resp
|
2020-09-24 14:34:58 -07:00
|
|
|
return target, nil
|
2020-08-17 09:37:15 -07:00
|
|
|
}
|