/// var fs = require("fs-extra"); var os = require("os"); import jamsglobal = require("./jamsglobal"); import jamslogging = require("./jamslogging"); import path = require('path'); export class Token { // #region Properties filename: string; token: string; expire: number; // #endregion // #region Constructor constructor() { } // #endregion // #region Actions setToken(data: any, callback, configuration: any = {}) { var log = new jamslogging.Logging({}, configuration); var global = new jamsglobal.Global(configuration.locale, configuration); var writeOptions = this.getWriteOptions(configuration); var file = this.getFile(configuration); fs.writeFile(file, JSON.stringify(data), writeOptions, cb => { callback(cb); var result = cb == null ? "" : cb; if (cb) return log.log(configuration, result); return console.log(`${global.getText("tokensavesuccess")} ${result}`); }); } setTokenPermissions(file: any, configuration: any = {}) { try { var options = this.getWriteOptions(configuration); fs.chmod(file, options.mode); } catch (ex) { console.log(`Error: Unable to change token file permissions. ${ ex.message}`); } } setTokenSync(data: any, configuration: any = {}): boolean { this.createTokenFolder(configuration); var log = new jamslogging.Logging({}, configuration); var writeOptions = this.getWriteOptions(configuration); var file = this.getFile(configuration); try { fs.writeFileSync(file, JSON.stringify(data), writeOptions); } catch (ex) { log.error(ex.message, configuration.ignoreerror); } var result = this.tokenExists(configuration); return result; } getToken(configuration: any = {}): any { var global = new jamsglobal.Global(configuration.locale, configuration); var file = this.getFile(configuration); var tokenObject = undefined; this.token = ""; this.expire = 0; try { tokenObject = fs.readJsonSync(file, { throws: `${global.getText("tokenfilemissing") }` }); } catch (e) { tokenObject = undefined; } if (tokenObject !== null && tokenObject !== undefined) { this.token = tokenObject.access_token !== null ? tokenObject.access_token : ""; this.expire = tokenObject.expires_in !== null ? tokenObject.expires_in : 0; } return this; } deleteTokenSync(configuration: any = {}) { var file = this.getFile(configuration); var log = new jamslogging.Logging({}, configuration); var removeResult = false; if (this.tokenExists(configuration)) { try { fs.remove(file); } catch (ex) { log.error(ex.message, configuration.ignoreerror); } removeResult = this.tokenExists(configuration); } return removeResult; } createTokenFolder(configuration: any = {}) { var tokenDir = this.getTokenDir(); fs.ensureDirSync(tokenDir); this.setTokenPermissions(tokenDir, configuration); } tokenExists(configuration: any = {}): boolean { var result = true; var tokenObject; try { tokenObject = this.getToken(configuration); if(tokenObject.token === "") { result = false; } } catch (err) { if (err.code === "ENOENT") { result = false; } else { throw(err); } } return result } // #endregion // #region Methods // #endregion // #region Helper Methods getFile(configuration: any) { var filename = configuration.tokenfilename; var fileloc = this.getTokenDir(); return path.join(fileloc, filename); } getTokenDir() { var homedir = os.homedir(); var tokendir = path.join(homedir, ".jams", "Tokens"); return tokendir; } getWriteOptions(configuration) { return { encoding: configuration.encoding, mode: configuration.filemode, flag: configuration.fileflagwrite }; } // #endregion }