profile.go raw
1 package linodego
2
3 import (
4 "context"
5 )
6
7 // LishAuthMethod constants start with AuthMethod and include Linode API Lish Authentication Methods
8 type LishAuthMethod string
9
10 // LishAuthMethod constants are the methods of authentication allowed when connecting via Lish
11 const (
12 AuthMethodPasswordKeys LishAuthMethod = "password_keys"
13 AuthMethodKeysOnly LishAuthMethod = "keys_only"
14 AuthMethodDisabled LishAuthMethod = "disabled"
15 )
16
17 // ProfileReferrals represent a User's status in the Referral Program
18 type ProfileReferrals struct {
19 Total int `json:"total"`
20 Completed int `json:"completed"`
21 Pending int `json:"pending"`
22 Credit float64 `json:"credit"`
23 Code string `json:"code"`
24 URL string `json:"url"`
25 }
26
27 // Profile represents a Profile object
28 type Profile struct {
29 UID int `json:"uid"`
30 Username string `json:"username"`
31 Email string `json:"email"`
32 Timezone string `json:"timezone"`
33 EmailNotifications bool `json:"email_notifications"`
34 IPWhitelistEnabled bool `json:"ip_whitelist_enabled"`
35 TwoFactorAuth bool `json:"two_factor_auth"`
36 Restricted bool `json:"restricted"`
37 LishAuthMethod LishAuthMethod `json:"lish_auth_method"`
38 Referrals ProfileReferrals `json:"referrals"`
39 AuthorizedKeys []string `json:"authorized_keys"`
40 AuthenticationType string `json:"authentication_type"`
41 VerifiedPhoneNumber string `json:"verified_phone_number,omitempty"`
42 }
43
44 // ProfileUpdateOptions fields are those accepted by UpdateProfile
45 type ProfileUpdateOptions struct {
46 Email string `json:"email,omitempty"`
47 Timezone string `json:"timezone,omitempty"`
48 EmailNotifications *bool `json:"email_notifications,omitempty"`
49 IPWhitelistEnabled *bool `json:"ip_whitelist_enabled,omitempty"`
50 LishAuthMethod LishAuthMethod `json:"lish_auth_method,omitempty"`
51 AuthorizedKeys *[]string `json:"authorized_keys,omitempty"`
52 TwoFactorAuth *bool `json:"two_factor_auth,omitempty"`
53 Restricted *bool `json:"restricted,omitempty"`
54 }
55
56 // GetUpdateOptions converts a Profile to ProfileUpdateOptions for use in UpdateProfile
57 func (i Profile) GetUpdateOptions() (o ProfileUpdateOptions) {
58 o.Email = i.Email
59 o.Timezone = i.Timezone
60 o.EmailNotifications = copyBool(&i.EmailNotifications)
61 o.IPWhitelistEnabled = copyBool(&i.IPWhitelistEnabled)
62 o.LishAuthMethod = i.LishAuthMethod
63 authorizedKeys := make([]string, len(i.AuthorizedKeys))
64 copy(authorizedKeys, i.AuthorizedKeys)
65 o.AuthorizedKeys = &authorizedKeys
66 o.TwoFactorAuth = copyBool(&i.TwoFactorAuth)
67 o.Restricted = copyBool(&i.Restricted)
68
69 return o
70 }
71
72 // GetProfile returns the Profile of the authenticated user
73 func (c *Client) GetProfile(ctx context.Context) (*Profile, error) {
74 return doGETRequest[Profile](ctx, c, "profile")
75 }
76
77 // UpdateProfile updates the Profile with the specified id
78 func (c *Client) UpdateProfile(ctx context.Context, opts ProfileUpdateOptions) (*Profile, error) {
79 return doPUTRequest[Profile](ctx, c, "profile", opts)
80 }
81