14 changed files with 547 additions and 217 deletions
@ -1,32 +0,0 @@ |
|||||
package client |
|
||||
|
|
||||
import ( |
|
||||
"testing" |
|
||||
"time" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
"github.com/stretchr/testify/require" |
|
||||
) |
|
||||
|
|
||||
func initTest(t *testing.T) *Client { |
|
||||
var config = HttpConfig{ |
|
||||
Addr: "http://172.19.0.3:8080", |
|
||||
Username: "admin", |
|
||||
Password: "admin", |
|
||||
Timeout: time.Second * 20, |
|
||||
} |
|
||||
var client *Client |
|
||||
{ |
|
||||
var err error |
|
||||
client, err = New(config) |
|
||||
require.Nil(t, err, "could not create client") |
|
||||
} |
|
||||
return client |
|
||||
} |
|
||||
|
|
||||
func TestGetToken(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
var err = client.getToken() |
|
||||
require.Nil(t, err, "could not get token") |
|
||||
assert.NotZero(t, client.accessToken) |
|
||||
} |
|
||||
@ -1,52 +0,0 @@ |
|||||
package client |
|
||||
|
|
||||
import ( |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
"github.com/stretchr/testify/require" |
|
||||
) |
|
||||
|
|
||||
func TestGetRealms(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
var realms []RealmRepresentation |
|
||||
{ |
|
||||
var err error |
|
||||
realms, err = client.GetRealms() |
|
||||
require.Nil(t, err, "could not get realms") |
|
||||
assert.NotNil(t, realms) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func TestCreateRealm(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
var realm = RealmRepresentation{ |
|
||||
Realm: str("__internal"), |
|
||||
} |
|
||||
var err = client.CreateRealm(realm) |
|
||||
assert.Nil(t, err) |
|
||||
} |
|
||||
|
|
||||
func TestGetRealm(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
|
|
||||
var realm, err = client.GetRealm("__internal") |
|
||||
assert.Nil(t, err) |
|
||||
assert.NotNil(t, realm) |
|
||||
} |
|
||||
|
|
||||
func TestUpdateRealm(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
|
|
||||
var realm = RealmRepresentation{ |
|
||||
DisplayName: str("Test update realm"), |
|
||||
} |
|
||||
var err = client.UpdateRealm("__internal", realm) |
|
||||
assert.Nil(t, err) |
|
||||
} |
|
||||
func TestDeleteRealm(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
|
|
||||
var err = client.DeleteRealm("__internal") |
|
||||
assert.Nil(t, err) |
|
||||
} |
|
||||
@ -1,42 +0,0 @@ |
|||||
package client |
|
||||
|
|
||||
import ( |
|
||||
"gopkg.in/h2non/gentleman.v2/plugins/body" |
|
||||
"gopkg.in/h2non/gentleman.v2/plugins/url" |
|
||||
) |
|
||||
|
|
||||
const ( |
|
||||
userPath = "/auth/admin/realms/:realm/users" |
|
||||
userCountPath = userPath + "/count" |
|
||||
userIDPath = userPath + "/:id" |
|
||||
) |
|
||||
|
|
||||
func (c *Client) GetUsers(realm string) ([]UserRepresentation, error) { |
|
||||
var resp = []UserRepresentation{} |
|
||||
var err = c.get(&resp, url.Path(userPath), url.Param("realm", realm)) |
|
||||
return resp, err |
|
||||
} |
|
||||
|
|
||||
func (c *Client) CreateUser(realm string, user UserRepresentation) error { |
|
||||
return c.post(url.Path(userPath), url.Param("realm", realm), body.JSON(user)) |
|
||||
} |
|
||||
|
|
||||
func (c *Client) CountUsers(realm string) (int, error) { |
|
||||
var resp = 0 |
|
||||
var err = c.get(&resp, url.Path(userCountPath), url.Param("realm", realm)) |
|
||||
return resp, err |
|
||||
} |
|
||||
|
|
||||
func (c *Client) GetUser(realm, userID string) (UserRepresentation, error) { |
|
||||
var resp = UserRepresentation{} |
|
||||
var err = c.get(&resp, url.Path(userIDPath), url.Param("realm", realm), url.Param("id", userID)) |
|
||||
return resp, err |
|
||||
} |
|
||||
|
|
||||
func (c *Client) UpdateUser(realm, userID string, user UserRepresentation) error { |
|
||||
return c.put(url.Path(userIDPath), url.Param("realm", realm), url.Param("id", userID), body.JSON(user)) |
|
||||
} |
|
||||
|
|
||||
func (c *Client) DeleteUser(realm, userID string) error { |
|
||||
return c.delete(url.Path(userIDPath), url.Param("realm", realm), url.Param("id", userID)) |
|
||||
} |
|
||||
@ -1,67 +0,0 @@ |
|||||
package client |
|
||||
|
|
||||
import ( |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
"github.com/stretchr/testify/require" |
|
||||
) |
|
||||
|
|
||||
func TestGetUsers(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
var users []UserRepresentation |
|
||||
{ |
|
||||
var err error |
|
||||
users, err = client.GetUsers("__internal") |
|
||||
require.Nil(t, err, "could not get users") |
|
||||
} |
|
||||
for _, i := range users { |
|
||||
assert.NotZero(t, *i.Username) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func TestCreateUser(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
var realm = "__internal" |
|
||||
var user = UserRepresentation{ |
|
||||
Username: str("john"), |
|
||||
} |
|
||||
var err = client.CreateUser(realm, user) |
|
||||
assert.Nil(t, err) |
|
||||
} |
|
||||
|
|
||||
func TestCountUsers(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
var realm = "__internal" |
|
||||
|
|
||||
var count, err = client.CountUsers(realm) |
|
||||
assert.Nil(t, err) |
|
||||
assert.NotZero(t, count) |
|
||||
} |
|
||||
|
|
||||
func TestGetUser(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
var user UserRepresentation |
|
||||
{ |
|
||||
var err error |
|
||||
user, err = client.GetUser("__internal", "e8b96f33-1d14-463d-80db-294c4db249ab") |
|
||||
require.Nil(t, err, "could not get users") |
|
||||
assert.NotZero(t, *user.Username) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
func TestUpdateUser(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
|
|
||||
var user = UserRepresentation{ |
|
||||
Email: str("john.doe@elca.ch"), |
|
||||
} |
|
||||
var err = client.UpdateUser("__internal", "e8b96f33-1d14-463d-80db-294c4db249aa", user) |
|
||||
assert.Nil(t, err) |
|
||||
} |
|
||||
func TestDeleteUser(t *testing.T) { |
|
||||
var client = initTest(t) |
|
||||
|
|
||||
var err = client.DeleteUser("__internal", "eb8b75ea-305d-40f6-87e5-ac8e16979c40") |
|
||||
assert.Nil(t, err) |
|
||||
} |
|
||||
@ -0,0 +1 @@ |
|||||
|
package keycloak |
||||
@ -1,4 +1,4 @@ |
|||||
package client |
package keycloak |
||||
|
|
||||
type AdminEventRepresentation struct { |
type AdminEventRepresentation struct { |
||||
AuthDetails *AuthDetailsRepresentation `json:"authDetails,omitempty"` |
AuthDetails *AuthDetailsRepresentation `json:"authDetails,omitempty"` |
||||
@ -1,2 +0,0 @@ |
|||||
// Keycloak client is a client for keycloak.
|
|
||||
package keycloakclient |
|
||||
@ -0,0 +1,401 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"log" |
||||
|
"strings" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/cloudtrust/keycloak-client" |
||||
|
"github.com/spf13/pflag" |
||||
|
) |
||||
|
|
||||
|
const ( |
||||
|
tstRealm = "__internal" |
||||
|
user = "version" |
||||
|
) |
||||
|
|
||||
|
func main() { |
||||
|
var conf = getKeycloakConfig() |
||||
|
var client, err = keycloak.New(*conf) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not create keycloak client: %v", err) |
||||
|
} |
||||
|
|
||||
|
// Delete test realm
|
||||
|
client.DeleteRealm(tstRealm) |
||||
|
|
||||
|
// Check existing realms
|
||||
|
var initialRealms []keycloak.RealmRepresentation |
||||
|
{ |
||||
|
var err error |
||||
|
initialRealms, err = client.GetRealms() |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get realms: %v", err) |
||||
|
} |
||||
|
for _, r := range initialRealms { |
||||
|
if *r.Realm == tstRealm { |
||||
|
log.Fatalf("test realm should not exists yet") |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Create test realm.
|
||||
|
{ |
||||
|
var realm = tstRealm |
||||
|
var err = client.CreateRealm(keycloak.RealmRepresentation{ |
||||
|
Realm: &realm, |
||||
|
}) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not create keycloak client: %v", err) |
||||
|
} |
||||
|
fmt.Println("Test realm created.") |
||||
|
} |
||||
|
|
||||
|
// Check getRealm.
|
||||
|
{ |
||||
|
var realmR, err = client.GetRealm(tstRealm) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get test realm: %v", err) |
||||
|
} |
||||
|
if *realmR.Realm != tstRealm { |
||||
|
log.Fatalf("test realm has wrong name") |
||||
|
} |
||||
|
if realmR.DisplayName != nil { |
||||
|
log.Fatalf("test realm should not have a field displayName") |
||||
|
} |
||||
|
fmt.Println("Test realm exists.") |
||||
|
} |
||||
|
|
||||
|
// Update Realm
|
||||
|
{ |
||||
|
var displayName = "updated realm" |
||||
|
var err = client.UpdateRealm(tstRealm, keycloak.RealmRepresentation{ |
||||
|
DisplayName: &displayName, |
||||
|
}) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not update test realm: %v", err) |
||||
|
} |
||||
|
// Check update
|
||||
|
{ |
||||
|
var realmR, err = client.GetRealm(tstRealm) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get test realm: %v", err) |
||||
|
} |
||||
|
if *realmR.DisplayName != displayName { |
||||
|
log.Fatalf("test realm update failed") |
||||
|
} |
||||
|
} |
||||
|
fmt.Println("Test realm updated.") |
||||
|
} |
||||
|
|
||||
|
// Count users.
|
||||
|
{ |
||||
|
var nbrUser, err = client.CountUsers(tstRealm) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not count users: %v", err) |
||||
|
} |
||||
|
if nbrUser != 0 { |
||||
|
log.Fatalf("there should be 0 users") |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Create test users.
|
||||
|
{ |
||||
|
for _, u := range tstUsers { |
||||
|
var username = strings.ToLower(u.firstname + "." + u.lastname) |
||||
|
var email = username + "@cloudtrust.ch" |
||||
|
var err = client.CreateUser(tstRealm, keycloak.UserRepresentation{ |
||||
|
Username: &username, |
||||
|
FirstName: &u.firstname, |
||||
|
LastName: &u.lastname, |
||||
|
Email: &email, |
||||
|
}) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not create test users: %v", err) |
||||
|
} |
||||
|
} |
||||
|
// Check that all users where created.
|
||||
|
{ |
||||
|
var nbrUser, err = client.CountUsers(tstRealm) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not count users: %v", err) |
||||
|
} |
||||
|
if nbrUser != 50 { |
||||
|
log.Fatalf("there should be 50 users") |
||||
|
} |
||||
|
} |
||||
|
fmt.Println("Test users created.") |
||||
|
} |
||||
|
|
||||
|
// Get users
|
||||
|
{ |
||||
|
{ |
||||
|
// No parameters.
|
||||
|
var users, err = client.GetUsers(tstRealm) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get users: %v", err) |
||||
|
} |
||||
|
if len(users) != 50 { |
||||
|
log.Fatalf("there should be 50 users") |
||||
|
} |
||||
|
} |
||||
|
{ |
||||
|
// email.
|
||||
|
var users, err = client.GetUsers(tstRealm, "email", "john.doe@cloudtrust.ch") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get users: %v", err) |
||||
|
} |
||||
|
if len(users) != 1 { |
||||
|
log.Fatalf("there should be 1 user matched by email") |
||||
|
} |
||||
|
} |
||||
|
{ |
||||
|
// firstname.
|
||||
|
var users, err = client.GetUsers(tstRealm, "firstName", "John") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get users: %v", err) |
||||
|
} |
||||
|
// Match John and Johnny
|
||||
|
if len(users) != 2 { |
||||
|
log.Fatalf("there should be 2 user matched by firstname") |
||||
|
} |
||||
|
} |
||||
|
{ |
||||
|
// lastname.
|
||||
|
var users, err = client.GetUsers(tstRealm, "lastName", "Wells") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get users: %v", err) |
||||
|
} |
||||
|
if len(users) != 3 { |
||||
|
log.Fatalf("there should be 3 users matched by lastname") |
||||
|
} |
||||
|
} |
||||
|
{ |
||||
|
// username.
|
||||
|
var users, err = client.GetUsers(tstRealm, "username", "lucia.nelson") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get users: %v", err) |
||||
|
} |
||||
|
if len(users) != 1 { |
||||
|
log.Fatalf("there should be 1 user matched by username") |
||||
|
} |
||||
|
} |
||||
|
{ |
||||
|
// first.
|
||||
|
var users, err = client.GetUsers(tstRealm, "max", "7") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get users: %v", err) |
||||
|
} |
||||
|
if len(users) != 7 { |
||||
|
log.Fatalf("there should be 7 users matched by max") |
||||
|
} |
||||
|
} |
||||
|
{ |
||||
|
// search.
|
||||
|
var users, err = client.GetUsers(tstRealm, "search", "le") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get users: %v", err) |
||||
|
} |
||||
|
if len(users) != 7 { |
||||
|
log.Fatalf("there should be 7 users matched by search") |
||||
|
} |
||||
|
} |
||||
|
fmt.Println("Test users retrieved.") |
||||
|
} |
||||
|
|
||||
|
// Update user.
|
||||
|
{ |
||||
|
// Get user ID.
|
||||
|
var userID string |
||||
|
{ |
||||
|
var users, err = client.GetUsers(tstRealm, "search", "Maria") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get Maria: %v", err) |
||||
|
} |
||||
|
if len(users) != 1 { |
||||
|
log.Fatalf("there should be 1 users matched by search Maria") |
||||
|
} |
||||
|
if users[0].Id == nil { |
||||
|
log.Fatalf("user ID should not be nil") |
||||
|
} |
||||
|
userID = *users[0].Id |
||||
|
} |
||||
|
// Update user.
|
||||
|
var username = "Maria" |
||||
|
var updatedLastname = "updated" |
||||
|
{ |
||||
|
|
||||
|
var err = client.UpdateUser(tstRealm, userID, keycloak.UserRepresentation{ |
||||
|
FirstName: &username, |
||||
|
LastName: &updatedLastname, |
||||
|
}) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not update user: %v", err) |
||||
|
} |
||||
|
} |
||||
|
// Check that user was updated.
|
||||
|
{ |
||||
|
var users, err = client.GetUsers(tstRealm, "search", "Maria") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get Maria: %v", err) |
||||
|
} |
||||
|
if len(users) != 1 { |
||||
|
log.Fatalf("there should be 1 users matched by search Maria") |
||||
|
} |
||||
|
if users[0].LastName == nil || *users[0].LastName != updatedLastname { |
||||
|
log.Fatalf("user was not updated") |
||||
|
} |
||||
|
} |
||||
|
fmt.Println("User updated.") |
||||
|
} |
||||
|
|
||||
|
// Delete user.
|
||||
|
{ |
||||
|
// Get user ID.
|
||||
|
var userID string |
||||
|
{ |
||||
|
var users, err = client.GetUsers(tstRealm, "search", "Toni") |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get Toni: %v", err) |
||||
|
} |
||||
|
if len(users) != 1 { |
||||
|
log.Fatalf("there should be 1 users matched by search Toni") |
||||
|
} |
||||
|
if users[0].Id == nil { |
||||
|
log.Fatalf("user ID should not be nil") |
||||
|
} |
||||
|
userID = *users[0].Id |
||||
|
} |
||||
|
// Delete user.
|
||||
|
{ |
||||
|
var err = client.DeleteUser(tstRealm, userID) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not delete user: %v", err) |
||||
|
} |
||||
|
} |
||||
|
// Check that user was deleted.
|
||||
|
{ |
||||
|
var nbrUser, err = client.CountUsers(tstRealm) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not count users: %v", err) |
||||
|
} |
||||
|
if nbrUser != 49 { |
||||
|
log.Fatalf("there should be 49 users") |
||||
|
} |
||||
|
} |
||||
|
fmt.Println("User deleted.") |
||||
|
} |
||||
|
|
||||
|
// Delete test realm.
|
||||
|
{ |
||||
|
var err = client.DeleteRealm(tstRealm) |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not delete test realm: %v", err) |
||||
|
} |
||||
|
// Check that the realm was deleted.
|
||||
|
{ |
||||
|
var realms, err = client.GetRealms() |
||||
|
if err != nil { |
||||
|
log.Fatalf("could not get realms: %v", err) |
||||
|
} |
||||
|
for _, r := range realms { |
||||
|
if *r.Realm == tstRealm { |
||||
|
log.Fatalf("test realm should be deleted") |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
fmt.Println("Test realm deleted.") |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/* |
||||
|
// GetUser get the represention of the user.
|
||||
|
func (c *Client) GetUser(realmName, userID string) (UserRepresentation, error) { |
||||
|
var resp = UserRepresentation{} |
||||
|
var err = c.get(&resp, url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID)) |
||||
|
return resp, err |
||||
|
} |
||||
|
|
||||
|
// UpdateUser update the user.
|
||||
|
func (c *Client) UpdateUser(realmName, userID string, user UserRepresentation) error { |
||||
|
return c.put(url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID), body.JSON(user)) |
||||
|
} |
||||
|
|
||||
|
// DeleteUser deletes the user.
|
||||
|
func (c *Client) DeleteUser(realmName, userID string) error { |
||||
|
return c.delete(url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID)) |
||||
|
} |
||||
|
|
||||
|
|
||||
|
*/ |
||||
|
|
||||
|
func getKeycloakConfig() *keycloak.Config { |
||||
|
var adr = pflag.String("url", "localhost:8080", "keycloak address") |
||||
|
var username = pflag.String("username", "admin", "keycloak username") |
||||
|
var password = pflag.String("password", "admin", "keycloak password") |
||||
|
pflag.Parse() |
||||
|
|
||||
|
return &keycloak.Config{ |
||||
|
Addr: *adr, |
||||
|
Username: *username, |
||||
|
Password: *password, |
||||
|
Timeout: 10 * time.Second, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var tstUsers = []struct { |
||||
|
firstname string |
||||
|
lastname string |
||||
|
}{ |
||||
|
{"John", "Doe"}, |
||||
|
{"Johnny", "Briggs"}, |
||||
|
{"Karen", "Sutton"}, |
||||
|
{"Cesar", "Mathis"}, |
||||
|
{"Ryan", "Kennedy"}, |
||||
|
{"Kent", "Phillips"}, |
||||
|
{"Loretta", "Curtis"}, |
||||
|
{"Derrick", "Cox"}, |
||||
|
{"Greg", "Wilkins"}, |
||||
|
{"Andy", "Reynolds"}, |
||||
|
{"Toni", "Meyer"}, |
||||
|
{"Joyce", "Sullivan"}, |
||||
|
{"Johanna", "Wells"}, |
||||
|
{"Judith", "Barnett"}, |
||||
|
{"Joanne", "Ward"}, |
||||
|
{"Bethany", "Johnson"}, |
||||
|
{"Maria", "Murphy"}, |
||||
|
{"Mattie", "Quinn"}, |
||||
|
{"Erick", "Robbins"}, |
||||
|
{"Beulah", "Greer"}, |
||||
|
{"Patty", "Wong"}, |
||||
|
{"Gayle", "Garrett"}, |
||||
|
{"Stewart", "Floyd"}, |
||||
|
{"Wilbur", "Schneider"}, |
||||
|
{"Diana", "Logan"}, |
||||
|
{"Eduardo", "Mitchell"}, |
||||
|
{"Lela", "Wells"}, |
||||
|
{"Homer", "Miles"}, |
||||
|
{"Audrey", "Park"}, |
||||
|
{"Rebecca", "Fuller"}, |
||||
|
{"Jeremiah", "Andrews"}, |
||||
|
{"Cedric", "Reyes"}, |
||||
|
{"Lee", "Griffin"}, |
||||
|
{"Ebony", "Knight"}, |
||||
|
{"Gilbert", "Franklin"}, |
||||
|
{"Jessie", "Norman"}, |
||||
|
{"Cary", "Wells"}, |
||||
|
{"Arlene", "James"}, |
||||
|
{"Jerry", "Chavez"}, |
||||
|
{"Marco", "Weber"}, |
||||
|
{"Celia", "Guerrero"}, |
||||
|
{"Faye", "Massey"}, |
||||
|
{"Jorge", "Mccarthy"}, |
||||
|
{"Jennifer", "Colon"}, |
||||
|
{"Angel", "Jordan"}, |
||||
|
{"Bennie", "Hubbard"}, |
||||
|
{"Terrance", "Norris"}, |
||||
|
{"May", "Sharp"}, |
||||
|
{"Glenda", "Hogan"}, |
||||
|
{"Lucia", "Nelson"}, |
||||
|
} |
||||
@ -0,0 +1 @@ |
|||||
|
package keycloak |
||||
@ -0,0 +1,58 @@ |
|||||
|
package keycloak |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
|
||||
|
"gopkg.in/h2non/gentleman.v2/plugins/body" |
||||
|
"gopkg.in/h2non/gentleman.v2/plugins/url" |
||||
|
) |
||||
|
|
||||
|
const ( |
||||
|
userPath = "/auth/admin/realms/:realm/users" |
||||
|
userCountPath = userPath + "/count" |
||||
|
userIDPath = userPath + "/:id" |
||||
|
) |
||||
|
|
||||
|
// GetUsers returns a list of users, filtered according to the query parameters.
|
||||
|
// Parameters: email, first (paging offset, int), firstName, lastName, username,
|
||||
|
// max (maximum result size, default = 100),
|
||||
|
// search (string contained in username, firstname, lastname or email)
|
||||
|
func (c *Client) GetUsers(realmName string, paramKV ...string) ([]UserRepresentation, error) { |
||||
|
if len(paramKV)%2 != 0 { |
||||
|
return nil, fmt.Errorf("the number of key/val parameters should be even") |
||||
|
} |
||||
|
|
||||
|
var resp = []UserRepresentation{} |
||||
|
var plugins = append(createQueryPlugins(paramKV...), url.Path(userPath), url.Param("realm", realmName)) |
||||
|
var err = c.get(&resp, plugins...) |
||||
|
return resp, err |
||||
|
} |
||||
|
|
||||
|
// CreateUser creates the user from its UserRepresentation. The username must be unique.
|
||||
|
func (c *Client) CreateUser(realm string, user UserRepresentation) error { |
||||
|
return c.post(url.Path(userPath), url.Param("realm", realm), body.JSON(user)) |
||||
|
} |
||||
|
|
||||
|
// CountUsers returns the number of users in the realm.
|
||||
|
func (c *Client) CountUsers(realmName string) (int, error) { |
||||
|
var resp = 0 |
||||
|
var err = c.get(&resp, url.Path(userCountPath), url.Param("realm", realmName)) |
||||
|
return resp, err |
||||
|
} |
||||
|
|
||||
|
// GetUser get the represention of the user.
|
||||
|
func (c *Client) GetUser(realmName, userID string) (UserRepresentation, error) { |
||||
|
var resp = UserRepresentation{} |
||||
|
var err = c.get(&resp, url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID)) |
||||
|
return resp, err |
||||
|
} |
||||
|
|
||||
|
// UpdateUser update the user.
|
||||
|
func (c *Client) UpdateUser(realmName, userID string, user UserRepresentation) error { |
||||
|
return c.put(url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID), body.JSON(user)) |
||||
|
} |
||||
|
|
||||
|
// DeleteUser deletes the user.
|
||||
|
func (c *Client) DeleteUser(realmName, userID string) error { |
||||
|
return c.delete(url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID)) |
||||
|
} |
||||
@ -0,0 +1 @@ |
|||||
|
package keycloak |
||||
Loading…
Reference in new issue