-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #376 from huwshimi/oidc-next-url
WD-13860 - feat: return to URL that initiated login
- Loading branch information
Showing
21 changed files
with
369 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { urls as rebacURLs } from "@canonical/rebac-admin"; | ||
|
||
const rebacAdminURLS = rebacURLs("/"); | ||
|
||
export const urls = { | ||
clients: { | ||
index: "/client", | ||
}, | ||
groups: rebacAdminURLS.groups, | ||
identities: { | ||
index: "/identity", | ||
}, | ||
index: "/", | ||
providers: { | ||
index: "/provider", | ||
}, | ||
roles: rebacAdminURLS.roles, | ||
schemas: { | ||
index: "/schema", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { getDomain } from "./getDomain"; | ||
|
||
test("handles extracting domain", () => { | ||
expect(getDomain("http://example.com/?next=/here#hash")).toBe("example.com"); | ||
}); | ||
|
||
test("handles no domain", () => { | ||
expect(getDomain("/a/path")).toBeUndefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Extract the domain from the URL. | ||
export const getDomain = (url: string) => url.match(/(?<=.+:\/\/)[^/]+/)?.[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getFullPath } from "./getFullPath"; | ||
|
||
vi.mock("./basePaths", async () => { | ||
const actual = await vi.importActual("./basePaths"); | ||
return { | ||
...actual, | ||
basePath: "/example/ui/", | ||
}; | ||
}); | ||
|
||
test("handles paths with query and hash", () => { | ||
expect(getFullPath("http://example.com/?next=/here#hash")).toBe( | ||
"/?next=/here#hash", | ||
); | ||
}); | ||
|
||
test("removes base", () => { | ||
expect( | ||
getFullPath("http://example.com/example/ui/roles/?next=/here#hash", true), | ||
).toBe("/roles/?next=/here#hash"); | ||
}); | ||
|
||
test("handles no path", () => { | ||
expect(getFullPath("http://example.com/")).toBeUndefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { basePath } from "./basePaths"; | ||
import { removeTrailingSlash } from "./removeTrailingSlash"; | ||
|
||
// Extract the path from the URL including query params, hash etc. | ||
export const getFullPath = (url: string, removeBase = false) => { | ||
const path = url.match(/(?<!\/)\/(?!\/).+$/)?.[0]; | ||
return removeBase | ||
? path?.replace(new RegExp(`^${removeTrailingSlash(basePath)}`), "") | ||
: path; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { urls } from "urls"; | ||
import { getURLKey } from "./getURLKey"; | ||
test("handles indexes", () => { | ||
expect(getURLKey(urls.clients.index)).toBe("clients"); | ||
}); | ||
|
||
test("handles named paths", () => { | ||
expect(getURLKey(urls.roles.add)).toBe("roles.add"); | ||
}); | ||
|
||
test("handles functions", () => { | ||
expect(getURLKey(urls.roles.edit({ id: "role1" }))).toBe("roles.edit"); | ||
}); | ||
|
||
test("handles paths that don't exist", () => { | ||
expect(getURLKey("/nothing/here")).toBeNull(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { matchPath } from "react-router-dom"; | ||
import { urls } from "urls"; | ||
|
||
export type URLS = { | ||
[key: string]: | ||
| string | ||
| URLS | ||
| ((args: unknown, relativeTo?: string) => string); | ||
}; | ||
|
||
const findPath = ( | ||
sections: URLS, | ||
pathname: string, | ||
keyPath = "", | ||
): string | null => { | ||
const entries = Object.entries(sections); | ||
let path: string | null = null; | ||
for (const entry of entries) { | ||
const [key, section] = entry; | ||
const thisPath = [keyPath, key].filter(Boolean).join("."); | ||
if (typeof section === "string" && section === pathname) { | ||
path = thisPath; | ||
break; | ||
} else if ( | ||
typeof section === "function" && | ||
!!matchPath(section(null), pathname) | ||
) { | ||
path = thisPath; | ||
break; | ||
} else if (typeof section === "object") { | ||
const matchingPath = findPath(section, pathname, thisPath); | ||
if (matchingPath) { | ||
path = matchingPath; | ||
break; | ||
} | ||
} | ||
} | ||
// Don't expose the index key. The index is handled when the path is returned | ||
// in the useNext hook. | ||
return path ? path.replace(/\.index$/, "") : null; | ||
}; | ||
|
||
export const getURLKey = (pathname: string) => findPath(urls as URLS, pathname); |
Oops, something went wrong.