Skip to content

Commit

Permalink
Fix origin issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mrruby committed May 24, 2024
1 parent 8ccdce2 commit 42d0138
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion holo-key-manager-extension/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const processMessageWebApp = async (
if (!session) {
return sendResponseWithSender({ action: EXTENSION_NOT_AUTHENTICATED });
}
if (await isAuthenticated(parsedMessage.payload.happId)) {
if (await isAuthenticated(parsedMessage.payload.happId, parsedMessage.origin)) {
const signature = await signMessageLogic({ ...parsedMessage.payload, session });
return sendResponseWithSender({
action: SIGN_MESSAGE_SUCCESS,
Expand Down
2 changes: 1 addition & 1 deletion holo-key-manager-extension/scripts/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const parseAndHandleMessage = async (event: MessageEvent) => {
const parsedResult = MessageWithIdSchema.safeParse(event.data);
if (!parsedResult.success || parsedResult.data.sender === SENDER_BACKGROUND_SCRIPT) return;
try {
const response = await sendMessage(parsedResult.data);
const response = await sendMessage({ ...parsedResult.data, origin: event.origin });
const parsedMessageSchema = parseMessageSchema(response);
window.postMessage(responseToMessage(parsedMessageSchema.data, parsedResult.data.id), '*');
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion holo-key-manager-extension/scripts/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const signMessageLogic = async ({ message, happId, session }: SignMessage
throw new Error('Authentication failed: Unable to parse apps list or session missing');
}

const index = parsedAuthenticatedAppsListData.data[happId];
const { index } = parsedAuthenticatedAppsListData.data[happId];

await hcSeedBundle.seedBundleReady;

Expand Down
3 changes: 2 additions & 1 deletion holo-key-manager-extension/src/lib/helpers/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const extractDetailsFromUrl = derived(page, ($page) => {
message: 'Unknown Message',
requireEmail: false,
requireRegistrationCode: false,
messageId: ''
messageId: '',
origin: ''
};

const params = new URLSearchParams(new URL($page.url.href).search);
Expand Down
33 changes: 16 additions & 17 deletions holo-key-manager-extension/src/lib/queries/applicationQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function createApplicationKeyMutation(queryClient: QueryClient) {
happLogo: string;
happUiUrl: string;
messageId: string;
origin: string;
email?: string;
registrationCode?: string;
}) => {
Expand Down Expand Up @@ -59,7 +60,10 @@ export function createApplicationKeyMutation(queryClient: QueryClient) {

const updatedAppsList = {
...currentParsedAuthenticatedAppsListData,
[mutationData.happId]: newIndex
[mutationData.happId]: {
index: newIndex,
origin: mutationData.origin
}
};

const pubKeyObject = await deriveSignPubKey(newIndex);
Expand Down Expand Up @@ -91,8 +95,13 @@ export function createApplicationKeyMutation(queryClient: QueryClient) {

export function createSignInWithKeyMutation(queryClient: QueryClient) {
return createMutation({
mutationFn: async (signInData: { keyName: string; happId: string; messageId: string }) => {
const { keyName, happId, messageId } = signInData;
mutationFn: async (signInData: {
keyName: string;
happId: string;
messageId: string;
origin: string;
}) => {
const { keyName, happId, messageId, origin } = signInData;
const currentAppsList = await fetchAndParseAppsList();

const appData = currentAppsList.find(
Expand All @@ -115,7 +124,10 @@ export function createSignInWithKeyMutation(queryClient: QueryClient) {
key: AUTHENTICATED_APPS_LIST,
value: {
...currentAuthenticatedAppsListData,
[happId]: newIndex
[happId]: {
index: newIndex,
origin
}
},
area: SESSION
});
Expand All @@ -133,19 +145,6 @@ export function createSignInWithKeyMutation(queryClient: QueryClient) {
});
}

export function createSignedInApplicationKeysIndexQuery() {
return (happId: string) => {
return createQuery({
queryKey: [APPLICATION_SIGNED_IN_KEY, happId],
queryFn: async () => {
const authenticatedAppsListData = await fetchAuthenticatedAppsList(happId);

return authenticatedAppsListData[happId];
}
});
};
}

export function createApplicationKeysQuery() {
return (happId: string) => {
return createQuery({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
happLogo: $extractDetailsFromUrl.happLogo,
happUiUrl: $extractDetailsFromUrl.happUiUrl,
messageId: $extractDetailsFromUrl.messageId,
origin: $extractDetailsFromUrl.origin,
email,
registrationCode
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
{
happId: $extractDetailsFromUrl.happId,
keyName: selectedKey,
origin: $extractDetailsFromUrl.origin,
messageId: $extractDetailsFromUrl.messageId
},
{
Expand Down
2 changes: 1 addition & 1 deletion holo-key-manager-extension/static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Holo key manager",
"description": "A browser extension to manage holo keys",
"version": "0.0.57",
"version": "0.0.58",
"manifest_version": 3,
"action": {
"default_title": "Holo key manager",
Expand Down
3 changes: 2 additions & 1 deletion shared/const/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export const relevantKeys = [
'requireEmail',
'requireRegistrationCode',
'message',
'messageId'
'messageId',
'origin'
] as const;
6 changes: 5 additions & 1 deletion shared/helpers/other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export const createQueryParams = (parsedMessage: MessageWithId) => {
}
return acc;
},
{ action: parsedMessage.action, messageId: parsedMessage.id }
{
action: parsedMessage.action,
messageId: parsedMessage.id,
origin: parsedMessage.origin || ''
}
);

return new URLSearchParams(additionalParams).toString();
Expand Down
8 changes: 6 additions & 2 deletions shared/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@ export const isAppSignUpComplete = async (happId: string) => {
return parsedData.some((app) => app.happId === happId);
};

export const isAuthenticated = async (happId: string) => {
export const isAuthenticated = async (happId: string, origin?: string) => {
if (!origin) return false;

const data = await storageService.getWithoutCallback({
key: AUTHENTICATED_APPS_LIST,
area: SESSION
});

const parsedData = AuthenticatedAppsListSchema.safeParse(data);

return parsedData.success && happId in parsedData.data;
return (
parsedData.success && happId in parsedData.data && parsedData.data[happId].origin === origin
);
};

export const signOut = async (happId: string) => {
Expand Down
3 changes: 2 additions & 1 deletion shared/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export const MessageWithIdSchema = z.intersection(
MessageSchema,
z.object({
id: z.string(),
appId: z.literal(HOLO_KEY_MANAGER_APP_ID)
appId: z.literal(HOLO_KEY_MANAGER_APP_ID),
origin: z.string().optional()
})
);

Expand Down
8 changes: 7 additions & 1 deletion shared/types/storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export const AppsListSchema = z.array(

export type AppsList = z.infer<typeof AppsListSchema>;

export const AuthenticatedAppsListSchema = z.record(z.string(), z.number());
export const AuthenticatedAppsListSchema = z.record(
z.string(),
z.object({
index: z.number(),
origin: z.string()
})
);

export type AuthenticatedAppsList = z.infer<typeof AuthenticatedAppsListSchema>;

Expand Down

0 comments on commit 42d0138

Please sign in to comment.