From bf08883f1556e4c618caef6a87acb591f96bff67 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:07:26 +0530 Subject: [PATCH 01/21] chore: method signatures for sendPostRequest method --- assets/js/oauth.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 022c33627..0c7a1a651 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -3,7 +3,15 @@ const redirectUri = `${window.location.origin}/`; ////////////////////////////////////////////////////////////////////// // GENERAL HELPER FUNCTIONS -// Make a POST request and parse the response as JSON +/** + * Makes a POST request and parses the response as JSON + * + * @param {String} url - The resource + * @param {Object} params - Configuration options + * @returns {Object} + */ + +// const sendPostRequest = async (url, params) => { const body = Object.keys(params) .map(key => `${key}=${params[key]}`) From 14f03e61ef21593739a015f23f5c20114638f19e Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:07:53 +0530 Subject: [PATCH 02/21] fix: minor tweak --- assets/js/oauth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 0c7a1a651..63bc004c8 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -1,6 +1,5 @@ const redirectUri = `${window.location.origin}/`; -////////////////////////////////////////////////////////////////////// // GENERAL HELPER FUNCTIONS /** From 7f8b49dd1d4391db531336515e8031a2af587635 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:08:05 +0530 Subject: [PATCH 03/21] fix: minor tweak --- assets/js/oauth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 63bc004c8..bb93cd0cd 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -10,7 +10,6 @@ const redirectUri = `${window.location.origin}/`; * @returns {Object} */ -// const sendPostRequest = async (url, params) => { const body = Object.keys(params) .map(key => `${key}=${params[key]}`) From 2f3c3aeba93013fc5791711bd151c7ba3cb0a281 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:10:11 +0530 Subject: [PATCH 04/21] chore: method signatures for parseQueryString method --- assets/js/oauth.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index bb93cd0cd..b83965418 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -30,7 +30,14 @@ const sendPostRequest = async (url, params) => { throw err; } }; -// Parse a query string into an object + +/** + * Parse a query string into an object + * + * @param {String} string - The query string + * @returns {Object} + */ + const parseQueryString = string => { if (string === "") { return {}; From 5dab781fbe022d9c70f2825c074185d139d98b24 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:11:02 +0530 Subject: [PATCH 05/21] fix: minor tweak --- assets/js/oauth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index b83965418..c5c09da1d 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -3,7 +3,7 @@ const redirectUri = `${window.location.origin}/`; // GENERAL HELPER FUNCTIONS /** - * Makes a POST request and parses the response as JSON + * Makes a POST request and parse the response as JSON * * @param {String} url - The resource * @param {Object} params - Configuration options From bbb559ad2c6c4c28d31e183094d40a404e2f590c Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:13:12 +0530 Subject: [PATCH 06/21] chore: change var name --- assets/js/oauth.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index c5c09da1d..813aa12a7 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -38,11 +38,11 @@ const sendPostRequest = async (url, params) => { * @returns {Object} */ -const parseQueryString = string => { - if (string === "") { +const parseQueryString = searchQuery => { + if (searchQuery === "") { return {}; } - const segments = string.split("&").map(s => s.split("=")); + const segments = searchQuery.split("&").map(s => s.split("=")); const queryString = {}; segments.forEach(s => (queryString[s[0]] = s[1])); return queryString; From ee49f8d26c47f9ae26c67f515b32492d64e3d3fa Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:13:53 +0530 Subject: [PATCH 07/21] fix: minor tweak --- assets/js/oauth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 813aa12a7..774da6d59 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -34,7 +34,7 @@ const sendPostRequest = async (url, params) => { /** * Parse a query string into an object * - * @param {String} string - The query string + * @param {String} searchQuery - The search query params * @returns {Object} */ From 1a04d79fb9f4f1cbf897ffad8345e49c26e4763b Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:15:09 +0530 Subject: [PATCH 08/21] chore: method signatures for getTokenConfiguration method --- assets/js/oauth.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 774da6d59..a1a32c6d0 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -47,7 +47,13 @@ const parseQueryString = searchQuery => { segments.forEach(s => (queryString[s[0]] = s[1])); return queryString; }; -// Get OAuth configuration from OpenID Discovery endpoint + +/** + * Get OAuth configuration from OpenID Discovery endpoint + * + * @returns {Object} + */ + const getTokenConfiguration = async endpoint => { const options = { method: "GET", From 80f7d3120ace2bdf7166dc33894ac6b42434996b Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:15:41 +0530 Subject: [PATCH 09/21] fix: minor tweak --- assets/js/oauth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index a1a32c6d0..0316d374f 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -71,7 +71,6 @@ const getTokenConfiguration = async endpoint => { } }; -////////////////////////////////////////////////////////////////////// // PKCE HELPER FUNCTIONS // Generate a secure random string using the browser crypto functions From 4ce673acfd527b2b9a0f1a38adf3541b61f049fb Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:16:57 +0530 Subject: [PATCH 10/21] chore: method signatures for getRandomString method --- assets/js/oauth.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 0316d374f..5c26832b3 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -73,7 +73,12 @@ const getTokenConfiguration = async endpoint => { // PKCE HELPER FUNCTIONS -// Generate a secure random string using the browser crypto functions +/** + * Generate a secure random string using the browser crypto functions + * + * @returns {Object} + */ + const generateRandomString = () => { const array = new Uint32Array(28); window.crypto.getRandomValues(array); From 12ef2f4465d2346885898748bb831929ce3245f0 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:17:24 +0530 Subject: [PATCH 11/21] fix: minor tweak --- assets/js/oauth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 5c26832b3..f9a58bdba 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -74,7 +74,7 @@ const getTokenConfiguration = async endpoint => { // PKCE HELPER FUNCTIONS /** - * Generate a secure random string using the browser crypto functions + * Generates a secure random string using the browser crypto functions * * @returns {Object} */ From 836bd9ab3c1298fd96fdb4150f346ab40f006ef2 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:19:31 +0530 Subject: [PATCH 12/21] chore: method signatures for sha256 method --- assets/js/oauth.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index f9a58bdba..d9cabff26 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -84,8 +84,13 @@ const generateRandomString = () => { window.crypto.getRandomValues(array); return Array.from(array, dec => `0${dec.toString(16)}`.substr(-2)).join(""); }; -// Calculate the SHA256 hash of the input text. -// Returns a promise that resolves to an ArrayBuffer + +/** + * Calculate the SHA256 hash of the input text + * + * @returns {Promise} + */ + const sha256 = plain => { const encoder = new TextEncoder(); const data = encoder.encode(plain); From 3ba279c81b6c57578c5cae45296739ffb64a0d84 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:24:04 +0530 Subject: [PATCH 13/21] chore: method signatures for base64urlencode method --- assets/js/oauth.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index d9cabff26..e2a64fe29 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -96,7 +96,14 @@ const sha256 = plain => { const data = encoder.encode(plain); return window.crypto.subtle.digest("SHA-256", data); }; -// Base64-urlencodes the input string + +/** + * Base64-urlencodes the input string + * + * @param {String} str - The string to be converted + * @returns {Promise} + */ + const base64urlencode = ( str // Convert the ArrayBuffer to string using Uint8 array to convert to what btoa accepts. ) => From 9e5a162d74adf42a01150d98c18f0223f1c48621 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:25:01 +0530 Subject: [PATCH 14/21] fix: minor tweak --- assets/js/oauth.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index e2a64fe29..9b50a0952 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -98,14 +98,14 @@ const sha256 = plain => { }; /** - * Base64-urlencodes the input string + * Encodes the input string into Base64 format * * @param {String} str - The string to be converted * @returns {Promise} */ const base64urlencode = ( - str // Convert the ArrayBuffer to string using Uint8 array to convert to what btoa accepts. + str // Converts the ArrayBuffer to string using Uint8 array to convert to what btoa accepts. ) => // btoa accepts chars only within ascii 0-255 and base64 encodes them. // Then convert the base64 encoded to base64url encoded From a4b89a2152da3478237c86ca80054ad46ec42451 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:26:56 +0530 Subject: [PATCH 15/21] chore: method signatures for pkceChallengeFromVerifier method --- assets/js/oauth.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 9b50a0952..fdbe1c7f7 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -114,7 +114,14 @@ const base64urlencode = ( .replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/, ""); -// Return the base64-urlencoded sha256 hash for the PKCE challenge + +/** + * Return the base64-urlencoded sha256 hash for the PKCE challenge + * + * @param {String} v - The randomly generated string + * @returns {String} + */ + const pkceChallengeFromVerifier = async v => { const hashed = await sha256(v); return base64urlencode(hashed); From daf214275b723da34b5edae0e6a140502d850179 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:29:57 +0530 Subject: [PATCH 16/21] chore: method signatures for tokenRequest method --- assets/js/oauth.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index fdbe1c7f7..784f6baea 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -127,10 +127,15 @@ const pkceChallengeFromVerifier = async v => { return base64urlencode(hashed); }; -////////////////////////////////////////////////////////////////////// // OAUTH REQUEST -// Initiate PKCE Auth Code flow when requested +/** + * Initiate PKCE Auth Code flow when requested + * + * @param {Object} - The necessary params + * @returns {Void} + */ + const tokenRequest = async ({ oidcDiscoveryUrl, grantType, From dcd148e43b1a0d2581e281df238919217628b5de Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:30:03 +0530 Subject: [PATCH 17/21] fix: minor tweak --- assets/js/oauth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 784f6baea..6e00a21c6 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -130,7 +130,7 @@ const pkceChallengeFromVerifier = async v => { // OAUTH REQUEST /** - * Initiate PKCE Auth Code flow when requested + * Initiates PKCE Auth Code flow when requested * * @param {Object} - The necessary params * @returns {Void} From e1423033a6ee04050a4a8c438ebbdfff576128a1 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 13:31:49 +0530 Subject: [PATCH 18/21] chore: method signatures for oauthRedirect method --- assets/js/oauth.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index 6e00a21c6..cfe3d5d15 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -185,11 +185,15 @@ const tokenRequest = async ({ window.location = buildUrl(); }; -////////////////////////////////////////////////////////////////////// // OAUTH REDIRECT HANDLING -// Handle the redirect back from the authorization server and -// get an access token from the token endpoint +/** + * Handle the redirect back from the authorization server and + * get an access token from the token endpoint + * + * @returns {Object} + */ + const oauthRedirect = async () => { let tokenResponse = ""; let q = parseQueryString(window.location.search.substring(1)); From 64222327c96abcf089a4e268d16c8481723a6bfd Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 16:36:37 +0530 Subject: [PATCH 19/21] refactor: prefer reduce --- assets/js/oauth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index cfe3d5d15..c324dfa05 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -44,7 +44,7 @@ const parseQueryString = searchQuery => { } const segments = searchQuery.split("&").map(s => s.split("=")); const queryString = {}; - segments.forEach(s => (queryString[s[0]] = s[1])); + const queryString = segments.reduce((obj, el) => ({ ...obj, [el[0]]: el[1] }), {}); return queryString; }; From 9838a68ff7744f14ee35de45591ae765314489ee Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 16:37:58 +0530 Subject: [PATCH 20/21] fix: remove redundant declaration --- assets/js/oauth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index c324dfa05..f09358ac9 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -43,7 +43,6 @@ const parseQueryString = searchQuery => { return {}; } const segments = searchQuery.split("&").map(s => s.split("=")); - const queryString = {}; const queryString = segments.reduce((obj, el) => ({ ...obj, [el[0]]: el[1] }), {}); return queryString; }; From 5fffc5d29e9f5e801b0926aa9525374740e14dee Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 11 Jan 2020 16:38:54 +0530 Subject: [PATCH 21/21] fix: lint --- assets/js/oauth.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assets/js/oauth.js b/assets/js/oauth.js index f09358ac9..447fe5334 100644 --- a/assets/js/oauth.js +++ b/assets/js/oauth.js @@ -43,7 +43,10 @@ const parseQueryString = searchQuery => { return {}; } const segments = searchQuery.split("&").map(s => s.split("=")); - const queryString = segments.reduce((obj, el) => ({ ...obj, [el[0]]: el[1] }), {}); + const queryString = segments.reduce( + (obj, el) => ({ ...obj, [el[0]]: el[1] }), + {} + ); return queryString; };