@block-auth.io/blockauth-sdk
Version:
Block-Auth Auth SDK is a SaaS service blockchain based authentication service. It provides a simple and secure way to authenticate users in your application.
306 lines (305 loc) • 10.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockAuthProvider = void 0;
const RpcClient_1 = require("./clients/RpcClient");
const WsClient_1 = require("./clients/WsClient");
const { SHA256 } = require('crypto-js');
/**
* This class allow you to authenticate with your BlockAuth
* wallet and get the data from your user and/or tenant.
*
* examples:
*
* const provider = new BlockAuthProvider();
*
*/
class BlockAuthProvider {
constructor(apiKey = '', apiSecret = '', wsUrl = 'wss://wss.block-auth.io') {
this._rpc = new RpcClient_1.RpcClient();
this._wsc = new WsClient_1.WsClient(wsUrl, true);
this._apiKey = apiKey;
this._apiSecret = apiSecret;
}
/**
* Allow you to connect to the WebSocket API
* for Block-Auth service
* @param waitMs - time to wait for connection ready
* @returns Promise<void>
*/
async connect(waitMs = 1000, url = '') {
// return this._wsc.connect('ws://localhost:9536', waitMs);
return this._wsc.connect(url, waitMs);
}
/**
* Allow you to disconnect from the WebSocket API
* for Block-Auth service
* @returns Promise<void>
*/
async disconnect() {
return this._wsc.close();
}
/**
* Check if the WebSocket API is connected
* to Block-Auth service
* @returns boolean
*/
isConnected() {
return this._wsc.isConnected();
}
/**
* Allow to Signin with your API Key and Secret
* to get the access token and refresh token.
* The access token is valid for 1 hour.
* The refresh token is valid for 2 hours.
* You can use the refresh token to get a new
* access token when the access token is expired using
* the method `refresh()`.
* @param apiKey - your API Key
* @param apiSecret - your API Secret
* @returns Promise<any>
*/
async signin() {
if (!this.isConnected())
throw new Error('Not connected');
// receive once time the event before emit to allow response
return new Promise((resolve) => {
this._wsc.ws.once('sdk:signin', (d) => {
const { success, message } = d;
resolve({ success, message });
if (!success)
return;
this.wsc.onSdkSignin(d);
});
// emit the event
this._wsc.emit('sdk:signin', {
apiKey: this._apiKey,
apiSecret: SHA256(this._apiSecret).toString(),
});
});
}
/**
* Allow to refresh the access token when it's expired.
* @param refreshToken - your refresh token
* @returns Promise<any>
*/
async refresh(refreshToken) {
if (!this.isConnected())
throw new Error('Not connected');
const _refreshToken = refreshToken || this._wsc.refreshToken;
if (!_refreshToken)
throw new Error('No refresh token provided');
// receive once time the event before emit to allow response
return new Promise((resolve, reject) => {
this._wsc.ws.once('sdk:refresh', (d) => {
const { success, message } = d;
if (!success)
return reject(new Error(message));
resolve({ ...message });
this.wsc.onSdkRefresh(d);
});
// emit the event
this._wsc.emit('sdk:refresh', {
refreshToken: _refreshToken,
});
});
}
/**
* Allow to get the data from your user.
* @param success - allow to know if address is signed
* @param message - extended info about signed address
* @returns Promise<any>
*/
async isAddressSigned(address, token) {
if (!this.isConnected())
throw new Error('Not connected');
const _token = token || this._wsc.accessToken;
// receive once time the event before emit to allow response
return new Promise((resolve) => {
this._wsc.ws.once('sdk:sign', (d) => {
const { success, message } = d;
// reject is not used cause we need to return false/true
// if the address is signed
resolve({ success, message });
this.wsc.onSdkSign(d);
});
// emit the event
this._wsc.emit('sdk:sign', {
token: _token,
address,
});
});
}
/**
* Allow to get the data from your app.
* @param token - your access token
* @returns Promise<any>
*/
async app(token) {
if (!this.isConnected())
throw new Error('Not connected');
const _token = token || this._wsc.accessToken;
// receive once time the event before emit to allow response
return new Promise((resolve, reject) => {
this._wsc.ws.once('sdk:app', (d) => {
const { success, message } = d;
if (!success)
return reject(new Error(message));
resolve({ ...message });
this.wsc.onSdkApp(d);
});
// emit the event
this._wsc.emit('sdk:app', {
token: _token,
});
});
}
async user(address, token) {
if (!this.isConnected())
throw new Error('Not connected');
const _token = token || this._wsc.accessToken;
// receive once time the event before emit to allow response
return new Promise((resolve) => {
this._wsc.ws.once('sdk:user', (d) => {
const { success, message } = d;
// reject is not used cause we need to return false/true
resolve({ success, message });
this.wsc.onSdkUser(d);
});
// emit the event
this._wsc.emit('sdk:user', {
token: _token,
address,
});
});
}
/**
* Allow to get a list of users from your app, also
* you can filter by role and/or permission.
* @param token - your access token
* @param data - the data to filter
* @returns Promise<any>
*/
async users(token, data) {
if (!this.isConnected())
throw new Error('Not connected');
const _token = token || this._wsc.accessToken;
// receive once time the event before emit to allow response
return new Promise((resolve, reject) => {
this._wsc.ws.once('sdk:users', (d) => {
const { success, message } = d;
if (!success)
return reject(new Error(message));
resolve({ ...message });
this.wsc.onSdkUsers(d);
});
// emit the event
this._wsc.emit('sdk:users', {
token: _token,
...data,
});
});
}
/**
* Allow to get a list of roles from your app.
* @param token - your access token
* @param data - the data to filter
* @returns Promise<any>
* */
async roles(token, data) {
if (!this.isConnected())
throw new Error('Not connected');
const _token = token || this._wsc.accessToken;
return new Promise((resolve, reject) => {
this._wsc.ws.once('sdk:roles', (d) => {
const { success, message } = d;
if (!success)
return reject(new Error(message));
resolve({ ...message });
this.wsc.onSdkRoles(d);
});
// emit the event
this._wsc.emit('sdk:roles', {
token: _token,
...data,
});
});
}
/**
* Allow to get a list of logs from your app.
* @param token - your access token
* @param data - the data to filter
* @returns Promise<any>
*/
async logs(token, data) {
if (!this.isConnected())
throw new Error('Not connected');
const _token = token || this._wsc.accessToken;
return new Promise((resolve, reject) => {
this._wsc.ws.once('sdk:logs', (d) => {
const { success, message } = d;
if (!success)
return reject(new Error(message));
resolve({ ...message });
this.wsc.onSdkLogs(d);
});
// emit the event
this._wsc.emit('sdk:logs', {
token: _token,
...data,
});
});
}
/**
* Allow to get KYC issuer for a certain user address from your app.
* @param address - the address of the user to be paid by the issuer
* @param data - the data to connect to the KYC issuer
* { operationId, sessionId, extraData }
* @param token - your access token
* @returns Promise<any>
*/
async kycIssuer(address, data, token) {
if (!this.isConnected())
throw new Error('Not connected');
const _token = token || this._wsc.accessToken;
return new Promise((resolve, reject) => {
this._wsc.ws.once('sdk:kyc:issuer', (d) => {
const { success, message } = d;
if (!success)
return reject(new Error(message));
resolve({ ...message });
this.wsc.onSdkKycIssuer(d);
});
// emit the event
this._wsc.emit('sdk:kyc:issuer', {
token: _token,
address,
details: { ...data },
});
});
}
get rpc() {
return this._rpc;
}
get wsc() {
return this._wsc;
}
get socket() {
return this._wsc.ws;
}
get uiConfig() {
return this._wsc.uiConfig;
}
get appUsers() {
return this._wsc.users;
}
get appRoles() {
return this._wsc.roles;
}
get appLogs() {
return this._wsc.logs;
}
get appKycIssuerByAddress() {
return this._wsc.kycIssuerByAddress;
}
}
exports.BlockAuthProvider = BlockAuthProvider;