-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user.go
executable file
·63 lines (60 loc) · 1.61 KB
/
create_user.go
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
package keycloak
import (
"context"
"github.com/Nerzal/gocloak/v12"
"github.com/pkg/errors"
"github.com/thavlik/t4vd/base/pkg/iam"
"go.uber.org/zap"
)
func (i *keyCloakIAM) CreateUser(
user *iam.User,
password string,
temporary bool,
) (string, error) {
accessToken, err := i.refreshAccessToken(context.Background())
if err != nil {
return "", err
}
i.log.Debug("creating user",
zap.String("realm", i.kc.Realm),
zap.String("user.ID", user.ID),
zap.String("user.Username", user.Username),
zap.String("user.FirstName", user.FirstName),
zap.String("user.LastName", user.LastName),
zap.String("user.Email", user.Email))
user.ID, err = i.kc.CreateUser(
context.Background(),
accessToken,
i.kc.Realm,
gocloak.User{
ID: gocloak.StringP(user.ID),
Username: gocloak.StringP(user.Username),
FirstName: gocloak.StringP(user.FirstName),
LastName: gocloak.StringP(user.LastName),
Email: gocloak.StringP(user.Email),
Enabled: gocloak.BoolP(user.Enabled),
EmailVerified: gocloak.BoolP(true),
},
)
if err != nil {
return "", errors.Wrap(err, "keycloak create user")
}
if err := i.kc.SetPassword(
context.Background(),
accessToken,
user.ID,
i.kc.Realm,
password,
temporary,
); err != nil {
return "", errors.Wrap(err, "keycloak set password")
}
i.log.Debug("created user",
zap.String("realm", i.kc.Realm),
zap.String("user.ID", user.ID),
zap.String("user.Username", user.Username),
zap.String("user.FirstName", user.FirstName),
zap.String("user.LastName", user.LastName),
zap.String("user.Email", user.Email))
return user.ID, nil
}