Code Monkey home page Code Monkey logo

keycloak-api's Introduction

Node.js Keycloak API Wrapper

Usage

const KeycloakApi = require('@trigo/keycloak-api');

const config = {
	authorizationEndpoint: 'https://<keycloak>/auth',
	clientId: '<clientId>',
	username: '<username>', // admin user
	password: '<password>',
	
};
const api = new KeycloakApi(config);
await api.waitForKeycloak(); // required 

Features

Ever function requires a function param called tokenProvider. It should return a access token.

Example

const tokenProvider = async () => {
    const res = await api.getToken({
        realm: 'master',
        grantType: 'password',
        clientId: 'admin-cli',
        username: '<username>',
        password: '<password>',
    });
    return res.data.access_token;
};

We will refer to this example in the following function calls.

Realms

createRealm

const {statusCode, data, header} = await api.createRealm({ realm: 'sparta', tokenProvider })

Users

getUsers

Get list of users. Optional query object as key value.

const query = {
	username: 'leonidas',
};

const {statusCode, data, header} = await api.getUsers({ realm: 'sparta', query, tokenProvider });

statusCode contains the HTTP status code. data array of Keycloak Users. header node-fetch style raw headers

createUser

Creates a new user.

const user = {
	username: 'leonidas',
};

const {statusCode, data, header} = await api.createUser({ realm: 'sparta', user, tokenProvider });

statusCode contains the HTTP status code. data the keycloak user object. header node-fetch style raw headers

Groups

getGroups

Returns all keycloak Groups

const {statusCode, data, header} = await api.getGroups({ realm: 'sparta', tokenProvider });

statusCode contains the HTTP status code. data array of keycloak groups. header node-fetch style raw headers

createGroup

Creates a new user group.

const group = {
	name: 'Spartiates',
	attributes: {
		'size': ['300'],
	},
};

const parentGroupId = 300; // Optional

const {statusCode, data, header} = await api.createGroup({ realm: 'sparta', group, parentGroupId, tokenProvider });

statusCode contains the HTTP status code. data the keycloak group object. header node-fetch style raw headers

deleteGroup

Deletes a user group by groupId

const {statusCode} = await api.deleteGroup({ realm: 'sparta', groupId: 300, tokenProvider });

statusCode contains the HTTP status code. header node-fetch style raw headers

makeGroupChildOfGroup

If group exists it sets the parent otherwise it creates the group as a child

const {statusCode} = await api.makeGroupChildOfGroup({ realm: 'sparta', parentGroupId: '42', group: { id: '43', name: 'child group' }, tokenProvider });

statusCode contains the HTTP status code. header node-fetch style raw headers

keycloak-api's People

Contributors

chrishubinger avatar hoeggi avatar markusvo avatar ofkrown avatar wolfgangbeer avatar

Watchers

Thomas Peklak avatar Markus Dulghier avatar  avatar  avatar Slađan Ristić avatar Stefan Rösch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.