Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: investigate login error [INS-3851] #7438

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

Comment on lines +31 to +66
Copy link
Contributor

@jackkav jackkav May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was pulled out of api-cilent code to make it clearer it was going to use insomniaFetch, no sure if this is desirable @gatzjames to decide.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes lets deprecate the api-client

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
Loading