Skip to content

Commit

Permalink
don't retry auth only in 2 very specific cases #165
Browse files Browse the repository at this point in the history
- 400 response
- bad auth config
  • Loading branch information
andrei-tatar committed Mar 10, 2022
1 parent 189b52e commit 4495614
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 51 deletions.
9 changes: 0 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ export function singleton<T>(): MonoTypeOperatorFunction<T> {
);
}

export function shouldRetryRequest(response: NodeFetchResponse) {
if (response.status === 429) {
return true;
}

const status = Math.floor(response.status / 100);
return status !== 2 && status !== 4;
}

export class HttpError extends Error {
constructor(
public readonly statusCode: number,
Expand Down
74 changes: 32 additions & 42 deletions src/nora/connection.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { deleteApp, FirebaseApp, initializeApp } from 'firebase/app';
import { getAuth, signInWithEmailAndPassword, signInWithCustomToken, UserCredential } from 'firebase/auth';
import { merge, Observable, of, timer } from 'rxjs';
import { firstValueFrom, merge, NEVER, Observable, of, timer } from 'rxjs';
import { delayWhen, finalize, ignoreElements, map, retryWhen, switchMap, tap } from 'rxjs/operators';
import fetch from 'node-fetch';

import { getHash, HttpError, Logger, publishReplayRefCountWithDelay, shouldRetryRequest } from '..';
import { getHash, HttpError, Logger, publishReplayRefCountWithDelay } from '..';
import { API_ENDPOINT, FIREBASE_CONFIG, NoraConfig, USER_AGENT } from '../config';
import { AsyncCommandsRegistry } from './async-commands.registry';
import { DeviceContext } from './device-context';
Expand Down Expand Up @@ -79,51 +79,41 @@ export class FirebaseConnection {
private static async authenticate(app: FirebaseApp, config: NoraConfig): Promise<UserCredential> {
const auth = getAuth(app);

try {
if (config.password?.length) {
return await signInWithEmailAndPassword(auth, config.email, config.password);
} else if (config.sso?.length) {
const customToken = await this.exchangeToken(config.sso);
return await signInWithCustomToken(auth, customToken);
} else {
throw new Error('nora: invalid auth config');
}
} catch (err) {
this.logger?.error(`nora: ${err}`);
await new Promise<never>(() => {
// never resolve, there's nothing to retry
});
throw new Error(); // make TS happy :)
if (config.password?.length) {
return await signInWithEmailAndPassword(auth, config.email, config.password);
} else if (config.sso?.length) {
const customToken = await this.exchangeToken(config.sso);
return await signInWithCustomToken(auth, customToken);
} else {
this.logger?.error('nora: invalid auth config; not retrying');
return firstValueFrom(NEVER);
}
}

private static async exchangeToken(ssoToken: string, tries = 3): Promise<string> {
while (tries--) {
const url = `${API_ENDPOINT}/sso/exchange`;
const response = await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
'user-agent': USER_AGENT,
},
body: JSON.stringify({
token: ssoToken
}),
});
if (response.status !== 200) {
const shouldRetry = shouldRetryRequest(response);
if (!shouldRetry || !tries) {
throw new HttpError(response.status, await response.text());
}
const delay = Math.round(Math.random() * 20) * 50 + 300;
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
const json = await response.json();
return json.token;
private static async exchangeToken(ssoToken: string): Promise<string> {
const url = `${API_ENDPOINT}/sso/exchange`;
const response = await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
'user-agent': USER_AGENT,
},
body: JSON.stringify({
token: ssoToken
}),
});

if (response.status === 200) {
const { token } = await response.json();
return token;
}

if (response.status === 400) {
this.logger?.error(`nora: invalid sso token; not retrying - ${await response.text()}`);
return firstValueFrom(NEVER);
}

throw new Error('could not exchange sso token');
throw new HttpError(response.status, await response.text());
}

private static createFirebaseApp() {
Expand Down

0 comments on commit 4495614

Please sign in to comment.