File: profile_devices.go

package info (click to toggle)
golang-github-linode-linodego 1.47.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 10,032 kB
  • sloc: makefile: 95; sh: 52; python: 24
file content (72 lines) | stat: -rw-r--r-- 2,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package linodego

import (
	"context"
	"encoding/json"
	"time"

	"github.com/linode/linodego/internal/parseabletime"
)

// ProfileDevice represents a ProfileDevice object
type ProfileDevice struct {
	// When this Remember Me session was started.
	Created *time.Time `json:"-"`

	// When this TrustedDevice session expires. Sessions typically last 30 days.
	Expiry *time.Time `json:"-"`

	// The unique ID for this TrustedDevice.
	ID int `json:"id"`

	// he last time this TrustedDevice was successfully used to authenticate to login.linode.com
	LastAuthenticated *time.Time `json:"-"`

	// The last IP Address to successfully authenticate with this TrustedDevice.
	LastRemoteAddr string `json:"last_remote_addr"`

	// The User Agent of the browser that created this TrustedDevice session.
	UserAgent string `json:"user_agent"`
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (pd *ProfileDevice) UnmarshalJSON(b []byte) error {
	type Mask ProfileDevice

	l := struct {
		*Mask
		Created           *parseabletime.ParseableTime `json:"created"`
		Expiry            *parseabletime.ParseableTime `json:"expiry"`
		LastAuthenticated *parseabletime.ParseableTime `json:"last_authenticated"`
	}{
		Mask: (*Mask)(pd),
	}

	if err := json.Unmarshal(b, &l); err != nil {
		return err
	}

	pd.Created = (*time.Time)(l.Created)
	pd.Expiry = (*time.Time)(l.Expiry)
	pd.LastAuthenticated = (*time.Time)(l.LastAuthenticated)

	return nil
}

// GetProfileDevice returns the ProfileDevice with the provided id
func (c *Client) GetProfileDevice(ctx context.Context, deviceID int) (*ProfileDevice, error) {
	e := formatAPIPath("profile/devices/%d", deviceID)
	return doGETRequest[ProfileDevice](ctx, c, e)
}

// ListProfileDevices lists ProfileDevices for the User
func (c *Client) ListProfileDevices(ctx context.Context, opts *ListOptions) ([]ProfileDevice, error) {
	return getPaginatedResults[ProfileDevice](ctx, c, "profile/devices", opts)
}

// DeleteProfileDevice revokes the given ProfileDevice's status as a trusted device
func (c *Client) DeleteProfileDevice(ctx context.Context, deviceID int) error {
	e := formatAPIPath("profile/devices/%d", deviceID)
	err := doDELETERequest(ctx, c, e)
	return err
}