@owlnext/heimdall-js-native
Version:
Heimdall API client & utils for react native technologies
195 lines • 8.5 kB
JavaScript
"use strict";
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cipher_1 = __importDefault(require("./crypto/cipher"));
const heimdall_api_1 = __importDefault(require("./api/heimdall-api"));
/**
* Heimdall Client
*/
class Heimdall {
/**
* Heimdall constructor.
*/
constructor() {
/**
* Authenticate the Heimdall client against the Heimdall API.
* @param props AuthenticationPayload values to authenticate the user on the API.
* @return Promise<AuthenticationTokenObject> A promise with the result of the authentication.
* @throws HeimdallError if an error occurs during API transaction.
*/
this.authenticate = (props) => {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const server_env = props.server_environment || Heimdall.SERVER_ENVIRONMENT_PRODUCTION;
const is_server_env_valid = this.validateServerEnvironment(server_env);
if (!is_server_env_valid) {
reject('You must use Heimdall.SERVER_ENVIRONMENT_* as a server_environment property.');
}
this.disconnect();
return this.api.authenticate({
login: props.login,
password: props.password,
application_uuid: props.application_uuid,
server_environment: server_env
}).then((result) => {
resolve(result);
}).catch((error) => {
console.error(error);
});
}));
};
/**
* Reconnects the Heimdall client to the API with JWT refresh data.
* @param props RefreshPayload values to reconnect the client.
* @return Promise<AuthenticationTokenObject | void> JWT data if reconnected, or void on error.
* @throws HeimdallError if an error occurs during API transaction.
*/
this.reconnect = (props) => {
this.disconnect();
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const server_env = props.server_environment || Heimdall.SERVER_ENVIRONMENT_PRODUCTION;
const is_server_env_valid = this.validateServerEnvironment(server_env);
if (!is_server_env_valid) {
reject('You must use Heimdall.SERVER_ENVIRONMENT_* as a server_environment property.');
}
return this.api.refreshWithExternalData({
application_uuid: props.application_uuid,
refresh_token: props.refresh_token,
server_environment: server_env
}).then((result) => {
resolve(result);
});
}));
};
/**
* Checks either if the Heimdall client is authenticated against the API.
* @return boolean true if the client is authenticated, false otherwise.
*/
this.isAuthenticated = () => {
return this.api.isAuthenticated();
};
/**
* Removes all authentication stored credentials to avoid further use of the API.
*/
this.disconnect = () => {
this.api.logout();
};
/**
* Refreshes authentication against Heimdall API
* @return Promise<AuthenticationTokenObject> A promise with the result of the authentication refresh.
* @throws HeimdallError if an error occurs during API transaction.
*/
this.refreshAuthentication = () => {
return this.api.refresh().then((result) => {
return Promise.resolve(result);
});
};
/**
* Creates a user.
* @param props CreateUserPayload properties to create the user.
* @return Promise<UserDetailObject | null> A promise with the details of the user created, or null on error.
* @throws HeimdallError if an error occurs during API transaction.
*/
this.createUser = (props) => {
return this.api
.createUser({
login: props.login,
password: props.password,
email: props.email,
firstName: props.firstName || '',
lastName: props.lastName || ''
})
.then(result => {
return Promise.resolve(result);
});
};
/**
* Creates a key for given groups of users.
* @param name string the name of the group.
* @param members Array<string> An array with users IRIs.
* @return Promise<KeychainDetailObject | null> a promise with the key details, or null on error.
* @throws HeimdallError if an error occurs during API transaction.
*/
this.createGroup = (name, members) => {
return this.api
.createGroup({
name: name,
members: members
})
.then(result => {
return Promise.resolve(result);
});
};
/**
* Creates a key for given groups of users.
* @param groups Array<string> An array with group IRIs.
* @return Promise<KeychainCreationDetailObject | null> a promise with the key details, or null on error.
* @throws HeimdallError if an error occurs during API transaction.
*/
this.createKey = (groups) => {
return this.api
.createKeychain({ groups: groups })
.then(result => {
return Promise.resolve(result);
});
};
/**
* Gets a cipher key.
* @param id string the ID of the keychain to get the cipher key on.
* @return Promise<string> A promise with the cipher key, or null on error.
* @throws HeimdallError if an error occurs during API transaction.
*/
this.getCipherKey = (id) => {
return this.api
.getKeychainCipherKey(id)
.then(result => {
return Promise.resolve(result);
});
};
/**
* Encrypts a message with a cipher key.
* @param message string the message to encrypt.
* @param cipherKey string the cipher key for encryption.
* @return string the encrypted message.
*/
this.encrypt = (message, cipherKey) => {
return this._cipher.encrypt(message, cipherKey);
};
/**
* Decrypts a message with a cipher key.
* @param message string the message to decrypt.
* @param cipherKey string the cipher key for decrypt.
* @return string the decrypted message.
*/
this.decrypt = (message, cipherKey) => {
return this._cipher.decrypt(message, cipherKey);
};
this.validateServerEnvironment = (server_env) => {
return server_env === Heimdall.SERVER_ENVIRONMENT_PRODUCTION ||
server_env === Heimdall.SERVER_ENVIRONMENT_INTEGRATION;
};
this._api = new heimdall_api_1.default();
this._cipher = new cipher_1.default();
}
/**
* Gets the Heimdall API client.
* @return HeimdallApi the Heimdall API client.
*/
get api() {
return this._api;
}
}
exports.default = Heimdall;
Heimdall.SERVER_ENVIRONMENT_PRODUCTION = 'production';
Heimdall.SERVER_ENVIRONMENT_INTEGRATION = 'integration';
//# sourceMappingURL=heimdall.js.map