wechaty-token
Version:
Wechaty Token Based Authentication Manager
125 lines • 4.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WechatyToken = void 0;
const https_1 = __importDefault(require("https"));
const http_1 = __importDefault(require("http"));
const uuid_1 = require("uuid");
const config_js_1 = require("./config.js");
const version_js_1 = require("./version.js");
const retry_policy_js_1 = require("./retry-policy.js");
class WechatyToken {
static VERSION = version_js_1.VERSION;
version() {
return version_js_1.VERSION;
}
/**
* Wechaty Token is a UUID prefixed with the SNI concated with a `_`
*
* @example "uuid_63d6b063-01e5-48b9-86ed-9fa2c05a6930"
*/
static generate(type = 'uuid') {
config_js_1.log.verbose('WechatyToken', 'generate(%s)', type);
return type + '_' + (0, uuid_1.v4)();
}
/**
* Instance
*/
authority;
sni;
token;
constructor(options) {
config_js_1.log.verbose('WechatyToken', 'constructor(%s)', JSON.stringify(options));
if (typeof options === 'string') {
this.token = options;
this.authority = config_js_1.DEFAULT_AUTHORITY;
}
else {
if (!options.authority) {
config_js_1.log.silly('WechatyToken', 'constructor() `options.authority` not set, use the default value "%s"', config_js_1.DEFAULT_AUTHORITY);
}
this.token = options.token;
this.authority = options.authority || config_js_1.DEFAULT_AUTHORITY;
}
const underscoreIndex = this.token.lastIndexOf('_');
if (underscoreIndex > 0) {
this.sni = this.token
.slice(0, underscoreIndex)
.toLowerCase();
}
}
toString() { return this.token; }
discoverApi(url) {
return new Promise((resolve, reject) => {
const httpClient = /^https:\/\//.test(url) ? https_1.default : http_1.default;
httpClient.get(url, function (res) {
if (/^4/.test('' + res.statusCode)) {
/**
* 4XX Not Found: Token service discovery fail: not exist
*/
return resolve(undefined); // 4XX NotFound
}
else if (!/^2/.test(String(res.statusCode))) {
/**
* Non-2XX: unknown error
*/
const e = new Error(`Unknown error: HTTP/${res.statusCode}`);
config_js_1.log.warn('WechatyToken', 'discoverApi() httpClient.get() %s', e.message);
return reject(e);
}
let body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
resolve(body);
});
}).on('error', function (e) {
console.error(e);
reject(new Error(`WechatyToken discover() endpoint<${url}> rejection: ${e}`));
});
});
}
async discover() {
config_js_1.log.verbose('WechatyToken', 'discover() for "%s"', this.token);
const url = `https://${this.authority}/v0/hosties/${this.token}`;
let jsonStr;
try {
jsonStr = await retry_policy_js_1.retryPolicy.execute(() => this.discoverApi(url));
}
catch (e) {
config_js_1.log.warn('WechatyToken', 'discover() retry.execute(discoverApi) fail: %s', e.message);
// console.error(e)
return undefined;
}
/**
* Token service discovery: Not Found
*/
if (!jsonStr) {
return undefined;
}
try {
const result = JSON.parse(jsonStr);
if (result.host && result.port) {
return result;
}
}
catch (e) {
console.error([
`WechatyToken.discover() for "${this.token}"`,
'failed: unable to parse JSON str to object:',
'----- jsonStr START -----',
jsonStr,
'----- jsonStr END -----',
].join('\n'));
console.error(e);
return undefined;
}
config_js_1.log.warn('WechatyToken', 'discover() address is malformed: "%s"', jsonStr);
return undefined;
}
}
exports.WechatyToken = WechatyToken;
//# sourceMappingURL=wechaty-token.js.map