|
| 1 | +package keycloak |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type OrganizationDomain struct { |
| 10 | + Name string `json:"name,omitempty"` |
| 11 | + Verified bool `json:"verified,omitempty"` |
| 12 | +} |
| 13 | + |
| 14 | +type Organization struct { |
| 15 | + Id string `json:"id,omitempty"` |
| 16 | + RealmId string `json:"-"` |
| 17 | + Name string `json:"name"` |
| 18 | + Alias string `json:"alias,omitempty"` |
| 19 | + Enabled bool `json:"enabled"` |
| 20 | + RedirectUrl string `json:"redirectUrl,omitempty"` |
| 21 | + Description string `json:"description,omitempty"` |
| 22 | + Domains []OrganizationDomain `json:"domains,omitempty"` |
| 23 | + Attributes map[string][]string `json:"attributes,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +func (keycloakClient *KeycloakClient) CreateOrganization(ctx context.Context, organization *Organization) error { |
| 27 | + path := fmt.Sprintf("/realms/%s/organizations", organization.RealmId) |
| 28 | + |
| 29 | + _, location, err := keycloakClient.post(ctx, path, organization) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + // Extract ID from location URL |
| 35 | + parts := strings.Split(location, "/") |
| 36 | + organization.Id = parts[len(parts)-1] |
| 37 | + |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +// GetOrganizationsPath returns the URL for the organization API endpoint |
| 42 | +func (keycloakClient *KeycloakClient) GetOrganizationsPath(realmId string) string { |
| 43 | + return fmt.Sprintf("/realms/%s/organizations", realmId) |
| 44 | +} |
| 45 | + |
| 46 | +// GetOrganizations gets all organizations in a realm |
| 47 | +// This function can be used to list all organizations or filter by search criteria |
| 48 | +func (keycloakClient *KeycloakClient) GetOrganizations(ctx context.Context, realmId string, params ...map[string]string) ([]*Organization, error) { |
| 49 | + var organizations []*Organization |
| 50 | + queryParams := make(map[string]string) |
| 51 | + |
| 52 | + // Apply optional filter parameters |
| 53 | + if len(params) > 0 && params[0] != nil { |
| 54 | + for k, v := range params[0] { |
| 55 | + queryParams[k] = v |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + err := keycloakClient.get(ctx, keycloakClient.GetOrganizationsPath(realmId), &organizations, queryParams) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to fetch organizations: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + // Set RealmId for each organization |
| 65 | + for _, organization := range organizations { |
| 66 | + organization.RealmId = realmId |
| 67 | + } |
| 68 | + |
| 69 | + return organizations, nil |
| 70 | +} |
| 71 | + |
| 72 | +// GetOrganizationsPaginated gets organizations with pagination support |
| 73 | +func (keycloakClient *KeycloakClient) GetOrganizationsPaginated(ctx context.Context, realmId string, first, max int, search string) ([]*Organization, error) { |
| 74 | + queryParams := map[string]string{ |
| 75 | + "first": fmt.Sprintf("%d", first), |
| 76 | + "max": fmt.Sprintf("%d", max), |
| 77 | + } |
| 78 | + |
| 79 | + if search != "" { |
| 80 | + queryParams["search"] = search |
| 81 | + } |
| 82 | + |
| 83 | + return keycloakClient.GetOrganizations(ctx, realmId, queryParams) |
| 84 | +} |
| 85 | + |
| 86 | +// GetOrganizationByName gets an organization by name |
| 87 | +func (keycloakClient *KeycloakClient) GetOrganizationByName(ctx context.Context, realmId, name string) (*Organization, error) { |
| 88 | + var organizations []*Organization |
| 89 | + |
| 90 | + params := map[string]string{ |
| 91 | + "search": name, |
| 92 | + "exact": "true", |
| 93 | + } |
| 94 | + |
| 95 | + err := keycloakClient.get(ctx, keycloakClient.GetOrganizationsPath(realmId), &organizations, params) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + // Find the exact match by name |
| 101 | + for _, organization := range organizations { |
| 102 | + if organization.Name == name { |
| 103 | + organization.RealmId = realmId |
| 104 | + return organization, nil |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return nil, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (keycloakClient *KeycloakClient) GetOrganization(ctx context.Context, realmId, id string) (*Organization, error) { |
| 112 | + organization := Organization{} |
| 113 | + organization.RealmId = realmId |
| 114 | + |
| 115 | + path := fmt.Sprintf("/realms/%s/organizations/%s", realmId, id) |
| 116 | + err := keycloakClient.get(ctx, path, &organization, nil) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + return &organization, nil |
| 122 | +} |
| 123 | + |
| 124 | +func (keycloakClient *KeycloakClient) UpdateOrganization(ctx context.Context, organization *Organization) error { |
| 125 | + path := fmt.Sprintf("/realms/%s/organizations/%s", organization.RealmId, organization.Id) |
| 126 | + return keycloakClient.put(ctx, path, organization) |
| 127 | +} |
| 128 | + |
| 129 | +func (keycloakClient *KeycloakClient) DeleteOrganization(ctx context.Context, realmId, id string) error { |
| 130 | + path := fmt.Sprintf("/realms/%s/organizations/%s", realmId, id) |
| 131 | + return keycloakClient.delete(ctx, path, nil) |
| 132 | +} |
0 commit comments