|
| 1 | +package keycloak |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | +) |
| 8 | + |
| 9 | +const renkuAdminRole string = "renku-admin" |
| 10 | + |
| 11 | +func (client *KeycloakClient) FindUser(ctx context.Context, realm string, email string) (userID string, err error) { |
| 12 | + getURL := client.GetAdminUsersURL(realm) |
| 13 | + |
| 14 | + query := url.Values{} |
| 15 | + query.Set("email", email) |
| 16 | + query.Set("exact", "true") |
| 17 | + getURL.RawQuery = query.Encode() |
| 18 | + |
| 19 | + var result []getUsersResponse |
| 20 | + _, err = client.GetJSON(ctx, getURL.String(), &result) |
| 21 | + if err != nil { |
| 22 | + return "", err |
| 23 | + } |
| 24 | + |
| 25 | + for _, user := range result { |
| 26 | + if user.Email == email { |
| 27 | + userID = user.ID |
| 28 | + fmt.Printf("Found user ID: %s\n", userID) |
| 29 | + return userID, nil |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return "", fmt.Errorf("Could not find user '%s' in Keycloak", email) |
| 34 | +} |
| 35 | + |
| 36 | +func (client *KeycloakClient) GetAdminUsersURL(realm string) *url.URL { |
| 37 | + path := fmt.Sprintf("./admin/realms/%s/users", realm) |
| 38 | + return client.BaseURL.JoinPath(path) |
| 39 | +} |
| 40 | + |
| 41 | +type getUsersResponse struct { |
| 42 | + ID string `json:"id"` |
| 43 | + Email string `json:"email"` |
| 44 | +} |
| 45 | + |
| 46 | +func (client *KeycloakClient) IsRenkuAdmin(ctx context.Context, realm string, userID string) (isAdmin bool, err error) { |
| 47 | + getURL := client.GetAdminRolesURL(realm, userID) |
| 48 | + |
| 49 | + var result []roleMapping |
| 50 | + _, err = client.GetJSON(ctx, getURL.String(), &result) |
| 51 | + if err != nil { |
| 52 | + return false, err |
| 53 | + } |
| 54 | + |
| 55 | + for _, role := range result { |
| 56 | + if role.Name == renkuAdminRole { |
| 57 | + return true, nil |
| 58 | + } |
| 59 | + } |
| 60 | + return false, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (client *KeycloakClient) findRenkuAdminRole(ctx context.Context, realm string, userID string) (role roleMapping, err error) { |
| 64 | + getURL := client.GetAdminAvailavleRolesURL(realm, userID) |
| 65 | + |
| 66 | + var result []roleMapping |
| 67 | + _, err = client.GetJSON(ctx, getURL.String(), &result) |
| 68 | + if err != nil { |
| 69 | + return roleMapping{}, err |
| 70 | + } |
| 71 | + |
| 72 | + for _, roleObj := range result { |
| 73 | + if roleObj.Name == renkuAdminRole { |
| 74 | + role = roleObj |
| 75 | + return role, err |
| 76 | + } |
| 77 | + } |
| 78 | + return roleMapping{}, fmt.Errorf("Could not find role '%s' in Keycloak", renkuAdminRole) |
| 79 | +} |
| 80 | + |
| 81 | +func (client *KeycloakClient) AddRenkuAdminRoleToUser(ctx context.Context, realm string, userID string) error { |
| 82 | + role, err := client.findRenkuAdminRole(ctx, realm, userID) |
| 83 | + |
| 84 | + postURL := client.GetAdminRolesURL(realm, userID) |
| 85 | + |
| 86 | + body := []roleMapping{ |
| 87 | + role, |
| 88 | + } |
| 89 | + |
| 90 | + _, err = client.PostJSON(ctx, postURL.String(), body, nil) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func (client *KeycloakClient) GetAdminRolesURL(realm string, userID string) *url.URL { |
| 98 | + path := fmt.Sprintf("./admin/realms/%s/users/%s/role-mappings/realm", realm, userID) |
| 99 | + return client.BaseURL.JoinPath(path) |
| 100 | +} |
| 101 | + |
| 102 | +func (client *KeycloakClient) GetAdminAvailavleRolesURL(realm string, userID string) *url.URL { |
| 103 | + path := fmt.Sprintf("./admin/realms/%s/users/%s/role-mappings/realm/available", realm, userID) |
| 104 | + return client.BaseURL.JoinPath(path) |
| 105 | +} |
| 106 | + |
| 107 | +type roleMapping struct { |
| 108 | + ID string `json:"id,omitempty"` |
| 109 | + ContainerID string `json:"containerId,omitempty"` |
| 110 | + Name string `json:"name,omitempty"` |
| 111 | +} |
0 commit comments