From 41ff323e6d6853467e9c8a8239c1d8fba473e616 Mon Sep 17 00:00:00 2001 From: Shekhar Patnaik Date: Tue, 23 Apr 2024 10:42:06 +0100 Subject: [PATCH] 'Fix issue #708: Error handling, naming conventions, and commenting' --- internal/gitlabnet/twofactorverify/client.go | 149 ++++++++++--------- 1 file changed, 76 insertions(+), 73 deletions(-) diff --git a/internal/gitlabnet/twofactorverify/client.go b/internal/gitlabnet/twofactorverify/client.go index 24167c150..dbe2c0ce1 100644 --- a/internal/gitlabnet/twofactorverify/client.go +++ b/internal/gitlabnet/twofactorverify/client.go @@ -1,105 +1,108 @@ package twofactorverify import ( - "context" - "errors" - "fmt" - "net/http" - - "gitlab.com/gitlab-org/gitlab-shell/v14/client" - "gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs" - "gitlab.com/gitlab-org/gitlab-shell/v14/internal/config" - "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet" - "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/discover" + "context" + "errors" + "fmt" + "net/http" + "log" + + "gitlab.com/gitlab-org/gitlab-shell/v14/client" + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs" + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet" + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/discover" ) +// Client is the main struct for handling two-factor authentication operations. type Client struct { - config *config.Config - client *client.GitlabNetClient + config *config.Config + client *client.GitlabNetClient } +// Response represents the response from the two-factor authentication API. type Response struct { - Success bool `json:"success"` - Message string `json:"message"` + Success bool `json:"success"` + Message string `json:"message"` } +// RequestBody is the request body for the two-factor authentication API. type RequestBody struct { - KeyId string `json:"key_id,omitempty"` - UserId int64 `json:"user_id,omitempty"` - OTPAttempt string `json:"otp_attempt,omitempty"` + KeyID string `json:"key_id,omitempty"` + UserID int64 `json:"user_id,omitempty"` + OTPAttempt string `json:"otp_attempt,omitempty"` } +// NewClient creates a new Client instance. func NewClient(config *config.Config) (*Client, error) { - client, err := gitlabnet.GetClient(config) - if err != nil { - return nil, fmt.Errorf("Error creating http client: %v", err) - } + client, err := gitlabnet.GetClient(config) + if err != nil { + return nil, fmt.Errorf("Error creating http client: %v", err) + } - return &Client{config: config, client: client}, nil + return &Client{config: config, client: client}, nil } +// VerifyOTP verifies a one-time password (OTP) for a user. func (c *Client) VerifyOTP(ctx context.Context, args *commandargs.Shell, otp string) error { - requestBody, err := c.getRequestBody(ctx, args, otp) - if err != nil { - return err - } - - response, err := c.client.Post(ctx, "/two_factor_manual_otp_check", requestBody) - if err != nil { - return err - } - defer response.Body.Close() - - return parse(response) + requestBody, err := c.getRequestBody(ctx, args, otp) + if err != nil { + return err + } + + response, err := c.client.Post(ctx, "/two_factor_manual_otp_check", requestBody) + if err != nil { + return err + } + defer func() { + if err := response.Body.Close(); err != nil { + log.Printf("Error closing response body: %v", err) + } + }() + + return parse(response) } +// PushAuth handles two-factor authentication via push notification. func (c *Client) PushAuth(ctx context.Context, args *commandargs.Shell) error { - requestBody, err := c.getRequestBody(ctx, args, "") - if err != nil { - return err - } - - response, err := c.client.Post(ctx, "/two_factor_push_otp_check", requestBody) - if err != nil { - return err - } - defer response.Body.Close() - - return parse(response) + requestBody, err := c.getRequestBody(ctx, args, "") + if err != nil { + return err + } + + response, err := c.client.Post(ctx, "/two_factor_push_otp_check", requestBody) + if err != nil { + return err + } + defer func() { + if err := response.Body.Close(); err != nil { + log.Printf("Error closing response body: %v", err) + } + }() + + return parse(response) } func parse(hr *http.Response) error { - response := &Response{} - if err := gitlabnet.ParseJSON(hr, response); err != nil { - return err - } + response := &Response{} + if err := gitlabnet.ParseJSON(hr, response); err != nil { + return err + } - if !response.Success { - return errors.New(response.Message) - } + if !response.Success { + return errors.New(response.Message) + } - return nil + return nil } func (c *Client) getRequestBody(ctx context.Context, args *commandargs.Shell, otp string) (*RequestBody, error) { - client, err := discover.NewClient(c.config) + client, err := discover.NewClient(c.config) - if err != nil { - return nil, err - } + if err != nil { + return nil, err + } - var requestBody *RequestBody - if args.GitlabKeyId != "" { - requestBody = &RequestBody{KeyId: args.GitlabKeyId, OTPAttempt: otp} - } else { - userInfo, err := client.GetByCommandArgs(ctx, args) - - if err != nil { - return nil, err - } - - requestBody = &RequestBody{UserId: userInfo.UserId, OTPAttempt: otp} - } - - return requestBody, nil -} + var requestBody *RequestBody + if args.GitlabKeyId != "" { + requestBody = & \ No newline at end of file -- GitLab