profile_devices.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  // ProfileDevice represents a ProfileDevice object
  12  type ProfileDevice struct {
  13  	// When this Remember Me session was started.
  14  	Created *time.Time `json:"-"`
  15  
  16  	// When this TrustedDevice session expires. Sessions typically last 30 days.
  17  	Expiry *time.Time `json:"-"`
  18  
  19  	// The unique ID for this TrustedDevice.
  20  	ID int `json:"id"`
  21  
  22  	// he last time this TrustedDevice was successfully used to authenticate to login.linode.com
  23  	LastAuthenticated *time.Time `json:"-"`
  24  
  25  	// The last IP Address to successfully authenticate with this TrustedDevice.
  26  	LastRemoteAddr string `json:"last_remote_addr"`
  27  
  28  	// The User Agent of the browser that created this TrustedDevice session.
  29  	UserAgent string `json:"user_agent"`
  30  }
  31  
  32  // UnmarshalJSON implements the json.Unmarshaler interface
  33  func (pd *ProfileDevice) UnmarshalJSON(b []byte) error {
  34  	type Mask ProfileDevice
  35  
  36  	l := struct {
  37  		*Mask
  38  
  39  		Created           *parseabletime.ParseableTime `json:"created"`
  40  		Expiry            *parseabletime.ParseableTime `json:"expiry"`
  41  		LastAuthenticated *parseabletime.ParseableTime `json:"last_authenticated"`
  42  	}{
  43  		Mask: (*Mask)(pd),
  44  	}
  45  
  46  	if err := json.Unmarshal(b, &l); err != nil {
  47  		return err
  48  	}
  49  
  50  	pd.Created = (*time.Time)(l.Created)
  51  	pd.Expiry = (*time.Time)(l.Expiry)
  52  	pd.LastAuthenticated = (*time.Time)(l.LastAuthenticated)
  53  
  54  	return nil
  55  }
  56  
  57  // GetProfileDevice returns the ProfileDevice with the provided id
  58  func (c *Client) GetProfileDevice(ctx context.Context, deviceID int) (*ProfileDevice, error) {
  59  	e := formatAPIPath("profile/devices/%d", deviceID)
  60  	return doGETRequest[ProfileDevice](ctx, c, e)
  61  }
  62  
  63  // ListProfileDevices lists ProfileDevices for the User
  64  func (c *Client) ListProfileDevices(ctx context.Context, opts *ListOptions) ([]ProfileDevice, error) {
  65  	return getPaginatedResults[ProfileDevice](ctx, c, "profile/devices", opts)
  66  }
  67  
  68  // DeleteProfileDevice revokes the given ProfileDevice's status as a trusted device
  69  func (c *Client) DeleteProfileDevice(ctx context.Context, deviceID int) error {
  70  	e := formatAPIPath("profile/devices/%d", deviceID)
  71  	return doDELETERequest(ctx, c, e)
  72  }
  73