Skip to content

Commit

Permalink
Merge pull request #267 from cyralinc/feature/add-idp-import-config
Browse files Browse the repository at this point in the history
Add ImportIdentityProviderConfig method
  • Loading branch information
Nerzal authored Mar 27, 2021
2 parents 0bd22a7 + c526c82 commit 485eeac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
20 changes: 20 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,26 @@ func (client *gocloak) DeleteIdentityProvider(ctx context.Context, token, realm,
return checkForError(resp, err, errMessage)
}

// ImportIdentityProviderConfig parses and returns the identity provider config at a given URL
func (client *gocloak) ImportIdentityProviderConfig(ctx context.Context, token, realm, fromURL, providerID string) (map[string]string, error) {
const errMessage = "could not import config"

result := make(map[string]string)
resp, err := client.getRequestWithBearerAuth(ctx, token).
SetResult(&result).
SetBody(map[string]string{
"fromUrl": fromURL,
"providerId": providerID,
}).
Post(client.getAdminRealmURL(realm, "identity-provider", "import-config"))

if err := checkForError(resp, err, errMessage); err != nil {
return nil, err
}

return result, nil
}

// ------------------
// Protection API
// ------------------
Expand Down
37 changes: 37 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5308,3 +5308,40 @@ func TestGocloak_GetClientsWithPagination(t *testing.T) {
require.NoError(t, err)
require.Equal(t, max, len(clients))
}

func TestGocloak_ImportIdentityProviderConfig(t *testing.T) {
t.Parallel()
cfg := GetConfig(t)
client := NewClientWithDebug(t)
token := GetAdminToken(t, client)

actual, err := client.ImportIdentityProviderConfig(
context.Background(),
token.AccessToken,
cfg.GoCloak.Realm,
"https://accounts.google.com/.well-known/openid-configuration",
"oidc")

require.NoError(t, err, "ImportIdentityProviderConfig failed")

expected := map[string]string{
"userInfoUrl": "https://openidconnect.googleapis.com/v1/userinfo",
"validateSignature": "true",
"tokenUrl": "https://oauth2.googleapis.com/token",
"authorizationUrl": "https://accounts.google.com/o/oauth2/v2/auth",
"jwksUrl": "https://www.googleapis.com/oauth2/v3/certs",
"issuer": "https://accounts.google.com",
"useJwksUrl": "true",
}

require.Len(
t, actual, len(expected),
"ImportIdentityProviderConfig should return exactly %d fields", len(expected))

for expectedKey, expectedVal := range expected {
require.Equal(
t, expectedVal, actual[expectedKey],
"ImportIdentityProviderConfig should return %q for %q, but returned %q",
expectedVal, expectedKey, actual[expectedKey])
}
}
2 changes: 2 additions & 0 deletions gocloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ type GoCloak interface {
UpdateIdentityProvider(ctx context.Context, token, realm, alias string, providerRep IdentityProviderRepresentation) error
// DeleteIdentityProvider deletes the identity provider in a realm
DeleteIdentityProvider(ctx context.Context, token, realm, alias string) error
// ImportIdentityProviderConfig parses and returns the identity provider config at a given URL
ImportIdentityProviderConfig(ctx context.Context, token, realm, fromURL, providerID string) (map[string]string, error)

// *** Protection API ***
// GetResource returns a client's resource with the given id, using access token from client
Expand Down

0 comments on commit 485eeac

Please sign in to comment.