Browse Source

Add routes for clients

master
Johan Droz 8 years ago
parent
commit
e15e6573f7
  1. 6
      client.go
  2. 41
      clients.go
  3. 49
      clients_test.go
  4. 2
      definitions.go

6
client.go

@ -43,9 +43,9 @@ func New(config Config) (*Client, error) {
} }
} }
if u.Scheme != "http" { // if u.Scheme != "http" {
return nil, fmt.Errorf("protocol not supported, your address must start with http://, not %v", u.Scheme) // return nil, fmt.Errorf("protocol not supported, your address must start with http://, not %v", u.Scheme)
} // }
var httpClient = gentleman.New() var httpClient = gentleman.New()
{ {

41
clients.go

@ -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
}

49
clients_test.go

@ -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)
}

2
definitions.go

@ -130,7 +130,7 @@ type ClientRepresentation struct {
NodeReRegistrationTimeout *int32 `json:"nodeReRegistrationTimeout,omitempty"` NodeReRegistrationTimeout *int32 `json:"nodeReRegistrationTimeout,omitempty"`
NotBefore *int32 `json:"notBefore,omitempty"` NotBefore *int32 `json:"notBefore,omitempty"`
Protocol *string `json:"protocol,omitempty"` Protocol *string `json:"protocol,omitempty"`
ProtocolMappers *ProtocolMapperRepresentation `json:"protocolMappers,omitempty"` ProtocolMappers *[]ProtocolMapperRepresentation `json:"protocolMappers,omitempty"`
PublicClient *bool `json:"publicClient,omitempty"` PublicClient *bool `json:"publicClient,omitempty"`
RedirectUris *[]string `json:"redirectUris,omitempty"` RedirectUris *[]string `json:"redirectUris,omitempty"`
RegisteredNodes *map[string]interface{} `json:"registeredNodes,omitempty"` RegisteredNodes *map[string]interface{} `json:"registeredNodes,omitempty"`

Loading…
Cancel
Save