Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #14 from slimovic/add-identity-providers-mappers
Browse files Browse the repository at this point in the history
Add support for identity providers mappers
  • Loading branch information
wwwy3y3 authored Feb 20, 2019
2 parents 5ef21f0 + 9fa0e81 commit be72a6d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]()

Expand Down
11 changes: 11 additions & 0 deletions src/defs/identityProviderMapperRepresentation.ts
Original file line number Diff line number Diff line change
@@ -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;
}
74 changes: 70 additions & 4 deletions src/resources/identityProviders.ts
Original file line number Diff line number Diff line change
@@ -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}> {
Expand All @@ -10,18 +11,20 @@ export class IdentityProviders extends Resource<{realm?: string}> {

public find = this.makeRequest<void, IdentityProviderRepresentation[]>({
method: 'GET',
path: '/instances',
});

public create = this.makeRequest<IdentityProviderRepresentation, void>({
method: 'POST',
path: '/instances',
});

public findOne = this.makeRequest<
{alias: string},
IdentityProviderRepresentation
>({
method: 'GET',
path: '/{alias}',
path: '/instances/{alias}',
urlParamKeys: ['alias'],
catchNotFound: true,
});
Expand All @@ -32,19 +35,82 @@ 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 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;
identityProviderMapper: IdentityProviderMapperRepresentation;
},
void
>({
method: 'POST',
path: '/instances/{alias}/mappers',
urlParamKeys: ['alias'],
payloadKey: 'identityProviderMapper',
});

public updateMapper = this.makeUpdateRequest<
{alias: string; id: string},
IdentityProviderMapperRepresentation,
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,
}),
Expand Down
81 changes: 80 additions & 1 deletion test/idp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,50 @@ 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.findOneMapper(
{
alias: this.currentIdpAlias,
id: idpMapperId,
},
);

// check idp mapper deleted
expect(idpMapperUpdated).to.be.null;

await this.kcAdminClient.identityProviders.del({
alias: this.currentIdpAlias,
});

// check deleted
const idp = await this.kcAdminClient.identityProviders.findOne({
alias: this.currentIdpAlias,
});

// check idp deleted
expect(idp).to.be.null;
});

Expand Down Expand Up @@ -75,4 +108,50 @@ 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.findOneMapper(
{
alias: this.currentIdpAlias,
id: idpMapperId,
},
);

const userAttribute = updatedIdpMappers.config['user.attribute'];
expect(userAttribute).to.equal('firstName');
});
});

0 comments on commit be72a6d

Please sign in to comment.