@tririga/tri-pull
Version:
A tool for pulling the UX view source files from the TRIRIGA server.
153 lines (134 loc) • 3.77 kB
JavaScript
"use strict";
const http = require("http");
const https = require("https");
const url = require("url");
const SIGNON_PATH = "/p/websignon/signon";
const SIGNOUT_PATH = "/p/websignon/signout";
let authenticatedUser = null;
class TriWebSignon {
constructor(target, user, password, basicAuthUser, basicAuthPassword) {
this.target = target;
this.user = user;
this.password = password;
this.basicAuth =
basicAuthUser && basicAuthPassword
? basicAuthUser + ":" + basicAuthPassword
: null;
this.signonUrl = new url.URL(this.target + SIGNON_PATH);
this.signoutUrl = new url.URL(this.target + SIGNOUT_PATH);
this.protocol = this.signonUrl.protocol === "https:" ? https : http;
}
static isUserAuthenticated() {
return authenticatedUser != null;
}
static getAuthenticatedUser() {
return authenticatedUser;
}
login() {
return new Promise((resolve, reject) => {
const loginData = this.createLoginRequestData();
const options = this.createLoginRequestOptions();
const req = this.protocol.request(options, (resp) => {
resp.on("data", (d) => {});
resp.on("end", () => {
switch (resp.statusCode) {
case 401:
reject(new Error("Invalid username or password."));
break;
case 200:
authenticatedUser = new TriAuthenticatedUser(
this.user,
resp.headers["set-cookie"],
this.basicAuth
);
resolve(authenticatedUser);
break;
default:
reject(
new Error("Unknown error occurred while trying to login.")
);
break;
}
});
});
req.on("error", (e) => {
reject(
new Error(
`Unknown error occurred while trying to login: ${e.message}`
)
);
});
req.end(loginData);
});
}
logout() {
return new Promise((resolve, reject) => {
if (!TriWebSignon.isUserAuthenticated()) {
reject(new Error("No authenticated user."));
}
const options = this.createLogoutRequestOptions();
const req = this.protocol.request(options, (resp) => {
resp.on("data", (d) => {});
resp.on("end", () => {
switch (resp.statusCode) {
case 200:
resolve();
break;
default:
reject(
new Error("Unknown error occurred while trying to logout.")
);
break;
}
});
});
req.end();
});
}
createLoginRequestData() {
return JSON.stringify({
userName: this.user,
password: this.password,
normal: false,
api: true,
});
}
createLoginRequestOptions() {
const options = {
hostname: this.signonUrl.hostname,
port: this.signonUrl.port,
path: this.signonUrl.pathname,
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
if (this.basicAuth) {
options.auth = this.basicAuth;
}
return options;
}
createLogoutRequestOptions() {
const options = {
hostname: this.signoutUrl.hostname,
port: this.signoutUrl.port,
path: this.signoutUrl.pathname,
method: "POST",
headers: {
Cookie: TriWebSignon.getAuthenticatedUser().cookies,
},
};
if (this.basicAuth) {
options.auth = this.basicAuth;
}
return options;
}
}
class TriAuthenticatedUser {
constructor(user, cookies, basicAuth) {
this.user = user;
this.cookies = cookies;
this.basicAuth = basicAuth;
}
}
module.exports = TriWebSignon;