Skip to content

Commit

Permalink
Migrate Two FA from packngo to metal-go client
Browse files Browse the repository at this point in the history
Signed-off-by: Ayush Rangwala <[email protected]>
  • Loading branch information
aayushrangwala authored and ctreatma committed Jan 24, 2024
1 parent cd49f37 commit ce17551
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
5 changes: 3 additions & 2 deletions internal/twofa/disable2fa.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package twofa

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,12 +50,12 @@ func (c *Client) Disable() *cobra.Command {

cmd.SilenceUsage = true
if sms {
_, err := c.Service.DisableSms(token)
_, err := c.TwoFAService.DisableTfaSms(context.Background()).Execute()
if err != nil {
return fmt.Errorf("Could not disable Two-Factor Authentication via SMS: %w", err)
}
} else if app {
_, err := c.Service.DisableApp(token)
_, err := c.TwoFAService.DisableTfaApp(context.Background()).Execute()
if err != nil {
return fmt.Errorf("Could not disable Two-Factor Authentication via App: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/twofa/enable2fa.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package twofa

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,12 +49,12 @@ func (c *Client) Enable() *cobra.Command {

cmd.SilenceUsage = true
if sms {
_, err := c.Service.EnableSms(token)
_, err := c.TwoFAService.EnableTfaSms(context.Background()).Execute()
if err != nil {
return fmt.Errorf("Could not enable Two-Factor Authentication: %w", err)
}
} else if app {
_, err := c.Service.EnableApp(token)
_, err := c.TwoFAService.EnableTfaApp(context.Background()).Execute()
if err != nil {
return fmt.Errorf("Could not enable Two-Factor Authentication: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/twofa/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package twofa

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -47,7 +48,7 @@ func (c *Client) Receive() *cobra.Command {

cmd.SilenceUsage = true
if sms {
_, err := c.Service.ReceiveSms()
_, err := c.OtpService.ReceiveCodes(context.Background()).Execute()
if err != nil {
return fmt.Errorf("Could not issue token via SMS: %w", err)
}
Expand All @@ -56,7 +57,7 @@ func (c *Client) Receive() *cobra.Command {
return nil
}

otpURI, _, err := c.Service.SeedApp()
otpURI, _, err := c.TwoFAService.SeedApp()
if err != nil {
return fmt.Errorf("Could not get the OTP Seed URI: %w", err)
}
Expand Down
19 changes: 12 additions & 7 deletions internal/twofa/twofa.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ package twofa

import (
"github.com/equinix/metal-cli/internal/outputs"
"github.com/packethost/packngo"

metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

type Client struct {
Servicer Servicer
Service packngo.TwoFactorAuthService
Out outputs.Outputer
Servicer Servicer
TwoFAService *metal.TwoFactorAuthApiService
OtpService *metal.OTPsApiService
Out outputs.Outputer
}

func (c *Client) NewCommand() *cobra.Command {
Expand All @@ -45,7 +47,8 @@ func (c *Client) NewCommand() *cobra.Command {
root.PersistentPreRun(cmd, args)
}
}
c.Service = c.Servicer.API(cmd).TwoFactorAuth
c.TwoFAService = c.Servicer.MetalAPI(cmd).TwoFactorAuthApi
c.OtpService = c.Servicer.MetalAPI(cmd).OTPsApi
},
}

Expand All @@ -58,8 +61,10 @@ func (c *Client) NewCommand() *cobra.Command {
}

type Servicer interface {
API(*cobra.Command) *packngo.Client
ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions
MetalAPI(*cobra.Command) *metal.APIClient
Filters() map[string]string
Includes(defaultIncludes []string) (incl []string)
Excludes(defaultExcludes []string) (excl []string)
}

func NewClient(s Servicer, out outputs.Outputer) *Client {
Expand Down

0 comments on commit ce17551

Please sign in to comment.