As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Unverified Commit 41ff323e authored by Shekhar Patnaik's avatar Shekhar Patnaik
Browse files

'Fix issue #708: Error handling, naming conventions, and commenting'

parent 4fc2f587
No related branches found
No related tags found
No related merge requests found
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment