stulz-sso-node-sdk-pub
Version:
<p align="center"> <img alt="STULZ Logo" height="100" src="https://www.stulz.com/typo3conf/ext/siteskin/Resources/Public/Images/Logos/logo.svg" title="STULZ-INDIA"/> </p>
128 lines (127 loc) • 4.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StulzAuthNodeSdk = void 0;
const crypto_js_1 = __importDefault(require("crypto-js"));
class StulzAuthNodeSdk {
constructor(config) {
this.secretKey = 'GFPO][%6z=b|"Ar';
if (!config || !config.serverUrl) {
throw new Error('[StulzAuthNodeSdk] Missing or invalid configuration. Please provide a valid config with "serverUrl".');
}
this.config = config;
}
/**
* Check if the user is currently logged in
*/
async isLoggedIn() {
const url = `${this.config.serverUrl}/api/v1/client/OAuth/logged-in`;
const response = await fetch(url, {
method: 'GET',
headers: {
'sso': this.encrypt({ ...this.config }),
'Content-Type': 'application/json'
},
credentials: 'include'
});
const data = await response.text();
const decrypted = this.decrypt(data);
if (decrypted.statusCode === 200) {
return decrypted;
}
else {
throw new Error(decrypted.message || 'Something went wrong');
}
}
/**
* Get current user detailsconsole.log('');
*/
async getUserInfo(userId) {
const url = `${this.config.serverUrl}/api/v1/client/users/info?userId=${encodeURIComponent(userId)}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'sso': this.encrypt({ ...this.config }),
'Content-Type': 'application/json'
},
credentials: 'include'
});
const data = await response.json();
const decrypted = this.decrypt(data.data);
if (decrypted.statusCode === 200) {
return decrypted;
}
else {
throw new Error(decrypted.message || 'Something went wrong');
}
}
/**
* Get current user Roles
*/
async getUserRole(userId) {
const url = `${this.config.serverUrl}/api/v1/client/users/userPermissions?userId=${encodeURIComponent(userId)}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'sso': this.encrypt({ ...this.config }),
'Content-Type': 'application/json'
},
credentials: 'include'
});
const data = await response.json();
const decrypted = this.decrypt(data.data);
if (decrypted.statusCode === 200) {
return decrypted;
}
else {
throw new Error(decrypted.message || 'Something went wrong');
}
}
/**
* Encrypt config request using AES
*/
encrypt(data) {
const stringified = JSON.stringify(data);
return crypto_js_1.default.AES.encrypt(stringified, this.secretKey).toString();
}
/**
* Decrypt the received response
*/
decrypt(cipherText) {
const bytes = crypto_js_1.default.AES.decrypt(cipherText, this.secretKey);
const decrypted = bytes.toString(crypto_js_1.default.enc.Utf8);
try {
return JSON.parse(decrypted);
}
catch {
return decrypted;
}
}
/**
* Get current user detailsconsole.log('');
*/
async getDepartmentByRoleCode(roleCode) {
const encodedRoleCode = roleCode ? encodeURIComponent(roleCode) : 'all';
const url = `${this.config.serverUrl}/api/v1/client/department/list?roleCode=${encodedRoleCode}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'sso': this.encrypt({ ...this.config }),
'Content-Type': 'application/json'
},
credentials: 'include'
});
const data = await response.json();
const decrypted = this.decrypt(data.data);
console.log("Decrypted response:", decrypted);
if (decrypted && Array.isArray(decrypted.departments)) {
return decrypted;
}
else {
throw new Error(decrypted.message || 'Invalid department data');
}
}
}
exports.StulzAuthNodeSdk = StulzAuthNodeSdk;