Skip to content

Commit

Permalink
feat: Refactor clearQueryParameters to cleanUrl and do cleanCode or c…
Browse files Browse the repository at this point in the history
…leanHashfragment in it
  • Loading branch information
RaymondSanders committed Mar 2, 2023
1 parent a12ea76 commit dad319a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/flows/code-flow/code-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isValidStoredAuthResult,
} from "../../jwt/validate-auth-result";
import { LogUtil } from "../../utils/log-util";
import { clearQueryParameters } from "../../utils/url";
import { cleanUrl } from "../../utils/url";
import {
accessTokenRequest,
createCodeFlowAccessTokenRequestParameters,
Expand All @@ -37,7 +37,7 @@ export async function codeFlow(
LogUtil.debug(
"The URL does have a code; save it in memory and clear the URL",
);
clearQueryParameters();
cleanUrl();

const codeFlowAuthResult = await codeFlowAccessTokenFlow(code);
LogUtil.debug("Got auth result by token request", codeFlowAuthResult);
Expand Down
4 changes: 2 additions & 2 deletions src/flows/implicit-flow/implicit-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { cleanSessionStorage } from "../../utils/clean-session-storage";
import { LogUtil } from "../../utils/log-util";
import { transformScopesStringToArray } from "../../utils/scope";
import { clearQueryParameters } from "../../utils/url";
import { cleanUrl } from "../../utils/url";
import {
deleteStoredHashString,
getAuthResultFromStoredHash,
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function implicitFlow(
if (authResultFromUrl) {
if (await isValidNewAuthResult(authResultFromUrl)) {
storeAuthResult(authResultFromUrl);
clearQueryParameters();
cleanUrl();
if (
isValidStoredAuthResult(
authResultFromUrl,
Expand Down
4 changes: 2 additions & 2 deletions src/flows/implicit-flow/session-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { discoveryState } from "../../discovery/discovery-state";
import { LogUtil } from "../../utils/log-util";
import { timeout } from "../../utils/timeout";
import {
clearQueryParameters,
cleanUrl,
getHashParameters,
parseQueryParameters,
toUrlParameterString,
Expand All @@ -16,7 +16,7 @@ import type { AuthResult } from "../../jwt/model/auth-result.model";
export function getSessionUpgradeToken(): string | null {
const authResultFromUrl = getHashParameters<Partial<AuthResult>>();
if (authResultFromUrl.session_upgrade_token) {
clearQueryParameters();
cleanUrl();
return authResultFromUrl.session_upgrade_token;
}

Expand Down
21 changes: 14 additions & 7 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LogUtil } from './log-util';
import { LogUtil } from "./log-util";
import { isCodeFlow } from "./is-code-flow";

/**
* Flush state param
Expand Down Expand Up @@ -28,7 +29,7 @@ export function getSearchParameters<T>(): T {
export function parseQueryParameters<T>(queryParametersString: string): T {
let queryParametersArray;
const firstSubstring = queryParametersString.substring(0, 1);
if (firstSubstring === '#' || firstSubstring === '?') {
if (firstSubstring === "#" || firstSubstring === "?") {
queryParametersArray = queryParametersString.substring(1).split("&");
} else {
queryParametersArray = queryParametersString.split("&");
Expand Down Expand Up @@ -87,12 +88,18 @@ export function cleanHashFragment(url: string): string {

export function cleanCode(url: string): string {
const cleanedUrl = new URL(url);
cleanedUrl.searchParams.delete('code');
cleanedUrl.searchParams.delete('state');
LogUtil.debug('Cleaning Code parameter from URL', url, cleanedUrl);
cleanedUrl.searchParams.delete("code");
cleanedUrl.searchParams.delete("state");
LogUtil.debug("Cleaning Code parameter from URL", url, cleanedUrl);
return cleanedUrl.toString();
}

export function clearQueryParameters(): void {
window.history.replaceState({}, document.title, window.location.pathname);
export function cleanUrl(): void {
let cleanedUrl = window.location.href;
if (isCodeFlow()) {
cleanedUrl = cleanCode(cleanedUrl);
} else {
cleanedUrl = cleanHashFragment(cleanedUrl);
}
window.history.replaceState({}, document.title, cleanedUrl);
}

0 comments on commit dad319a

Please sign in to comment.