forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.py
More file actions
138 lines (110 loc) · 3.87 KB
/
Copy pathprofile.py
File metadata and controls
138 lines (110 loc) · 3.87 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from linode_api4.errors import UnexpectedResponseError
from linode_api4.objects import Base, Property
class AuthorizedApp(Base):
api_endpoint = "/profile/apps/{id}"
properties = {
"id": Property(identifier=True),
"scopes": Property(),
"label": Property(),
"created": Property(is_datetime=True),
"expiry": Property(is_datetime=True),
"thumbnail_url": Property(),
"website": Property(),
}
class PersonalAccessToken(Base):
api_endpoint = "/profile/tokens/{id}"
properties = {
"id": Property(identifier=True),
"scopes": Property(),
"label": Property(mutable=True),
"created": Property(is_datetime=True),
"token": Property(),
"expiry": Property(is_datetime=True),
}
class WhitelistEntry(Base):
api_endpoint = "/profile/whitelist/{id}"
properties = {
'id': Property(identifier=True),
'address': Property(),
'netmask': Property(),
'note': Property(),
}
class Profile(Base):
api_endpoint = "/profile"
id_attribute = 'username'
properties = {
'username': Property(identifier=True),
'uid': Property(),
'email': Property(mutable=True),
'timezone': Property(mutable=True),
'email_notifications': Property(mutable=True),
'referrals': Property(),
'ip_whitelist_enabled': Property(mutable=True),
'lish_auth_method': Property(mutable=True),
'authorized_keys': Property(mutable=True),
'two_factor_auth': Property(),
'restricted': Property(),
}
def enable_tfa(self):
"""
Enables TFA for the token's user. This requies a follow-up request
to confirm TFA. Returns the TFA secret that needs to be confirmed.
"""
result = self._client.post('/profile/tfa-enable')
return result['secret']
def confirm_tfa(self, code):
"""
Confirms TFA for an account. Needs a TFA code generated by enable_tfa
"""
self._client.post('/profile/tfa-enable-confirm', data={
"tfa_code": code
})
return True
def disable_tfa(self):
"""
Turns off TFA for this user's account.
"""
self._client.post('/profile/tfa-disable')
return True
@property
def grants(self):
"""
Returns grants for the current user
"""
from linode_api4.objects.account import UserGrants # pylint: disable-all
resp = self._client.get('/profile/grants') # use special endpoint for restricted users
grants = None
if resp is not None:
# if resp is None, we're unrestricted and do not have grants
grants = UserGrants(self._client, self.username, resp)
return grants
@property
def whitelist(self):
"""
Returns the user's whitelist entries, if whitelist is enabled
"""
return self._client._get_and_filter(WhitelistEntry)
def add_whitelist_entry(self, address, netmask, note=None):
"""
Adds a new entry to this user's IP whitelist, if enabled
"""
result = self._client.post("{}/whitelist".format(Profile.api_endpoint),
data={
"address": address,
"netmask": netmask,
"note": note,
})
if not 'id' in result:
raise UnexpectedResponseError("Unexpected response creating whitelist entry!")
return WhitelistEntry(result['id'], self._client, json=result)
class SSHKey(Base):
"""
An SSH Public Key uploaded to your profile for use in Linode Instance deployments.
"""
api_endpoint = '/profile/sshkeys/{id}'
properties = {
"id": Property(identifier=True),
"label": Property(mutable=True),
"ssh_key": Property(),
"created": Property(is_datetime=True),
}