Skip to content

Commit

Permalink
chore: investigate login error [INS-3851] (Kong#7438)
Browse files Browse the repository at this point in the history
* chore: investigate login error [INS-3851]

* another pass at error we show

* Update packages/insomnia/src/ui/auth-session-provider.ts
  • Loading branch information
filfreire authored and stefancruz committed Jun 30, 2024
1 parent e496978 commit 87f34bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
41 changes: 39 additions & 2 deletions packages/insomnia/src/ui/auth-session-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { decodeBase64, encodeBase64 } from '@getinsomnia/api-client/base64';
import { keyPair, open } from '@getinsomnia/api-client/sealedbox';

import * as session from '../account/session';
Expand Down Expand Up @@ -29,6 +28,43 @@ encodeBase64(sessionKeyPair.secretKey).then(res => {
* Keypair used for the login handshake.
* This keypair can be re-used for the entire session.
*/

export async function decodeBase64(base64: string): Promise<Uint8Array> {
try {
let uri = 'data:application/octet-binary;base64,';
uri += base64;
const res = await fetch(uri);
const buffer = await res.arrayBuffer();
return new Uint8Array(buffer);

} catch (error) {
console.error(error);
throw new Error('Failed to decode base64');
}
}

export async function encodeBase64(data: Uint8Array): Promise<string> {
const dataUri = await new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (typeof reader.result === 'string') {
resolve(reader.result);
} else {
reject();
}
};
reader.onerror = reject;
reader.readAsDataURL(new Blob([data]));
});

const dataAt = dataUri.indexOf(',');
if (dataAt === -1) {
throw new Error(`unexpected data uri output: ${dataUri}`);
}

return dataUri.slice(dataAt + 1);
}

export async function submitAuthCode(code: string) {
try {
const rawBox = await decodeBase64(code.trim());
Expand All @@ -41,7 +77,8 @@ export async function submitAuthCode(code: string) {
const box: AuthBox = JSON.parse(decoder.decode(boxData));
await session.absorbKey(box.token, box.key);
} catch (error) {
return error.message;
console.error(error);
return error;
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/insomnia/src/ui/routes/auth.authorize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export const action: ActionFunction = async ({
const data = await request.json();

invariant(typeof data?.code === 'string', 'Expected code to be a string');
const fetchError = await submitAuthCode(data.code);
if (fetchError) {
const error = await submitAuthCode(data.code);
if (error) {
const humanReadableError = error === 'TypeError: Failed to fetch' ? 'Network failed, try again.' : error;
return {
errors: {
message: 'Invalid code: ' + fetchError,
message: humanReadableError,
},
};
}
Expand Down

0 comments on commit 87f34bb

Please sign in to comment.