Repository: Nerzal/gocloak
Pull request #80 (2019-06-20 10:09:47)

Added GetClientScope Method


Patch:
diff --git a/README.md b/README.md
index 078cbf1..e777b52 100644
--- a/README.md
+++ b/README.md
@@ -137,6 +137,7 @@ type GoCloak interface {
 	DeleteClientScope(accessToken string, realm, scopeID string) error
 
 	GetClient(accessToken string, realm string, clientID string) (*Client, error)
+	GetClientScope(token string, realm string, scopeID string) (*ClientScope, error)
 	GetClientSecret(token string, realm string, clientID string) (*CredentialRepresentation, error)
 	RegenerateClientSecret(token string, realm string, clientID string) (*CredentialRepresentation, error)
 	GetKeyStoreConfig(accessToken string, realm string) (*KeyStoreConfig, error)
diff --git a/client.go b/client.go
index b9b16de..86fb4ab 100644
--- a/client.go
+++ b/client.go
@@ -485,6 +485,21 @@ func (client *gocloak) GetClient(token string, realm string, clientID string) (*
 	return &result, nil
 }
 
+// GetClientScope returns a clientscope
+func (client *gocloak) GetClientScope(token string, realm string, scopeID string) (*ClientScope, error) {
+	var result ClientScope
+
+	resp, err := client.getRequestWithBearerAuth(token).
+		SetResult(&result).
+		Get(client.getAdminRealmURL(realm, "client-scopes", scopeID))
+
+	if err := checkForError(resp, err); err != nil {
+		return nil, err
+	}
+
+	return &result, nil
+}
+
 // GetClientSecret returns a client's secret
 func (client *gocloak) GetClientSecret(token string, realm string, clientID string) (*CredentialRepresentation, error) {
 	var result CredentialRepresentation
diff --git a/gocloak.go b/gocloak.go
index b6ed189..f7f8af5 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -75,6 +75,8 @@ type GoCloak interface {
 
 	// GetClient returns a client
 	GetClient(accessToken string, realm string, clientID string) (*Client, error)
+	// GetClientScope returns a clientscope
+	GetClientScope(token string, realm string, scopeID string) (*ClientScope, error)
 	// GetClientSecret returns a client's secret
 	GetClientSecret(token string, realm string, clientID string) (*CredentialRepresentation, error)
 	// RegenerateClientSecret creates a new client secret returning the updated CredentialRepresentation


Test patch:
diff --git a/client_test.go b/client_test.go
index f50dcce..89816ec 100644
--- a/client_test.go
+++ b/client_test.go
@@ -746,6 +746,33 @@ func TestGocloak_CreateClientScope(t *testing.T) {
 	FailIfErr(t, err, "DeleteClientScope failed")
 }
 
+func TestGocloak_GetClientScope(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+
+	newScope := ClientScope{
+		ID:   "foo",
+		Name: "bar",
+	}
+	err := client.CreateClientScope(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		newScope)
+	FailIfErr(t, err, "CreateClientScope failed")
+
+	// Getting exact client scope
+	createdClientScope, err := client.GetClientScope(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		newScope.ID)
+	FailIfErr(t, err, "GetClientScope failed")
+	t.Logf("Created clientScope : %+v", createdClientScope)
+	// Checking that GetClientScope returns same client scope
+	AssertEquals(t, newScope.ID, createdClientScope.ID)
+}
+
 func TestGocloak_CreateListGetUpdateDeleteClient(t *testing.T) {
 	t.Parallel()
 	cfg := GetConfig(t)