UNPKG

@sneko/ionic-appauth

Version:

Intergration for OpenId/AppAuth-JS into Ionic V3/4/5

135 lines (134 loc) 5.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CordovaSecureStorage = void 0; var tslib_1 = require("tslib"); var cordova_document_1 = require("./cordova-document"); var appauth_1 = require("@openid/appauth"); var secure_storage_1 = require("@ionic-native/secure-storage"); // REQUIRES CORDOVA PLUGINS // cordova-plugin-secure-storage var CordovaSecureStorage = /** @class */ (function (_super) { tslib_1.__extends(CordovaSecureStorage, _super); function CordovaSecureStorage() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.localData = {}; _this.KEYSTORE = "SecretStore"; return _this; } CordovaSecureStorage.prototype.SecureStorageExists = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, cordova_document_1.CordovaDocument.ready()]; case 1: _a.sent(); return [2 /*return*/, secure_storage_1.SecureStorage.create(this.KEYSTORE).then(function () { return true; }, function () { return false; })]; } }); }); }; CordovaSecureStorage.prototype.hasRecord = function (store, key) { return tslib_1.__awaiter(this, void 0, void 0, function () { var keys; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, store.keys()]; case 1: keys = _a.sent(); return [2 /*return*/, (keys.indexOf(key) > -1)]; } }); }); }; CordovaSecureStorage.prototype.getItem = function (name) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, cordova_document_1.CordovaDocument.ready()]; case 1: _a.sent(); return [2 /*return*/, secure_storage_1.SecureStorage.create(this.KEYSTORE).then(function (store) { return store.get(name).catch(function () { return null; }); }) .catch(function () { return _this.getTemp(name); })]; } }); }); }; CordovaSecureStorage.prototype.removeItem = function (name) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, cordova_document_1.CordovaDocument.ready()]; case 1: _a.sent(); return [2 /*return*/, secure_storage_1.SecureStorage.create(this.KEYSTORE).then(function (store) { store.remove(name); }) .catch(function () { _this.removeTemp(name); })]; } }); }); }; CordovaSecureStorage.prototype.setItem = function (name, value) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, cordova_document_1.CordovaDocument.ready()]; case 1: _a.sent(); return [2 /*return*/, secure_storage_1.SecureStorage.create(this.KEYSTORE).then(function (store) { store.set(name, value); }) .catch(function () { _this.setTemp(name, value); })]; } }); }); }; CordovaSecureStorage.prototype.clear = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, cordova_document_1.CordovaDocument.ready()]; case 1: _a.sent(); return [2 /*return*/, secure_storage_1.SecureStorage.create(this.KEYSTORE).then(function (store) { store.clear(); }) .catch(function () { _this.clearTemp(); })]; } }); }); }; CordovaSecureStorage.prototype.getTemp = function (key) { if (this.localData[key]) return this.localData[key]; else return null; }; CordovaSecureStorage.prototype.setTemp = function (key, data) { this.localData[key] = data; }; CordovaSecureStorage.prototype.removeTemp = function (key) { if (this.localData[key]) { delete this.localData[key]; } }; CordovaSecureStorage.prototype.clearTemp = function () { this.localData = {}; }; return CordovaSecureStorage; }(appauth_1.StorageBackend)); exports.CordovaSecureStorage = CordovaSecureStorage;