-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.go
More file actions
457 lines (397 loc) · 12.1 KB
/
user.go
File metadata and controls
457 lines (397 loc) · 12.1 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
//go:embed userinfo.gql
var userinfoQuery string
//go:embed users.gql
var usersQuery string
//go:embed groups.gql
var groupsQuery string
type UserInfo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FirstName string `json:"firstname"`
Username string `json:"username"`
Emails []UserEmail `json:"emails"`
Groups []UserGroup `json:"groups"`
Roles []UserRole `json:"roles"`
AcceptedTOS bool `json:"accepted_tos"`
SurveySubmittedTime *string `json:"survey_submitted_time"`
}
type UserEmail struct {
Email string `json:"email"`
}
type UserGroup struct {
ID int64 `json:"id"`
Group UserGroupDetails `json:"group"`
}
type UserGroupDetails struct {
Name string `json:"name"`
GroupID int64 `json:"group_id"`
}
type UserRole struct {
Role UserRoleDetails `json:"role"`
}
type UserRoleDetails struct {
Description string `json:"description"`
ID int64 `json:"id"`
Name string `json:"name"`
}
type UserInfoRequest struct {
OperationName string `json:"operationName"`
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}
type UserInfoResponse struct {
Data struct {
Users []UserInfo `json:"users"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
func getUserInfo(server string) (*UserInfo, error) {
token, err := ensureValidToken()
if err != nil {
return nil, fmt.Errorf("authentication required: %w", err)
}
query := userinfoQuery
// Create GraphQL request
graphqlReq := UserInfoRequest{
OperationName: "UserInfo",
Query: query,
Variables: map[string]interface{}{},
}
jsonData, err := json.Marshal(graphqlReq)
if err != nil {
return nil, fmt.Errorf("failed to marshal GraphQL request: %w", err)
}
url := fmt.Sprintf("https://%s/v1/graphql", server)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.IDToken))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Hasura-Role", "jhuser")
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("GraphQL request failed (status %d): %s", resp.StatusCode, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
var response UserInfoResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
// Check for GraphQL errors
if len(response.Errors) > 0 {
return nil, fmt.Errorf("GraphQL errors: %v", response.Errors)
}
if len(response.Data.Users) == 0 {
return nil, fmt.Errorf("no user information found")
}
return &response.Data.Users[0], nil
}
func showUserInfo(server string) error {
userInfo, err := getUserInfo(server)
if err != nil {
return err
}
fmt.Printf("User Information:\n\n")
fmt.Printf("ID: %d\n", userInfo.ID)
fmt.Printf("Name: %s\n", userInfo.Name)
fmt.Printf("First Name: %s\n", userInfo.FirstName)
fmt.Printf("Username: %s\n", userInfo.Username)
fmt.Printf("Accepted Terms of Service: %t\n", userInfo.AcceptedTOS)
if userInfo.SurveySubmittedTime != nil {
fmt.Printf("Survey Submitted: %s\n", *userInfo.SurveySubmittedTime)
}
// Show emails
if len(userInfo.Emails) > 0 {
fmt.Printf("\nEmails:\n")
for _, email := range userInfo.Emails {
fmt.Printf(" - %s\n", email.Email)
}
}
// Show groups
if len(userInfo.Groups) > 0 {
fmt.Printf("\nGroups:\n")
for _, group := range userInfo.Groups {
fmt.Printf(" - %s (ID: %d)\n", group.Group.Name, group.Group.GroupID)
}
}
// Show roles
if len(userInfo.Roles) > 0 {
fmt.Printf("\nRoles:\n")
for _, role := range userInfo.Roles {
fmt.Printf(" - %s: %s\n", role.Role.Name, role.Role.Description)
}
}
return nil
}
// ManageUser represents a user from the /app/config/features/manage endpoint
type ManageUser struct {
Email string `json:"email"`
Name *string `json:"name"`
UUID string `json:"uuid"`
Features json.RawMessage `json:"features"` // Will be parsed from JSON string
JuliaHubGroups string `json:"juliahub_groups"`
SiteGroups string `json:"site_groups"`
ParsedFeatures map[string]interface{} `json:"-"` // Parsed features
}
// ManageUsersResponse represents the response from /app/config/features/manage
type ManageUsersResponse struct {
Users []ManageUser `json:"users"`
Features json.RawMessage `json:"features"`
}
type GroupsGQLResponse struct {
Data struct {
Groups []struct {
Name string `json:"name"`
GroupID int64 `json:"group_id"`
} `json:"groups"`
Products []struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
ID int64 `json:"id"`
ComputeTypeName string `json:"compute_type_name"`
} `json:"products"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
type AdminGroup struct {
Name string `json:"name"`
ID int64 `json:"id"`
}
func listGroups(server string) error {
token, err := ensureValidToken()
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
url := fmt.Sprintf("https://%s/app/config/groups", server)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.IDToken))
req.Header.Set("Accept", "application/json")
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to fetch groups: %w", err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("request failed (status %d): %s", resp.StatusCode, string(body))
}
var groups []AdminGroup
if err := json.Unmarshal(body, &groups); err != nil {
return fmt.Errorf("failed to parse groups: %w", err)
}
if len(groups) == 0 {
fmt.Println("No groups found")
return nil
}
for _, g := range groups {
fmt.Println(g.Name)
}
return nil
}
func listGroupsGQL(server string) error {
token, err := ensureValidToken()
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
gqlReq := UserInfoRequest{
OperationName: "Groups",
Query: groupsQuery,
Variables: map[string]interface{}{"limit": 500},
}
jsonData, err := json.Marshal(gqlReq)
if err != nil {
return fmt.Errorf("failed to marshal groups request: %w", err)
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://%s/v1/graphql", server), bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.IDToken))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Hasura-Role", "jhuser")
req.Header.Set("X-Juliahub-Ensure-JS", "true")
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to fetch groups: %w", err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
var groupsResp GroupsGQLResponse
if err := json.Unmarshal(body, &groupsResp); err != nil {
return fmt.Errorf("failed to parse groups: %w", err)
}
if len(groupsResp.Errors) > 0 {
return fmt.Errorf("GraphQL errors: %v", groupsResp.Errors)
}
if len(groupsResp.Data.Groups) == 0 {
fmt.Println("No groups found")
return nil
}
for _, g := range groupsResp.Data.Groups {
fmt.Println(g.Name)
}
return nil
}
type UsersGQLResponse struct {
Data struct {
Users []struct {
Username string `json:"username"`
ID int64 `json:"id"`
Name *string `json:"name"`
} `json:"users"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
func listUsersGQL(server string) error {
token, err := ensureValidToken()
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
gqlReq := UserInfoRequest{
OperationName: "Users",
Query: usersQuery,
Variables: map[string]interface{}{"limit": 500},
}
jsonData, err := json.Marshal(gqlReq)
if err != nil {
return fmt.Errorf("failed to marshal users request: %w", err)
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://%s/v1/graphql", server), bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.IDToken))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Hasura-Role", "jhuser")
req.Header.Set("X-Juliahub-Ensure-JS", "true")
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to fetch users: %w", err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
var usersResp UsersGQLResponse
if err := json.Unmarshal(body, &usersResp); err != nil {
return fmt.Errorf("failed to parse users: %w", err)
}
if len(usersResp.Errors) > 0 {
return fmt.Errorf("GraphQL errors: %v", usersResp.Errors)
}
if len(usersResp.Data.Users) == 0 {
fmt.Println("No users found")
return nil
}
for _, u := range usersResp.Data.Users {
name := u.Username
if u.Name != nil && *u.Name != "" {
name = *u.Name
}
fmt.Printf("%s (%s)\n", name, u.Username)
}
return nil
}
func listUsers(server string, verbose bool) error {
token, err := ensureValidToken()
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
url := fmt.Sprintf("https://%s/app/config/features/manage", server)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.IDToken))
req.Header.Set("Accept", "application/json")
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("request failed (status %d): %s", resp.StatusCode, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}
var response ManageUsersResponse
if err := json.Unmarshal(body, &response); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
// Parse features JSON string for each user (only needed in verbose mode)
if verbose {
for i := range response.Users {
var features map[string]interface{}
if err := json.Unmarshal(response.Users[i].Features, &features); err == nil {
response.Users[i].ParsedFeatures = features
}
}
}
if verbose {
fmt.Printf("Users (%d total):\n\n", len(response.Users))
// Verbose mode: show all details
for _, user := range response.Users {
fmt.Printf("UUID: %s\n", user.UUID)
fmt.Printf("Email: %s\n", user.Email)
if user.Name != nil {
fmt.Printf("Name: %s\n", *user.Name)
}
if user.JuliaHubGroups != "" {
fmt.Printf("JuliaHub Groups: %s\n", user.JuliaHubGroups)
}
if user.SiteGroups != "" {
fmt.Printf("Site Groups: %s\n", user.SiteGroups)
}
if len(user.ParsedFeatures) > 0 {
fmt.Printf("Features: %v\n", user.ParsedFeatures)
}
fmt.Println()
}
} else {
for _, user := range response.Users {
name := user.Email
if user.Name != nil && *user.Name != "" {
name = *user.Name
}
fmt.Printf("%s (%s)\n", name, user.Email)
}
}
return nil
}