Skip to content

Commit 1292aa8

Browse files
authored
fix: session refresh (#85)
1 parent 3d3d6c3 commit 1292aa8

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/client.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,19 @@ zitiFetch = async ( url, opts ) => {
621621

622622
if (url.match( regex )) { // the request is targeting the Ziti HTTP Agent
623623

624+
let updb = new ZitiUPDB(ZitiUPDB.prototype);
625+
await updb.init( { ctx: ziti._ctx, logger: ziti._ctx.logger } );
626+
let haveCreds = await updb._haveCreds();
627+
if (!haveCreds) {
628+
await updb.awaitCredentialsAndAPISession();
629+
} else {
630+
// Remain in this loop until the creds entered on login form are acceptable to the Ziti Controller
631+
let validCreds;
632+
do {
633+
validCreds = await ziti._ctx.ensureAPISession();
634+
} while ( !validCreds );
635+
}
636+
624637
var newUrl = new URL( url );
625638
newUrl.hostname = zitiConfig.httpAgent.target.host;
626639
newUrl.port = zitiConfig.httpAgent.target.port;

src/context/context.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ ZitiContext.prototype._awaitIdentityLoadCompleteFromServiceWorker = async functi
281281

282282
if (!isUndefined(res.error)) {
283283
self.logger.error(res.error.message);
284-
resolve( false );
285-
return;
284+
return resolve( false );
286285
}
287286

288287
self._apiSession = res.data;
@@ -293,7 +292,7 @@ ZitiContext.prototype._awaitIdentityLoadCompleteFromServiceWorker = async functi
293292

294293
await ls.setWithExpiry(zitiConstants.get().ZITI_API_SESSION_TOKEN, self._apiSession, new Date( Date.parse( self._apiSession.expiresAt )));
295294

296-
resolve( true );
295+
return resolve( true );
297296
});
298297
}
299298

src/updb/updb.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ ZitiUPDB.prototype.promptForCreds = async function() {
171171
expiry.setMinutes(expiry.getMinutes()+5);
172172
await ls.setWithExpiry(zitiConstants.get().ZITI_IDENTITY_USERNAME, self._loginFormValues.username, expiry );
173173
await ls.setWithExpiry(zitiConstants.get().ZITI_IDENTITY_PASSWORD, self._loginFormValues.password, expiry );
174+
175+
// if we're restarting with updb, then purge any existing API session & cert
176+
await ls.removeItem( zitiConstants.get().ZITI_API_SESSION_TOKEN );
177+
await ls.removeItem( zitiConstants.get().ZITI_CLIENT_CERT_PEM );
174178
});
175179

176180
MicroModal.init({

0 commit comments

Comments
 (0)