Skip to content

Commit ef25b1e

Browse files
authored
feat: Gen keypair in background (#81)
1 parent 00cb6f8 commit ef25b1e

File tree

7 files changed

+440
-247
lines changed

7 files changed

+440
-247
lines changed

src/channel/channel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,9 @@ module.exports = class ZitiChannel {
801801
*/
802802
async _recvSend(data) {
803803
if (!isUndefined(this._zws)) {
804-
this._ctx.logger.debug('_recvSend -> sentLen[%o] bufferedLen[%o]', data.byteLength, this._zws._ws.bufferedAmount);
804+
if (!isNull(this._zws._ws)) {
805+
this._ctx.logger.debug('_recvSend -> sentLen[%o] bufferedLen[%o]', data.byteLength, this._zws._ws.bufferedAmount);
806+
}
805807
}
806808
}
807809

src/client.js

Lines changed: 107 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717

1818
const isUndefined = require('lodash.isundefined');
1919
const isEqual = require('lodash.isequal');
20+
const isNull = require('lodash.isnull');
2021
const formatMessage = require('format-message');
2122
const { PassThrough } = require('readable-stream')
2223
const Mutex = require('async-mutex');
@@ -41,7 +42,7 @@ const ZitiPKI = require('./pki/pki');
4142
const ZitiUPDB = require('./updb/updb');
4243
const ls = require('./utils/localstorage');
4344
const zitiConstants = require('./constants');
44-
const isNull = require('lodash.isnull');
45+
const error = require('./updb/error');
4546

4647
formatMessage.setup({
4748
// locale: 'en', // what locale strings should be displayed
@@ -765,6 +766,22 @@ if (typeof window !== 'undefined') {
765766
window.fetch = zitiFetch;
766767
window.XMLHttpRequest = ZitiXMLHttpRequest;
767768
window.WebSocket = ZitiWebSocketWrapper;
769+
770+
771+
window.addEventListener('beforeunload', function (e) {
772+
773+
if (!isUndefined(ziti._ctx)) {
774+
}
775+
776+
purgeSensitiveValues(); // flush the IndexedDB
777+
778+
// e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
779+
// e.returnValue = ''; // Chrome requires returnValue to be set
780+
781+
return undefined;
782+
783+
});
784+
768785
}
769786
}
770787

@@ -795,19 +812,42 @@ _onMessage_setControllerApi = async ( event ) => {
795812
*
796813
*/
797814
_onMessage_generateKeyPair = async ( event ) => {
815+
798816
let pki = new ZitiPKI(ZitiPKI.prototype);
799817
await pki.init( { ctx: ziti._ctx, logger: ziti._ctx.logger } );
800-
let neededToGenerateKeyPair = await pki.generateKeyPair(); // await keypair calculation complete
818+
pki.generateKeyPair(); // initiate keypair calculation
819+
820+
_sendResponse( event, 'OK' );
821+
}
822+
801823

802-
if (neededToGenerateKeyPair) {
824+
/**
825+
*
826+
*/
827+
_onMessage_promptForZitiCreds = async ( event ) => {
828+
829+
let username = await ls.getWithExpiry( zitiConstants.get().ZITI_IDENTITY_USERNAME );
830+
let password = await ls.getWithExpiry( zitiConstants.get().ZITI_IDENTITY_PASSWORD );
831+
832+
if (
833+
isNull( username ) || isUndefined( username ) ||
834+
isNull( password ) || isUndefined( password )
835+
) {
803836

804837
let updb = new ZitiUPDB(ZitiUPDB.prototype);
838+
805839
await updb.init( { ctx: ziti._ctx, logger: ziti._ctx.logger } );
806-
await updb.awaitLoginFormComplete(); // await user creds input
807-
updb.closeLoginForm();
808-
809-
// Reload the page now that we have obtained the UPDB creds
810-
setTimeout(function(){ window.location.reload() }, 1000);
840+
841+
await updb.awaitCredentialsAndAPISession();
842+
843+
// Do not proceed until we have a keypair (this will render a dialog to the user informing them of status)
844+
let pki = new ZitiPKI(ZitiPKI.prototype);
845+
await pki.init( { ctx: ziti._ctx, logger: ziti._ctx.logger } );
846+
await pki.awaitKeyPairGenerationComplete(); // await completion of keypair calculation
847+
848+
// Trigger a page reload now that we have creds and keypair
849+
// setTimeout(function(){ window.location.reload() }, 1000);
850+
setTimeout(function(){ window.location.href = window.location.href }, 1000);
811851
}
812852

813853
_sendResponse( event, 'OK' );
@@ -839,10 +879,31 @@ _onMessage_awaitIdentityLoaded = async ( event ) => {
839879
if (isUndefined(ziti._ctx)) {
840880
let ctx = new ZitiContext(ZitiContext.prototype);
841881
await ctx.initFromServiceWorker({ logLevel: LogLevel[event.data.options.logLevel] } );
842-
ctx.logger.success('JS SDK version %s init (_onMessage_awaitIdentityLoaded) completed', pjson.version);
882+
ctx.logger.success('JS SDK version %s initFromServiceWorker (_onMessage_awaitIdentityLoaded) completed', pjson.version);
843883
ziti._ctx = ctx;
844884
}
845885

886+
let pki = new ZitiPKI(ZitiPKI.prototype);
887+
await pki.init( { ctx: ziti._ctx, logger: ziti._ctx.logger } );
888+
await pki.awaitKeyPairGenerationComplete(); // ensure keypair calculation has completed
889+
890+
if ( isNull( ziti._ctx._loginFormValues.username ) || isUndefined( ziti._ctx._loginFormValues.username ) || isNull( ziti._ctx._loginFormValues.password ) || isUndefined( ziti._ctx._loginFormValues.password ) ) {
891+
892+
let username = await ls.getWithExpiry( zitiConstants.get().ZITI_IDENTITY_USERNAME );
893+
let password = await ls.getWithExpiry( zitiConstants.get().ZITI_IDENTITY_PASSWORD );
894+
895+
if ( isNull( username ) || isUndefined( username ) || isNull( password ) || isUndefined( password ) ) {
896+
897+
let updb = new ZitiUPDB(ZitiUPDB.prototype);
898+
await updb.init( { ctx: ziti._ctx, logger: ziti._ctx.logger } );
899+
await updb.awaitCredentialsAndAPISession();
900+
901+
}
902+
}
903+
904+
905+
await ziti._ctx.ensureAPISession();
906+
846907
await ziti._ctx._awaitIdentityLoadComplete().catch((err) => {
847908
release();
848909
_sendResponse( event, err.message );
@@ -878,10 +939,6 @@ _onMessage_nop = async ( event ) => {
878939
_sendResponse( event, 'nop OK' );
879940
}
880941

881-
// var some_cookies = Cookies.get();
882-
// if (!isUndefined(some_cookies)) {
883-
// ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, some_cookies, new Date(8640000000000000));
884-
// }
885942

886943
if (!zitiConfig.serviceWorker.active) {
887944
if ('serviceWorker' in navigator) {
@@ -911,9 +968,10 @@ if (!zitiConfig.serviceWorker.active) {
911968
navigator.serviceWorker.addEventListener('message', event => {
912969
console.log('----- Client received msg from serviceWorker: ', event.data.command);
913970

914-
if (event.data.command === 'initClient') { _onMessage_initClient( event ); }
971+
if (event.data.command === 'initClient') { _onMessage_initClient( event ); }
915972
else if (event.data.command === 'generateKeyPair') { _onMessage_generateKeyPair( event ); }
916973
else if (event.data.command === 'setControllerApi') { _onMessage_setControllerApi( event ); }
974+
else if (event.data.command === 'promptForZitiCreds') { _onMessage_promptForZitiCreds( event ); }
917975
else if (event.data.command === 'awaitIdentityLoaded') { _onMessage_awaitIdentityLoaded( event ); }
918976
else if (event.data.command === 'purgeCert') { _onMessage_purgeCert( event ); }
919977

@@ -955,3 +1013,38 @@ async function sendMessageToServiceworker( message ) {
9551013
navigator.serviceWorker.controller.postMessage(message, [ messageChannel.port2 ]);
9561014
});
9571015
}
1016+
1017+
1018+
/**
1019+
*
1020+
*/
1021+
async function purgeSensitiveValues() {
1022+
1023+
await ls.removeItem( zitiConstants.get().ZITI_CONTROLLER ); // The location of the Controller REST endpoint
1024+
await ls.removeItem( zitiConstants.get().ZITI_SERVICES ); //
1025+
await ls.removeItem( zitiConstants.get().ZITI_API_SESSION_TOKEN ); //
1026+
await ls.removeItem( zitiConstants.get().ZITI_NETWORK_SESSIONS ); //
1027+
await ls.removeItem( zitiConstants.get().ZITI_COOKIES ); //
1028+
await ls.removeItem( zitiConstants.get().ZITI_CLIENT_CERT_PEM ); //
1029+
await ls.removeItem( zitiConstants.get().ZITI_CLIENT_PRIVATE_KEY_PEM ); //
1030+
await ls.removeItem( zitiConstants.get().ZITI_IDENTITY_CERT ); //
1031+
1032+
}
1033+
1034+
1035+
(async function purgeExpiredValues() {
1036+
1037+
// await ls.getWithExpiry( zitiConstants.get().ZITI_CONTROLLER ); // The location of the Controller REST endpoint
1038+
// await ls.getWithExpiry( zitiConstants.get().ZITI_SERVICES ); //
1039+
// await ls.getWithExpiry( zitiConstants.get().ZITI_API_SESSION_TOKEN ); //
1040+
// await ls.getWithExpiry( zitiConstants.get().ZITI_NETWORK_SESSIONS ); //
1041+
// await ls.getWithExpiry( zitiConstants.get().ZITI_COOKIES ); //
1042+
// await ls.getWithExpiry( zitiConstants.get().ZITI_CLIENT_CERT_PEM ); //
1043+
// await ls.getWithExpiry( zitiConstants.get().ZITI_CLIENT_PRIVATE_KEY_PEM ); //
1044+
// await ls.getWithExpiry( zitiConstants.get().ZITI_IDENTITY_CERT ); //
1045+
// await ls.getWithExpiry( zitiConstants.get().ZITI_IDENTITY_USERNAME ); //
1046+
// await ls.getWithExpiry( zitiConstants.get().ZITI_IDENTITY_PASSWORD ); //
1047+
// await ls.getWithExpiry( zitiConstants.get().ZITI_COOKIES ); //
1048+
1049+
setTimeout(purgeExpiredValues, (1000 * 5) ); // pulse this function every few seconds
1050+
})()

0 commit comments

Comments
 (0)