-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
209 lines (181 loc) · 7.19 KB
/
user.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package pritunl
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// UserRequest represents the structure of User Get/Post/Put request
type UserRequest struct {
Name string `json:"name"`
Email string `json:"email"`
AuthType string `json:"auth_type"`
YubicoId string `json:"yubico_id"`
Groups []string `json:"groups"`
Pin string `json:"pin"`
Disabled bool `json:"disabled"`
NetworkLinks []string `json:"network_links"`
BypassSecondary bool `json:"bypass_secondary"`
ClientToClient bool `json:"client_to_client"`
MacAddresses []string `json:"mac_addresses"`
DnsServers []string `json:"dns_servers"`
DnsSuffix string `json:"dns_suffix"`
PortForwarding []UserPortForwardingData `json:"port_forwarding"`
SendKeyEmail bool `json:"send_key_email"` // Addition for Put Method
}
// UserResponse represents the structure of User response
type UserResponse struct {
ID string `json:"id"`
Organization string `json:"organization"`
OrganizationName string `json:"organization_name"`
Name string `json:"name"`
Email string `json:"email"`
Groups []string `json:"groups"`
LastActive int64 `json:"last_active"`
Pin bool `json:"pin"`
Type string `json:"type"`
AuthType string `json:"auth_type"`
YubicoId string `json:"yubico_id"`
OTPSecret string `json:"otp_secret"`
Disabled bool `json:"disabled"`
BypassSecondary bool `json:"bypass_secondary"`
ClientToClient bool `json:"client_to_client"`
MacAddresses []string `json:"mac_addresses"`
DnsServers []string `json:"dns_servers"`
DnsSuffix string `json:"dns_suffix"`
PortForwarding []UserPortForwardingData `json:"port_forwarding"`
Devices []interface{} `json:"devices"`
Gravatar bool `json:"gravatar"`
Audit bool `json:"audit"`
Status bool `json:"status"`
SSO interface{} `json:"sso"`
AuthModes []interface{} `json:"auth_modes"`
DNSMapping interface{} `json:"dns_mapping"`
NetworkLinks []interface{} `json:"network_links"`
Servers []ServerData `json:"servers"` // Nested struct for servers
}
// Substructure of `UserRequest` and `UserResponse` structs for `PortForwarding` field
type UserPortForwardingData struct {
Protocol string `json:"protocol"`
Port string `json:"port"`
Dport string `json:"dport"`
}
// Substructure of `UserResponse` struct for `Servers` field
type ServerData struct {
ID string `json:"id"`
Name string `json:"name"`
Status bool `json:"status"`
ServerID string `json:"server_id"`
DeviceName interface{} `json:"device_name"`
Platform interface{} `json:"platform"`
RealAddress interface{} `json:"real_address"`
VirtAddress string `json:"virt_address"`
VirtAddress6 string `json:"virt_address6"`
ConnectedSince interface{} `json:"connected_since"`
}
// UserGet retrieves a user or users on the server
func (c *Client) UserGet(ctx context.Context, orgId string, userId ...string) ([]UserResponse, error) {
// Construct the API path based on the orgId and optional userId
path := fmt.Sprintf("/user/%s", orgId)
// Handle optional userId argument
if len(userId) > 0 {
// If userId is provided, append it to the path
path = fmt.Sprintf("%s/%s", path, userId[0])
}
// Send an authenticated HTTP GET request to the API
response, err := c.AuthRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close() // Close the response body when done
// Unmarshal the JSON data into a slice of UserResponse
var users []UserResponse
if err := handleUnmarshal(body, &users); err != nil {
return nil, err
}
// Return the slice of users
return users, nil
}
// UserCreate creates a new user on the server
func (c *Client) UserCreate(ctx context.Context, orgId string, newUser UserRequest) ([]UserResponse, error) {
// Marshal the UserRequest struct into JSON data
userData, err := json.Marshal(newUser)
if err != nil {
return nil, fmt.Errorf("failed to marshal user data: %w", err)
}
// Construct the API path based on the orgId
path := fmt.Sprintf("/user/%s", orgId)
// Send an authenticated HTTP POST request to the API
response, err := c.AuthRequest(ctx, http.MethodPost, path, userData)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of UserResponse
var users []UserResponse
if err := handleUnmarshal(body, &users); err != nil {
return nil, err
}
// Return the slice of users
return users, nil
}
// UserUpdate updates an existing user on the server
func (c *Client) UserUpdate(ctx context.Context, orgId string, userId string, updateUser UserRequest) ([]UserResponse, error) {
// Marshal the UserRequest struct into JSON data
userData, err := json.Marshal(updateUser)
if err != nil {
return nil, fmt.Errorf("failed to marshal user data: %w", err)
}
// Construct the API path using the orgId and userId
path := fmt.Sprintf("/user/%s/%s", orgId, userId)
// Send an authenticated HTTP PUT request to the API
response, err := c.AuthRequest(ctx, http.MethodPut, path, userData)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of UserResponse
var users []UserResponse
if err := handleUnmarshal(body, &users); err != nil {
return nil, err
}
// Return the slice of users
return users, nil
}
// UserDelete deletes an existing user on the server
func (c *Client) UserDelete(ctx context.Context, orgId string, userId string) ([]UserResponse, error) {
// Construct the API path using the organization ID and user ID
path := fmt.Sprintf("/user/%s/%s", orgId, userId)
// Send an authenticated HTTP DELETE request to the API
response, err := c.AuthRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of UserResponse
var users []UserResponse
if err := handleUnmarshal(body, &users); err != nil {
return nil, err
}
return users, nil
}