From 7bc48e5b7bc54e78d84330dc47ad04e4d8316142 Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 18 Feb 2019 13:38:30 -0500 Subject: [PATCH 1/6] Added identity provider mapper representation --- src/defs/identityProviderMapperRepresentation.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/defs/identityProviderMapperRepresentation.ts diff --git a/src/defs/identityProviderMapperRepresentation.ts b/src/defs/identityProviderMapperRepresentation.ts new file mode 100644 index 00000000..c7223087 --- /dev/null +++ b/src/defs/identityProviderMapperRepresentation.ts @@ -0,0 +1,11 @@ +/** + * https://www.keycloak.org/docs-api/4.1/rest-api/#_identityprovidermapperrepresentation + */ + +export default interface IdentityProviderMapperRepresentation { + config?: any; + id?: string; + identityProviderAlias?: string; + identityProviderMapper?: string; + name?: string; +} From b3ba2fe66f68e0c65f0749e60f99b452e994562e Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 18 Feb 2019 14:08:47 -0500 Subject: [PATCH 2/6] Added identity provider mappers API calls --- src/resources/identityProviders.ts | 70 ++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/src/resources/identityProviders.ts b/src/resources/identityProviders.ts index 09a99270..868eb509 100644 --- a/src/resources/identityProviders.ts +++ b/src/resources/identityProviders.ts @@ -1,5 +1,6 @@ import Resource from './resource'; import IdentityProviderRepresentation from '../defs/identityProviderRepresentation'; +import IdentityProviderMapperRepresentation from '../defs/identityProviderMapperRepresentation'; import {KeycloakAdminClient} from '../client'; export class IdentityProviders extends Resource<{realm?: string}> { @@ -10,10 +11,12 @@ export class IdentityProviders extends Resource<{realm?: string}> { public find = this.makeRequest({ method: 'GET', + path: '/instances', }); public create = this.makeRequest({ method: 'POST', + path: '/instances', }); public findOne = this.makeRequest< @@ -21,7 +24,7 @@ export class IdentityProviders extends Resource<{realm?: string}> { IdentityProviderRepresentation >({ method: 'GET', - path: '/{alias}', + path: '/instances/{alias}', urlParamKeys: ['alias'], catchNotFound: true, }); @@ -32,19 +35,78 @@ export class IdentityProviders extends Resource<{realm?: string}> { void >({ method: 'PUT', - path: '/{alias}', + path: '/instances/{alias}', urlParamKeys: ['alias'], }); public del = this.makeRequest<{alias: string}, void>({ method: 'DELETE', - path: '/{alias}', + path: '/instances/{alias}', + urlParamKeys: ['alias'], + }); + + + public findFactory = this.makeRequest< + {providerId: string}, + any + >({ + method: 'GET', + path: '/providers/{providerId}', + urlParamKeys: ['providerId'], + }); + + + public findMappers = this.makeRequest< + {alias: string}, + IdentityProviderMapperRepresentation[] + >({ + method: 'GET', + path: '/instances/{alias}/mappers', + urlParamKeys: ['alias'], + }); + + public createMapper = this.makeRequest< + { + alias: string; + identityProviderMapper: IdentityProviderMapperRepresentation; + }, + void + >({ + method: 'POST', + path: '/instances/{alias}/mappers', + urlParamKeys: ['alias'], + payloadKey: 'identityProviderMapper', + }); + + public updateMapper = this.makeUpdateRequest< + {alias: string, id: string}, + IdentityProviderRepresentation, + void + >({ + method: 'PUT', + path: '/instances/{alias}/mappers/{id}', + urlParamKeys: ['alias', 'id'], + }); + + public delMapper = this.makeRequest<{alias: string, id: string}, void>({ + method: 'DELETE', + path: '/instances/{alias}/mappers/{id}', + urlParamKeys: ['alias', 'id'], + }); + + + public findMapperTypes = this.makeRequest< + {alias: string}, + IdentityProviderMapperRepresentation[] + >({ + method: 'GET', + path: '/instances/{alias}/mapper-types', urlParamKeys: ['alias'], }); constructor(client: KeycloakAdminClient) { super(client, { - path: '/admin/realms/{realm}/identity-provider/instances', + path: '/admin/realms/{realm}/identity-provider', getUrlParams: () => ({ realm: client.realmName, }), From 9e892141866485d690294cde2c4311c0672d8ef4 Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 18 Feb 2019 16:05:15 -0500 Subject: [PATCH 3/6] Added unit tests --- src/resources/identityProviders.ts | 14 ++---- test/idp.spec.ts | 77 +++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/src/resources/identityProviders.ts b/src/resources/identityProviders.ts index 868eb509..0b287fee 100644 --- a/src/resources/identityProviders.ts +++ b/src/resources/identityProviders.ts @@ -45,17 +45,12 @@ export class IdentityProviders extends Resource<{realm?: string}> { urlParamKeys: ['alias'], }); - - public findFactory = this.makeRequest< - {providerId: string}, - any - >({ + public findFactory = this.makeRequest<{providerId: string}, any>({ method: 'GET', path: '/providers/{providerId}', urlParamKeys: ['providerId'], }); - public findMappers = this.makeRequest< {alias: string}, IdentityProviderMapperRepresentation[] @@ -79,8 +74,8 @@ export class IdentityProviders extends Resource<{realm?: string}> { }); public updateMapper = this.makeUpdateRequest< - {alias: string, id: string}, - IdentityProviderRepresentation, + {alias: string; id: string}, + IdentityProviderMapperRepresentation, void >({ method: 'PUT', @@ -88,13 +83,12 @@ export class IdentityProviders extends Resource<{realm?: string}> { urlParamKeys: ['alias', 'id'], }); - public delMapper = this.makeRequest<{alias: string, id: string}, void>({ + public delMapper = this.makeRequest<{alias: string; id: string}, void>({ method: 'DELETE', path: '/instances/{alias}/mappers/{id}', urlParamKeys: ['alias', 'id'], }); - public findMapperTypes = this.makeRequest< {alias: string}, IdentityProviderMapperRepresentation[] diff --git a/test/idp.spec.ts b/test/idp.spec.ts index db0ec228..c2f5e968 100644 --- a/test/idp.spec.ts +++ b/test/idp.spec.ts @@ -25,18 +25,48 @@ describe('Identity providers', function() { providerId: 'saml', }); this.currentIdpAlias = alias; + + // create idp mapper + const mapper = { + name: 'First Name', + identityProviderAlias: this.currentIdpAlias, + identityProviderMapper: 'saml-user-attribute-idp-mapper', + config: {}, + }; + await this.kcAdminClient.identityProviders.createMapper({ + alias: this.currentIdpAlias, + identityProviderMapper: mapper, + }); }); after(async () => { + const idpMapper = await this.kcAdminClient.identityProviders.findMappers({ + alias: this.currentIdpAlias, + }); + + const idpMapperId = idpMapper[0].id; + await this.kcAdminClient.identityProviders.delMapper({ + alias: this.currentIdpAlias, + id: idpMapperId, + }); + + const idpMapperUpdated = await this.kcAdminClient.identityProviders.findMappers( + { + alias: this.currentIdpAlias, + }, + ); + await this.kcAdminClient.identityProviders.del({ alias: this.currentIdpAlias, }); - // check deleted const idp = await this.kcAdminClient.identityProviders.findOne({ alias: this.currentIdpAlias, }); + + // check idp and idp mapper deleted expect(idp).to.be.null; + expect(idpMapperUpdated.length).to.equal(0); }); it('list idp', async () => { @@ -75,4 +105,49 @@ describe('Identity providers', function() { displayName: 'test', }); }); + + it('list idp factory', async () => { + const idpFactory = await this.kcAdminClient.identityProviders.findFactory({ + providerId: 'saml', + }); + + expect(idpFactory).to.include({ + id: 'saml', + }); + }); + + it('get an idp mapper', async () => { + const mappers = await this.kcAdminClient.identityProviders.findMappers({ + alias: this.currentIdpAlias, + }); + expect(mappers.length).to.be.least(1); + }); + + it('update an idp mapper', async () => { + const idpMapper = await this.kcAdminClient.identityProviders.findMappers({ + alias: this.currentIdpAlias, + }); + const idpMapperId = idpMapper[0].id; + + await this.kcAdminClient.identityProviders.updateMapper( + {alias: this.currentIdpAlias, id: idpMapperId}, + { + id: idpMapperId, + identityProviderAlias: this.currentIdpAlias, + identityProviderMapper: 'saml-user-attribute-idp-mapper', + config: { + 'user.attribute': 'firstName', + }, + }, + ); + + const updatedIdpMappers = await this.kcAdminClient.identityProviders.findMappers( + { + alias: this.currentIdpAlias, + }, + ); + + const userAttribute = updatedIdpMappers[0].config['user.attribute']; + expect(userAttribute).to.equal('firstName'); + }); }); From 6cd1d011fa122d68953acdc4fbbde2011ec0a2ff Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 18 Feb 2019 16:19:28 -0500 Subject: [PATCH 4/6] Added 'findOneMapper' fct --- src/resources/identityProviders.ts | 10 ++++++++++ test/idp.spec.ts | 10 ++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/resources/identityProviders.ts b/src/resources/identityProviders.ts index 0b287fee..35611ad4 100644 --- a/src/resources/identityProviders.ts +++ b/src/resources/identityProviders.ts @@ -60,6 +60,16 @@ export class IdentityProviders extends Resource<{realm?: string}> { urlParamKeys: ['alias'], }); + public findOneMapper = this.makeRequest< + {alias: string; id: string}, + IdentityProviderMapperRepresentation + >({ + method: 'GET', + path: '/instances/{alias}/mappers/{id}', + urlParamKeys: ['alias', 'id'], + catchNotFound: true, + }); + public createMapper = this.makeRequest< { alias: string; diff --git a/test/idp.spec.ts b/test/idp.spec.ts index c2f5e968..92fd8f33 100644 --- a/test/idp.spec.ts +++ b/test/idp.spec.ts @@ -50,9 +50,10 @@ describe('Identity providers', function() { id: idpMapperId, }); - const idpMapperUpdated = await this.kcAdminClient.identityProviders.findMappers( + const idpMapperUpdated = await this.kcAdminClient.identityProviders.findOneMapper( { alias: this.currentIdpAlias, + id: idpMapperId, }, ); @@ -66,7 +67,7 @@ describe('Identity providers', function() { // check idp and idp mapper deleted expect(idp).to.be.null; - expect(idpMapperUpdated.length).to.equal(0); + expect(idp).to.be.null; }); it('list idp', async () => { @@ -141,13 +142,14 @@ describe('Identity providers', function() { }, ); - const updatedIdpMappers = await this.kcAdminClient.identityProviders.findMappers( + const updatedIdpMappers = await this.kcAdminClient.identityProviders.findOneMapper( { alias: this.currentIdpAlias, + id: idpMapperId, }, ); - const userAttribute = updatedIdpMappers[0].config['user.attribute']; + const userAttribute = updatedIdpMappers.config['user.attribute']; expect(userAttribute).to.equal('firstName'); }); }); From 37f1afeaab57fadce4436d09d6de6b37961ecb75 Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 18 Feb 2019 16:25:50 -0500 Subject: [PATCH 5/6] Updated the README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 700bd34c..e50d8175 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,13 @@ Demo code: https://github.com/keycloak/keycloak-nodejs-admin-client/blob/master/ - Get the identity provider (`GET /{realm}/identity-provider/instances/{alias}`) - Update the identity provider (`PUT /{realm}/identity-provider/instances/{alias}`) - Delete the identity provider (`DELETE /{realm}/identity-provider/instances/{alias}`) +- Find identity provider factory (`GET /{realm}/identity-provider//providers/{providerId}`) +- Create a new identity provider mapper (`POST /{realm}/identity-provider/instances/{alias}/mappers`) +- Get identity provider mappers (`GET /{realm}/identity-provider/instances/{alias}/mappers`) +- Get the identity provider mapper (`GET /{realm}/identity-provider/instances/{alias}/mappers/{id}`) +- Update the identity provider mapper (`PUT /{realm}/identity-provider/instances/{alias}/mappers/{id}`) +- Delete the identity provider mapper (`DELETE /{realm}/identity-provider/instances/{alias}/mappers/{id}`) +- Find the identity provider mapper types (`GET /{realm}/identity-provider/instances/{alias}/mapper-types`) ### [Component]() From 9fa0e81afab0f25bda9724a22d761bc0d0e882d7 Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 18 Feb 2019 22:46:41 -0500 Subject: [PATCH 6/6] Fixed typos --- README.md | 2 +- test/idp.spec.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e50d8175..4486860c 100644 --- a/README.md +++ b/README.md @@ -204,7 +204,7 @@ Demo code: https://github.com/keycloak/keycloak-nodejs-admin-client/blob/master/ - Get the identity provider (`GET /{realm}/identity-provider/instances/{alias}`) - Update the identity provider (`PUT /{realm}/identity-provider/instances/{alias}`) - Delete the identity provider (`DELETE /{realm}/identity-provider/instances/{alias}`) -- Find identity provider factory (`GET /{realm}/identity-provider//providers/{providerId}`) +- Find identity provider factory (`GET /{realm}/identity-provider/providers/{providerId}`) - Create a new identity provider mapper (`POST /{realm}/identity-provider/instances/{alias}/mappers`) - Get identity provider mappers (`GET /{realm}/identity-provider/instances/{alias}/mappers`) - Get the identity provider mapper (`GET /{realm}/identity-provider/instances/{alias}/mappers/{id}`) diff --git a/test/idp.spec.ts b/test/idp.spec.ts index 92fd8f33..42112806 100644 --- a/test/idp.spec.ts +++ b/test/idp.spec.ts @@ -56,6 +56,9 @@ describe('Identity providers', function() { id: idpMapperId, }, ); + + // check idp mapper deleted + expect(idpMapperUpdated).to.be.null; await this.kcAdminClient.identityProviders.del({ alias: this.currentIdpAlias, @@ -65,8 +68,7 @@ describe('Identity providers', function() { alias: this.currentIdpAlias, }); - // check idp and idp mapper deleted - expect(idp).to.be.null; + // check idp deleted expect(idp).to.be.null; });