github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/iam/user_password.go (about)

     1  package iam
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  	"strconv"
    10  
    11  	validation "github.com/go-ozzo/ozzo-validation/v4"
    12  )
    13  
    14  type (
    15  	// UserPassword is the IAM managing user's password API interface
    16  	UserPassword interface {
    17  		// ResetUserPassword optionally sends a one-time use password to the user.
    18  		// If you send the email with the password directly to the user, the response for this operation doesn't include that password.
    19  		// If you don't send the password to the user through email, the password is included in the response.
    20  		//
    21  		// See: https://techdocs.akamai.com/iam-user-admin/reference/post-reset-password
    22  		ResetUserPassword(context.Context, ResetUserPasswordRequest) (*ResetUserPasswordResponse, error)
    23  
    24  		// SetUserPassword sets a specific password for a user
    25  		//
    26  		// See: https://techdocs.akamai.com/iam-user-admin/reference/post-set-password
    27  		SetUserPassword(context.Context, SetUserPasswordRequest) error
    28  	}
    29  
    30  	// ResetUserPasswordRequest contains the request parameters of the reset user password endpoint
    31  	ResetUserPasswordRequest struct {
    32  		IdentityID string
    33  		SendEmail  bool
    34  	}
    35  
    36  	// ResetUserPasswordResponse contains the response from the reset user password endpoint
    37  	ResetUserPasswordResponse struct {
    38  		NewPassword string `json:"newPassword"`
    39  	}
    40  
    41  	// SetUserPasswordRequest contains the request parameters of the set user password endpoint
    42  	SetUserPasswordRequest struct {
    43  		IdentityID  string `json:"-"`
    44  		NewPassword string `json:"newPassword"`
    45  	}
    46  )
    47  
    48  var (
    49  	// ErrResetUserPassword is returned when ResetUserPassword fails
    50  	ErrResetUserPassword = errors.New("reset user password")
    51  
    52  	// ErrSetUserPassword is returned when SetUserPassword fails
    53  	ErrSetUserPassword = errors.New("set user password")
    54  )
    55  
    56  // Validate validates ResetUserPasswordRequest
    57  func (r ResetUserPasswordRequest) Validate() error {
    58  	return validation.Errors{
    59  		"IdentityID": validation.Validate(r.IdentityID, validation.Required),
    60  	}.Filter()
    61  }
    62  
    63  // Validate validates SetUserPasswordRequest
    64  func (r SetUserPasswordRequest) Validate() error {
    65  	return validation.Errors{
    66  		"IdentityID":  validation.Validate(r.IdentityID, validation.Required),
    67  		"NewPassword": validation.Validate(r.NewPassword, validation.Required),
    68  	}.Filter()
    69  }
    70  
    71  func (i *iam) ResetUserPassword(ctx context.Context, params ResetUserPasswordRequest) (*ResetUserPasswordResponse, error) {
    72  	if err := params.Validate(); err != nil {
    73  		return nil, fmt.Errorf("%s: %w:\n%s", ErrResetUserPassword, ErrStructValidation, err)
    74  	}
    75  
    76  	u, err := url.Parse(fmt.Sprintf("/identity-management/v2/user-admin/ui-identities/%s/reset-password", params.IdentityID))
    77  	if err != nil {
    78  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrResetUserPassword, err)
    79  	}
    80  
    81  	q := u.Query()
    82  	q.Add("sendEmail", strconv.FormatBool(params.SendEmail))
    83  	u.RawQuery = q.Encode()
    84  
    85  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), nil)
    86  	if err != nil {
    87  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrResetUserPassword, err)
    88  	}
    89  
    90  	var result ResetUserPasswordResponse
    91  	resp, err := i.Exec(req, &result)
    92  	if err != nil {
    93  		return nil, fmt.Errorf("%w: request failed: %s", ErrResetUserPassword, err)
    94  	}
    95  
    96  	if !((!params.SendEmail && resp.StatusCode == http.StatusOK) || (params.SendEmail && resp.StatusCode == http.StatusNoContent)) {
    97  		return nil, fmt.Errorf("%s: %w", ErrResetUserPassword, i.Error(resp))
    98  	}
    99  
   100  	return &result, nil
   101  }
   102  
   103  func (i *iam) SetUserPassword(ctx context.Context, params SetUserPasswordRequest) error {
   104  	if err := params.Validate(); err != nil {
   105  		return fmt.Errorf("%s: %w:\n%s", ErrSetUserPassword, ErrStructValidation, err)
   106  	}
   107  
   108  	u := fmt.Sprintf("/identity-management/v2/user-admin/ui-identities/%s/set-password", params.IdentityID)
   109  
   110  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, nil)
   111  	if err != nil {
   112  		return fmt.Errorf("%w: failed to create request: %s", ErrSetUserPassword, err)
   113  	}
   114  
   115  	resp, err := i.Exec(req, nil, params)
   116  	if err != nil {
   117  		return fmt.Errorf("%w: request failed: %s", ErrSetUserPassword, err)
   118  	}
   119  
   120  	if resp.StatusCode != http.StatusNoContent {
   121  		return fmt.Errorf("%s: %w", ErrSetUserPassword, i.Error(resp))
   122  	}
   123  
   124  	return nil
   125  }