Koibanx Auth SDK
Auth SDK based in module Auth
Description
The Module Auth handles:
- Apps: Allows Koibanx users to manage apps.
- Permissions: Manage roles within an app
- Users: Manage users within an app
- User Access: Provides the permissions to give access to users.
SDK Documentation
Feast yourself
Installation
npm install @koibanx/auth-sdk
NOTE: you must have the npm token in your .npmrc file
Initialization
Node
Using ES6 import
import AuthSdk from '@koibanx/auth-sdk';
const auth = AuthSdk({
baseURL: 'http://your-url',
});
With require
exports.__esModule = true;
const AuthSdk = require('@koibanx/auth-sdk')["default"];
const auth = AuthSdk({
baseURL: 'http://your-url',
});
Types
- Typescript (
@koibanx/auth-sdk/dist/index.d.ts
)
Examples
Using ES6 import
import AuthSdk from "@koibanx/auth-sdk";
const auth = AuthSdk({
baseURL: 'http://your-url',
apiKey: 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1',
secret: 'b2b2b2b3-b2b2-b2b2-b2b2-b2b2b3b2b2b3',
Authorization: 'Basic abC3abC3abC3abC3abC3abC'
})
const catchError = (err, modulo) => {
console.log('Modulo: ', modulo);
console.log('details: ', err.details);
console.log('shortMessage: ', err.message);
console.log('errorCode: ', err.code);
}
const random = Math.floor(Math.random() * 1000) + 1;
const username = `test${random}`;
Users
authSDK.users.listUsers().then((res) => {
console.log(JSON.stringify(res.results));
}).catch((e) => catchError(e, 'listUsers'));
authSDK.users.createUser({
username,
password: '1234',
tags: [
'example',
],
}).then((res) => {
authSDK.users.editUser({
username: res.username,
password: 'test',
}).then((resEdit) => {
authSDK.users.deleteUser(resEdit.username)
.then((resDelete) => {
authSDK.users.recoverUser({
app: {
id: resDelete.profile.app || 'test',
},
username: resDelete.username,
}).then((resRecover) => {
authSDK.users.getUser(resRecover.username)
.then((resGet) => console.log('getUser', JSON.stringify(resGet)))
.catch((e) => catchError(e, 'getUser'));
}).catch((e) => catchError(e, 'recoverUser'));
}).catch((e) => catchError(e, 'deleteUser'));
}).catch((e) => catchError(e, 'editUser'));
authSDK.users.assignRole({
roleName: 'admin',
username: res.username,
})
.then((resAssign) => console.log('assignRole', JSON.stringify(resAssign)))
.catch((e) => catchError(e, 'assignRole'));
}).catch((e) => catchError(e, 'createUser'));
Apps
authSDK.apps.createApp({
name: username,
}).then((resCreate) => console.log('createApp', JSON.stringify(resCreate)))
.catch((e) => catchError(e, 'createApp'));
authSDK.apps.editApp({
name: username,
resetKeys: false,
}).then((resEdit) => console.log('editApp', JSON.stringify(resEdit)))
.catch((e) => catchError(e, 'editApp'));
authSDK.apps.listApps()
.then((resList) => console.log('listApps', JSON.stringify(resList)))
.catch((e) => catchError(e, 'listApps'));
authSDK.apps.viewAccessLogs({
app: {
id: '63289b05ab19a739204c346a',
},
username: 'test21',
page: 1,
limit: 100,
}).then((resLogs) => console.log('viewAccessLogs', JSON.stringify(resLogs.results)))
.catch((e) => catchError(e, 'viewAccessLogs'));
authSDK.apps.createApp({
name: username,
}).then((resCreate) => {
authSDK.apps.editApp({
name: resCreate.name,
resetKeys: true,
}).then((resEdit) => console.log('editApp', JSON.stringify(resEdit)))
.catch((e) => catchError(e, 'editApp'));
}).catch((e) => catchError(e, 'createApp'));
Roles
authSDK.roles.createRole({
roleName: username,
}).then((resCreate) => console.log('createRole', JSON.stringify(resCreate)))
.catch((e) => catchError(e, 'createRole'));
authSDK.roles.deleteRole({
roleName: username,
}).then((resDelete) => console.log('deleteRole', JSON.stringify(resDelete)))
.catch((e) => catchError(e, 'deleteRole'));
authSDK.roles.getRole({
roleName: username,
}).then((resGet) => console.log('getRole', JSON.stringify(resGet)))
.catch((e) => catchError(e, 'getRole'));
authSDK.roles.listRoles()
.then((resList) => console.log('listRoles', JSON.stringify(resList)))
.catch((e) => catchError(e, 'listRoles'));
authSDK.roles.createRole({
roleName: username,
}).then((resCreate) => {
authSDK.roles.deleteRole({
roleName: resCreate.name,
}).then((resDelete) => console.log('deleteRole', JSON.stringify(resDelete)))
.catch((e) => catchError(e, 'deleteRole'));
}).catch((e) => catchError(e, 'createRole'));
authSDK.roles.createRole({
roleName: username,
}).then((resCreate) => {
authSDK.roles.getRole({
roleName: resCreate.name,
}).then((resGet) => console.log('getRole', JSON.stringify(resGet)))
.catch((e) => catchError(e, 'getRole'));
}).catch((e) => catchError(e, 'createRole'));
authSDK.roles.addPermissionToRole({
permissionName: 'getUser',
roleName: 'test278',
}).then((resAdd) => console.log('addPermissionToRole', JSON.stringify(resAdd)))
.catch((e) => catchError(e, 'addPermissionToRole'));
authSDK.roles.removePermissionFromRole({
permissionName: 'getUser',
roleName: 'test278',
}).then((resRm) => console.log('removePermissionFromRole', JSON.stringify(resRm)))
.catch((e) => catchError(e, 'removePermissionFromRole'));
Permissions
authSDK.permissions.listPermissions({
limit: 1,
page: 1,
}).then((resList) => console.log('listPermissions', JSON.stringify(resList)))
.catch((e) => catchError(e, 'listPermissions'));
authSDK.permissions.createPermission({
permissionName: username,
}).then((resCreate) => console.log('createPermission', JSON.stringify(resCreate)))
.catch((e) => catchError(e, 'createPermission'));
authSDK.permissions.deletePermission({
permissionName: username,
}).then((resDel) => console.log('deletePermission', JSON.stringify(resDel)))
.catch((e) => catchError(e, 'deletePermission'));
authSDK.permissions.createPermission({
permissionName: username,
}).then((resCreate) => {
console.log(JSON.stringify(resCreate));
authSDK.permissions.deletePermission({
permissionName: resCreate.name,
}).then((resDel) => console.log('deletePermission', JSON.stringify(resDel)))
.catch((e) => catchError(e, 'deletePermission'));
}).catch((e) => catchError(e, 'createPermission'));
User Access
authSDK.userAccess.getPublicKeys()
.then((resList) => console.log('getPublicKeys', JSON.stringify(resList)))
.catch((e) => catchError(e, 'getPublicKeys'));
authSDK.userAccess.createTemporalToken()
.then((resList) => console.log('createTemporalToken', JSON.stringify(resList)))
.catch((e) => catchError(e, 'createTemporalToken'));
const expiryDate = new Date(Date.now() + 2 * (60 * 60 * 1000));
authSDK.userAccess.createTemporalPassword({
expiration: expiryDate,
username: 'superadmin',
oneTime: true,
})
.then((resList) => console.log('createTemporalToken', JSON.stringify(resList.temporalPassword)))
.catch((e) => catchError(e, 'createTemporalToken'));