From 3e5dfbc5f62352adaedb69d01e8e68d85fdaee67 Mon Sep 17 00:00:00 2001 From: Govindarajan Nagarajan Date: Mon, 3 Aug 2020 17:42:48 +0200 Subject: [PATCH] Refactor: move encoding state parameters into a separate function --- plugins/auth-backend/src/lib/OAuthProvider.ts | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/plugins/auth-backend/src/lib/OAuthProvider.ts b/plugins/auth-backend/src/lib/OAuthProvider.ts index d92d31c4ff..4dfed10016 100644 --- a/plugins/auth-backend/src/lib/OAuthProvider.ts +++ b/plugins/auth-backend/src/lib/OAuthProvider.ts @@ -60,9 +60,19 @@ const readState = (stateString: string): Array => { return decodeURIComponent(stateString).split('&'); }; +export const encodeState = (stateObject: object): string => { + const vals = Object.keys(stateObject).map( + key => + `${encodeURIComponent(key)}=${encodeURIComponent( + Reflect.get(stateObject, key), + )}`, + ); + + return vals.join('&'); +}; export const verifyNonce = (req: express.Request, providerId: string) => { const cookieNonce = req.cookies[`${providerId}-nonce`]; - const state = req.query.state; + const state: Array = readState(req.query.state?.toString() ?? ''); const stateNonce = readStateParameter(state, 'nonce'); if (!cookieNonce) { @@ -135,21 +145,12 @@ export class OAuthProvider implements AuthProviderRouteHandlers { // set a nonce cookie before redirecting to oauth provider this.setNonceCookie(res, nonce); - // const stateObject: {nonce: string, - // env: string} - const stateObject = { nonce: nonce, env: env }; - - const state = Object.keys(stateObject) - .map( - key => - `${encodeURIComponent(key)}=${encodeURIComponent(stateObject[key])}`, - ) - .join('&'); + const stateParameter = encodeState(stateObject); const queryParameters = { scope, - state: state, + state: stateParameter, }; const { url, status } = await this.providerHandlers.start(