-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
114 lines (92 loc) · 2.87 KB
/
admin.go
File metadata and controls
114 lines (92 loc) · 2.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
package keycloak
import (
"context"
"fmt"
"net/url"
)
const renkuAdminRole string = "renku-admin"
func (client *KeycloakClient) FindUser(ctx context.Context, realm string, email string) (userID string, err error) {
getURL := client.GetAdminUsersURL(realm)
query := url.Values{}
query.Set("email", email)
query.Set("exact", "true")
getURL.RawQuery = query.Encode()
var result []getUsersResponse
_, err = client.GetJSON(ctx, getURL.String(), &result)
if err != nil {
return "", err
}
for _, user := range result {
if user.Email == email {
userID = user.ID
fmt.Printf("Found user ID: %s\n", userID)
return userID, nil
}
}
return "", fmt.Errorf("could not find user '%s' in Keycloak", email)
}
func (client *KeycloakClient) GetAdminUsersURL(realm string) *url.URL {
path := fmt.Sprintf("./admin/realms/%s/users", realm)
return client.BaseURL.JoinPath(path)
}
type getUsersResponse struct {
ID string `json:"id"`
Email string `json:"email"`
}
func (client *KeycloakClient) IsRenkuAdmin(ctx context.Context, realm string, userID string) (isAdmin bool, err error) {
getURL := client.GetAdminRolesURL(realm, userID)
var result []roleMapping
_, err = client.GetJSON(ctx, getURL.String(), &result)
if err != nil {
return false, err
}
for _, role := range result {
if role.Name == renkuAdminRole {
return true, nil
}
}
return false, nil
}
func (client *KeycloakClient) findRenkuAdminRole(ctx context.Context, realm string, userID string) (role roleMapping, err error) {
getURL := client.GetAdminAvailavleRolesURL(realm, userID)
var result []roleMapping
_, err = client.GetJSON(ctx, getURL.String(), &result)
if err != nil {
return roleMapping{}, err
}
for _, roleObj := range result {
if roleObj.Name == renkuAdminRole {
role = roleObj
return role, err
}
}
return roleMapping{}, fmt.Errorf("could not find role '%s' in Keycloak", renkuAdminRole)
}
func (client *KeycloakClient) AddRenkuAdminRoleToUser(ctx context.Context, realm string, userID string) error {
role, err := client.findRenkuAdminRole(ctx, realm, userID)
if err != nil {
return err
}
postURL := client.GetAdminRolesURL(realm, userID)
body := []roleMapping{
role,
}
_, err = client.PostJSON(ctx, postURL.String(), body, nil)
if err != nil {
return err
}
return nil
}
func (client *KeycloakClient) GetAdminRolesURL(realm string, userID string) *url.URL {
path := fmt.Sprintf("./admin/realms/%s/users/%s/role-mappings/realm", realm, userID)
return client.BaseURL.JoinPath(path)
}
func (client *KeycloakClient) GetAdminAvailavleRolesURL(realm string, userID string) *url.URL {
path := fmt.Sprintf("./admin/realms/%s/users/%s/role-mappings/realm/available", realm, userID)
return client.BaseURL.JoinPath(path)
}
type roleMapping struct {
ID string `json:"id,omitempty"`
ContainerID string `json:"containerId,omitempty"`
Name string `json:"name,omitempty"`
}