sendingnetwork-bot-sdk
Version:
TypeScript/JavaScript SDK for SDN bots
81 lines • 2.89 kB
JavaScript
;
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, "");
}
async didPreLogin(address) {
const body = {};
let response;
try {
let tmpClient = this.createTemplateClient();
let queryDidResp = await tmpClient.doRequest("GET", "/_api/client/unstable/address/" + address);
if (queryDidResp["data"].length > 0) {
body["did"] = queryDidResp["data"][0];
}
else {
body["address"] = address;
}
response = await tmpClient.doRequest("POST", "/_api/client/unstable/did/pre_login1", null, body);
}
catch (e) {
throw e;
}
if (!response)
throw new Error("didPreLogin fail");
return response;
}
async didLogin(address, did, message, token, nonce, update_time) {
return await this.didLoginWithAppToken(address, did, message, token, "", nonce, update_time);
}
async didLoginWithAppToken(address, did, message, token, appToken, nonce, update_time) {
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 = await 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