4 changed files with 130 additions and 40 deletions
@ -0,0 +1,41 @@ |
|||||
|
package keycloak |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
|
||||
|
"gopkg.in/h2non/gentleman.v2/plugins/url" |
||||
|
) |
||||
|
|
||||
|
const ( |
||||
|
clientsPath = "/auth/admin/realms/:realm/clients" |
||||
|
clientIDPath = clientsPath + "/:id" |
||||
|
clientSecret = clientsPath + "/client-secret" |
||||
|
) |
||||
|
|
||||
|
// GetClients returns a list of clients belonging to the realm.
|
||||
|
// Parameters: clientId (filter by clientId),
|
||||
|
// viewableOnly (filter clients that cannot be viewed in full by admin, default="false")
|
||||
|
func (c *Client) GetClients(realmName string, paramKV ...string) ([]ClientRepresentation, error) { |
||||
|
if len(paramKV)%2 != 0 { |
||||
|
return nil, fmt.Errorf("the number of key/val parameters should be even") |
||||
|
} |
||||
|
|
||||
|
var resp = []ClientRepresentation{} |
||||
|
var plugins = append(createQueryPlugins(paramKV...), url.Path(clientsPath), url.Param("realm", realmName)) |
||||
|
var err = c.get(&resp, plugins...) |
||||
|
return resp, err |
||||
|
} |
||||
|
|
||||
|
// GetClient get the representation of the client. idClient is the id of client (not client-id).
|
||||
|
func (c *Client) GetClient(realmName, idClient string) (ClientRepresentation, error) { |
||||
|
var resp = ClientRepresentation{} |
||||
|
var err = c.get(&resp, url.Path(clientIDPath), url.Param("realm", realmName), url.Param("id", idClient)) |
||||
|
return resp, err |
||||
|
} |
||||
|
|
||||
|
// GetSecret get the client secret. idClient is the id of client (not client-id).
|
||||
|
func (c *Client) GetSecret(realmName, idClient string) (CredentialRepresentation, error) { |
||||
|
var resp = CredentialRepresentation{} |
||||
|
var err = c.get(&resp, url.Path(clientSecret), url.Param("realm", realmName), url.Param("id", idClient)) |
||||
|
return resp, err |
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
package keycloak |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"testing" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"github.com/stretchr/testify/require" |
||||
|
) |
||||
|
|
||||
|
func initTest(t *testing.T) *Client { |
||||
|
var config = Config{ |
||||
|
Addr: "http://127.0.0.1", |
||||
|
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 TestCreateRealm(t *testing.T) { |
||||
|
var client = initTest(t) |
||||
|
var clients, err = client.GetClients("master") |
||||
|
for i, c := range clients { |
||||
|
fmt.Println(i, *(c.Id), *c.ClientId) |
||||
|
} |
||||
|
assert.Nil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestGetClient(t *testing.T) { |
||||
|
var client = initTest(t) |
||||
|
var c, err = client.GetClient("318ab6db-c056-4d2f-b4f6-c0b585ee45b3", "master") |
||||
|
fmt.Println(*(c.Id), *c.ClientId, c.Secret) |
||||
|
assert.Nil(t, err) |
||||
|
} |
||||
|
|
||||
|
func TestGetSecret(t *testing.T) { |
||||
|
var client = initTest(t) |
||||
|
var c, err = client.GetSecret("318ab6db-c056-4d2f-b4f6-c0b585ee45b3", "master") |
||||
|
fmt.Println(*(c.Value)) |
||||
|
assert.Nil(t, err) |
||||
|
} |
||||
Loading…
Reference in new issue