Skip to content

Commit 4b0ac0e

Browse files
committed
keycloak user & groups
1 parent 1f1ff6c commit 4b0ac0e

File tree

12 files changed

+353
-574
lines changed

12 files changed

+353
-574
lines changed

plugins/keycloak/pkg/app/assets/transform_template.tmpl

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,55 @@
99
{
1010
"id": "{{ $.id }}",
1111
"type": "user",
12-
"display_name": "{{ $.firstname }} {{ $.middlename -}} {{ $.lastname }}",
12+
"display_name": "{{ $.firstName }} {{ $.lastName }}",
1313
"properties": {
14-
"enabled": "{{ not $.account_locked }}",
14+
"enabled": "{{ $.enabled }}",
1515
"email": "{{ $.email }}",
1616
"user_id": "{{ $.id }}",
1717
"username": "{{ $.username }}",
18-
"manager": "{{ $.manager }}",
19-
"organization": "{{ $.company }}",
20-
"department": "{{ $.department }}",
21-
"title": "{{ $.jobTitle }}",
22-
{{ range $i, $attr := $.attributes }}
23-
{{ if eq $attr.name "roles" }}
24-
"{{ $attr.name }}": {{ splitList "," $attr.value | marshal }},
25-
{{ else }}
26-
"{{ $attr.name }}": "{{ $attr.value }}",
27-
{{ end }}
28-
{{ end }}
29-
"status": "{{ $status }}"
30-
},
31-
"created_at": "{{ $.created }}"
18+
"totp": "{{ $.totp }}"
19+
}
3220
},
3321
{
3422
"id": "{{ $.email }}",
3523
"type": "identity",
36-
"display_name": "{{ $.firstname }} {{ $.middlename -}} {{ $.lastname }} (email)"
24+
"display_name": "{{ $.firstName }} {{ $.lastName }} (email)"
3725
},
3826
{
3927
"id": "{{ $.username }}",
4028
"type": "identity",
41-
"display_name": "{{ $.firstname }} {{ $.middlename -}} {{ $.lastname }} (username)"
29+
"display_name": "{{ $.firstName }} {{ $.lastName }} (username)"
4230
}
4331
{{ end }}
44-
45-
{{ if eq $.type "user_group" }}
32+
{{ if eq $.type "group" }}
4633
{
47-
"id": "{{ $.name }}",
34+
"id": "{{ $.id }}",
4835
"type": "group",
49-
"display_name": "{{ $.name }}"
36+
"display_name": "{{ $.name }}",
37+
"properties": {
38+
"path": "{{ $.path }}"
39+
}
5040
}
5141
{{ end }}
5242
],
5343
"relations": [
5444
{{ if eq $.type "user" }}
5545
{
56-
"object_type": "user",
57-
"object_id": "{{ $.id }}",
46+
"object_type": "identity",
47+
"object_id": "{{ $.email }}",
5848
"relation": "identifier",
59-
"subject_type": "identity",
60-
"subject_id": "{{ $.email }}"
49+
"subject_type": "user",
50+
"subject_id": "{{ $.id }}"
6151
},
6252
{
63-
"object_type": "user",
64-
"object_id": "{{ $.id }}",
53+
"object_type": "identity",
54+
"object_id": "{{ $.username }}",
6555
"relation": "identifier",
66-
"subject_type": "identity",
67-
"subject_id": "{{ $.username }}"
68-
}
69-
{{ if $.manager }}
70-
,{
71-
"object_type": "user",
72-
"object_id": "{{ $.id }}",
73-
"relation": "manager",
7456
"subject_type": "user",
75-
"subject_id": "{{ $.manager }}"
57+
"subject_id": "{{ $.id }}"
7658
}
77-
{{ end }}
7859
{{ end }}
79-
80-
{{ if eq $.type "user_group" }}
60+
{{ if eq $.type "group" }}
8161
{{ range $i, $user := $.users }}
8262
{{ if $i }},{{ end }}
8363
{

plugins/keycloak/pkg/app/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type ExecCmd struct {
1414
}
1515

1616
func (cmd *ExecCmd) Run(ctx *cc.CommonCtx) error {
17-
gClient, err := kc.NewKeyCloudClient(ctx.Context, cmd.APIKey)
17+
gClient, err := kc.NewKeycloakClient(ctx.Context, &cmd.KeycloakClientConfig)
1818
if err != nil {
1919
return err
2020
}

plugins/keycloak/pkg/app/fetch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
)
1111

1212
type FetchCmd struct {
13-
APIKey string `short:"k" help:"keycloak API Key" env:"KC_API_KEY" required:""`
14-
Groups bool `short:"g" help:"Retrieve keycloak groups" env:"KC_GROUPS" default:"false"`
13+
kc.KeycloakClientConfig
14+
Groups bool `short:"g" help:"Retrieve keycloak groups" env:"KEYCLOAK_GROUPS" default:"false"`
1515
}
1616

1717
func (cmd *FetchCmd) Run(ctx *cc.CommonCtx) error {
18-
kcClient, err := kc.NewKeyCloudClient(ctx.Context, cmd.APIKey)
18+
kcClient, err := kc.NewKeycloakClient(ctx.Context, &cmd.KeycloakClientConfig)
1919
if err != nil {
2020
return err
2121
}

plugins/keycloak/pkg/app/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type VerifyCmd struct {
1111
}
1212

1313
func (v *VerifyCmd) Run(ctx *cc.CommonCtx) error {
14-
gClient, err := kc.NewKeyCloudClient(ctx.Context, v.APIKey)
14+
gClient, err := kc.NewKeycloakClient(ctx.Context, &v.KeycloakClientConfig)
1515
if err != nil {
1616
return err
1717
}

plugins/keycloak/pkg/fetch/fetch.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
)
1212

1313
type Fetcher struct {
14-
kcc *kc.KeyCloudClient
14+
kcc *kc.KeycloakClient
1515
Groups bool
1616
}
1717

18-
func New(client *kc.KeyCloudClient) (*Fetcher, error) {
18+
func New(client *kc.KeycloakClient) (*Fetcher, error) {
1919
return &Fetcher{
2020
kcc: client,
2121
}, nil
@@ -36,9 +36,10 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter
3636
return err
3737
}
3838

39-
idLookup := map[string]*kc.BaseUser{}
39+
idLookup := map[string]*kc.User{}
4040

4141
for _, user := range users {
42+
user.Type = "user"
4243
userBytes, err := json.Marshal(user)
4344
if err != nil {
4445
errorWriter.Error(err)
@@ -56,7 +57,7 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter
5657
errorWriter.Error(err)
5758
}
5859

59-
idLookup[user.ID] = &user.BaseUser
60+
idLookup[user.ID] = user
6061
}
6162

6263
if f.Groups {
@@ -71,15 +72,16 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter
7172
func (f *Fetcher) fetchGroups(ctx context.Context,
7273
writer *js.JSONArrayWriter,
7374
errorWriter common.ErrorWriter,
74-
idLookup map[string]*kc.BaseUser,
75+
idLookup map[string]*kc.User,
7576
) error {
76-
groups, err := f.kcc.ListGroups(ctx, kc.UserGroups)
77+
groups, err := f.kcc.ListGroups(ctx)
7778
if err != nil {
7879
errorWriter.Error(err)
7980
return err
8081
}
8182

8283
for _, group := range groups {
84+
group.Type = "group"
8385
groupBytes, err := json.Marshal(group)
8486
errorWriter.Error(err)
8587

@@ -89,11 +91,10 @@ func (f *Fetcher) fetchGroups(ctx context.Context,
8991
continue
9092
}
9193

92-
usersInGroup, err := f.kcc.ExpandUsersInGroup(ctx, group.ID, idLookup)
94+
usersInGroup, err := f.kcc.GetUsersOfGroup(ctx, group.ID)
9395
errorWriter.Error(err)
9496

9597
usersInGroupBytes, err := json.Marshal(usersInGroup)
96-
9798
errorWriter.Error(err)
9899

99100
var users []map[string]any

plugins/keycloak/pkg/kc/group.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package kc
22

3-
const TypeGroup string = "group"
4-
53
type Group struct {
6-
ID string `json:"id"`
7-
Name string `json:"name"`
8-
Type string `json:"type"`
9-
Description string `json:"description,omitempty"`
10-
Email string `json:"email,omitempty"`
11-
Attributes interface{} `json:"attributes,omitempty"`
4+
Type string `json:"type"`
5+
ID string `json:"id"`
6+
Name string `json:"name"`
7+
Path string `json:"path"`
8+
SubGroupCount int `json:"subGroupCount"`
9+
SubGroups []any `json:"subGroups"`
10+
Access struct {
11+
View bool `json:"view"`
12+
ViewMembers bool `json:"viewMembers"`
13+
ManageMembers bool `json:"manageMembers"`
14+
Manage bool `json:"manage"`
15+
ManageMembership bool `json:"manageMembership"`
16+
} `json:"access"`
1217
}

0 commit comments

Comments
 (0)