sendingnetwork-bot-sdk
Version:
TypeScript/JavaScript SDK for SDN bots
96 lines • 3.94 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SDNAuth = void 0;
const SDNClient_1 = require("./SDNClient");
/**
* Functions for interacting with SDN prior to having an access token. Intended
* to be used for logging in/registering to get a SDNClient instance.
*
* By design, this limits the options used to create the SDNClient. To specify
* custom elements to the client, get the access token from the returned client
* and create a new SDNClient instance. Due to the nature of SDN, it is
* also recommended to use the nodeUrl from the generated SDNClient as
* it may be different from that given to the SDNAuth class.
*/
class SDNAuth {
/**
* Creates a new SDNAuth class for creating a SDNClient
* @param {string} nodeUrl The node URL to authenticate against.
*/
constructor(nodeUrl) {
this.nodeUrl = nodeUrl;
// nothing to do
}
/**
* Generate a client with no access token so we can reuse the doRequest
* logic already written.
*/
createTemplateClient() {
return new SDNClient_1.SDNClient(this.nodeUrl, "");
}
didPreLogin(address) {
return __awaiter(this, void 0, void 0, function* () {
const body = {};
let response;
try {
let tmpClient = this.createTemplateClient();
let queryDidResp = yield tmpClient.doRequest("GET", "/_api/client/unstable/address/" + address);
if (queryDidResp["data"].length > 0) {
body["did"] = queryDidResp["data"][0];
}
else {
body["address"] = address;
}
response = yield tmpClient.doRequest("POST", "/_api/client/unstable/did/pre_login1", null, body);
}
catch (e) {
throw e;
}
if (!response)
throw new Error("didPreLogin fail");
return response;
});
}
didLogin(address, did, message, token, nonce, update_time) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.didLoginWithAppToken(address, did, message, token, "", nonce, update_time);
});
}
didLoginWithAppToken(address, did, message, token, appToken, nonce, update_time) {
return __awaiter(this, void 0, void 0, function* () {
const body = {
"identifier": {
"did": did,
"address": address,
"message": message,
"token": token,
"app_token": appToken
},
"type": "m.login.did.identity",
"random_server": nonce,
"updated": update_time,
};
let response;
try {
response = yield this.createTemplateClient().doRequest("POST", "/_api/client/unstable/did/login", null, body);
}
catch (e) {
throw e;
}
if (!response)
throw new Error("didLogin fail");
return response;
});
}
}
exports.SDNAuth = SDNAuth;
//# sourceMappingURL=SDNAuth.js.map