UNPKG

@daedalus/wso2

Version:

This is a set of tools to help connect and manage interactions with various WSO2 products. Please see README.MD for more details.

169 lines (154 loc) 4.34 kB
const https = require("https"); const querystring = require("querystring"); const axios = require("axios").create({ httpsAgent: new https.Agent({ rejectUnauthorized: false }), proxy: false }); const serviceResource = "services/"; /* data.body = { username: '', password: '' } */ const authenticate = (data, callback) => { const endpoint = serviceResource + "AuthenticationAdmin/login"; const url = data.server.url + endpoint; const body = querystring.stringify(data.auth); //console.log(body); let options = { auth: data.auth }; axios .post(url, body) .then(response => { let cookie = response.headers["set-cookie"] && response.headers["set-cookie"].length > 0 ? response.headers["set-cookie"][0].split(';')[0].trim() : ''; callback(null, cookie); }) .catch(error => { if (error.response && error.response.data) { callback(null, { success: false, msg: error.response.data}); } else { callback(null, { success: false, msg: 'Uknown Error from Axios'}); } }); }; /* data.body = { userName: '', password: '', roles: '' } */ const addUser = (data, callback) => { const endpoint = serviceResource + "UserAdmin/addUser"; const url = data.server.url + endpoint; const body = querystring.stringify(data.body); authenticate(data, (err, cookie) => { if (err) callback(err, null); else { let options = { headers: { Cookie: cookie, Accept: 'application/xml' } }; axios .post(url, body, options) .then(response => { switch(response.status){ case 200: { callback(null, { success: true, msg: "User added successfully" }); break; } default:{ callback(null, { success: false, msg: "User add failed" }); break; } } }) .catch(error => { if (error.response && error.response.data) { callback(null, { success: false, msg: error.response.data}); } else { callback(null, { success: false, msg: 'Uknown Error from Axios'}); } }); } }); }; /* data.body = { userName: '' } */ const deleteUser = (data, callback) => { const endpoint = serviceResource + "UserAdmin/deleteUser"; const url = data.server.url + endpoint; const body = querystring.stringify(data.body); authenticate(data, (err, cookie) => { if (err) callback(err, null); else { let options = { headers: { Cookie: cookie, Accept: 'application/xml' } }; axios .post(url, body, options) .then(response => { //console.log(response.status); switch(response.status){ case 200: { callback(null, { success: true, msg: "User deleted successfully" }); break; } default:{ callback(null, { success: false, msg: "User delete failed" }); break; } } }) .catch(error => { if (error.response && error.response.data) { callback(null, { success: false, msg: error.response.data}); } else { callback(null, { success: false, msg: 'Uknown Error from Axios'}); } }); } }); }; /* data.body = { roleName: '', permissions: '' } */ const addRole = (data, callback) => { const endpoint = serviceResource + "UserAdmin/addRole"; const url = data.server.url + endpoint; callback(null, {success: false, msg: "method not implemented"}); }; /* data.body = { roleName: '' } */ const deleteRole = (data, callback) => { const endpoint = serviceResource + "UserAdmin/deleteRole"; const url = data.server.url + endpoint; callback(null, {success: false, msg: "method not implemented"}) }; module.exports = { authenticate, addUser, addRole, deleteUser, deleteRole };