From cb80b2b4f8fc66189faf8c444e2f3e5eb05af8d4 Mon Sep 17 00:00:00 2001 From: Govindarajan Nagarajan Date: Tue, 4 Aug 2020 18:42:16 +0200 Subject: [PATCH] Refactor: encode state Parameters as URLSearchParams instead of JSON --- plugins/auth-backend/src/lib/OAuthProvider.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plugins/auth-backend/src/lib/OAuthProvider.ts b/plugins/auth-backend/src/lib/OAuthProvider.ts index 608e7f2075..6faaa9f733 100644 --- a/plugins/auth-backend/src/lib/OAuthProvider.ts +++ b/plugins/auth-backend/src/lib/OAuthProvider.ts @@ -42,10 +42,12 @@ export type Options = { const readState = (stateString: string): OAuthState => { try { - const state = JSON.parse(decodeURIComponent(stateString)); + const state = Object.fromEntries( + new URLSearchParams(decodeURIComponent(stateString)), + ); return { - nonce: state.nonce, - env: state.env, + nonce: state.nonce ?? undefined, + env: state.env ?? undefined, }; } catch (e) { return { @@ -56,7 +58,11 @@ const readState = (stateString: string): OAuthState => { }; export const encodeState = (state: OAuthState): string => { - return encodeURIComponent(JSON.stringify(state)); + const searchParams = new URLSearchParams(); + searchParams.append('nonce', state.nonce ?? ''); + searchParams.append('env', state.env ?? ''); + + return encodeURIComponent(searchParams.toString()); }; /*