profile_tfa.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  // TwoFactorSecret contains fields returned by CreateTwoFactorSecret
  12  type TwoFactorSecret struct {
  13  	Expiry *time.Time `json:"expiry"`
  14  	Secret string     `json:"secret"`
  15  }
  16  
  17  // ConfirmTwoFactorOptions contains fields used by ConfirmTwoFactor
  18  type ConfirmTwoFactorOptions struct {
  19  	TFACode string `json:"tfa_code"`
  20  }
  21  
  22  // ConfirmTwoFactorResponse contains fields returned by ConfirmTwoFactor
  23  type ConfirmTwoFactorResponse struct {
  24  	Scratch string `json:"scratch"`
  25  }
  26  
  27  func (s *TwoFactorSecret) UnmarshalJSON(b []byte) error {
  28  	type Mask TwoFactorSecret
  29  
  30  	p := struct {
  31  		*Mask
  32  
  33  		Expiry *parseabletime.ParseableTime `json:"expiry"`
  34  	}{
  35  		Mask: (*Mask)(s),
  36  	}
  37  
  38  	if err := json.Unmarshal(b, &p); err != nil {
  39  		return err
  40  	}
  41  
  42  	s.Expiry = (*time.Time)(p.Expiry)
  43  
  44  	return nil
  45  }
  46  
  47  // CreateTwoFactorSecret generates a Two Factor secret for your User.
  48  func (c *Client) CreateTwoFactorSecret(ctx context.Context) (*TwoFactorSecret, error) {
  49  	return doPOSTRequest[TwoFactorSecret, any](ctx, c, "profile/tfa-enable")
  50  }
  51  
  52  // DisableTwoFactor disables Two Factor Authentication for your User.
  53  func (c *Client) DisableTwoFactor(ctx context.Context) error {
  54  	return doPOSTRequestNoRequestResponseBody(ctx, c, "profile/tfa-disable")
  55  }
  56  
  57  // ConfirmTwoFactor confirms that you can successfully generate Two Factor codes and enables TFA on your Account.
  58  func (c *Client) ConfirmTwoFactor(ctx context.Context, opts ConfirmTwoFactorOptions) (*ConfirmTwoFactorResponse, error) {
  59  	return doPOSTRequest[ConfirmTwoFactorResponse](ctx, c, "profile/tfa-enable-confirm", opts)
  60  }
  61