app.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { method, registerCustomElement } from "ojs/ojvcomponent";
  2. import { h } from "preact";
  3. import { useState } from "preact/hooks";
  4. import { useEffect } from "preact/hooks";
  5. import Context = require("ojs/ojcontext");
  6. import { Footer } from "./footer";
  7. import { Header } from "./header";
  8. import { Content } from "./content/index";
  9. import { Provider } from "react-redux";
  10. import { store } from "../state/store";
  11. type Props = Readonly<{
  12. appName?: string;
  13. userLogin?: string;
  14. }>;
  15. const parseJwt = token => {
  16. var base64Url = token.split('.')[1];
  17. var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
  18. var jsonPayload = decodeURIComponent(window.atob(base64).split('').map(
  19. c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
  20. ).join(''));
  21. return JSON.parse(jsonPayload);
  22. };
  23. const login = () => {
  24. const state = crypto.randomUUID();
  25. const nonce = crypto.randomUUID();
  26. sessionStorage.setItem("oauth", JSON.stringify({ state, nonce }));
  27. window.location.href = "https://idcs-25070016ce0c4eb8b6eea18f07fe170d.identity.oraclecloud.com/oauth2/v1/authorize" +
  28. "?client_id=4e728d65cf5b482ea81e56bf23a9ad8a" +
  29. "&response_type=code" +
  30. "&redirect_uri=https%3A%2F%2Flocalhost%3A8000" +
  31. "&scope=openid" +
  32. `&nonce=${nonce}` +
  33. `&state=${state}`;
  34. };
  35. export const App = registerCustomElement(
  36. "app-root",
  37. ({ appName = "App Name" }: Props) => {
  38. const [userLogin, setUserLogin] = useState("");
  39. useEffect(() => {
  40. Context.getPageContext().getBusyContext().applicationBootstrapComplete();
  41. const searchParams = new URLSearchParams(window.location.search);
  42. if (searchParams.has("code") && searchParams.has("state")) {
  43. const { state } = JSON.parse(sessionStorage.getItem("oauth"));
  44. const receivedState = searchParams.get("state");
  45. if (receivedState !== state) {
  46. return;
  47. }
  48. // Get the tokens using the received authorization code
  49. const code = searchParams.get("code");
  50. console.log("code", code);
  51. fetch("https://idcs-25070016ce0c4eb8b6eea18f07fe170d.identity.oraclecloud.com/oauth2/v1/token", {
  52. method: "POST",
  53. headers: new Headers({
  54. Authorization: "Basic NGU3MjhkNjVjZjViNDgyZWE4MWU1NmJmMjNhOWFkOGE6ZGM1NDdkYmUtOGQ0Yi00MTU1LWEzNzgtZjNhMDNkNTZhNjU0",
  55. "Content-Type": "application/x-www-form-urlencoded"
  56. }),
  57. body: new URLSearchParams({
  58. grant_type: "authorization_code",
  59. code
  60. })
  61. })
  62. .then(response => response.json())
  63. .then(body => {
  64. const idToken = parseJwt(body.id_token);
  65. setUserLogin(idToken.user_displayname);
  66. })
  67. .finally(() => sessionStorage.removeItem("oauth"));
  68. // Clear the search parameter from the url
  69. window.history.pushState({}, document.title, window.location.pathname);
  70. } else {
  71. sessionStorage.removeItem("oauth");
  72. }
  73. }, []);
  74. return (
  75. <Provider store={store}>
  76. <div id="appContainer" class="oj-web-applayout-page">
  77. <Header
  78. appName={appName}
  79. userLogin={userLogin}
  80. login={login}
  81. />
  82. <Content />
  83. <Footer />
  84. </div>
  85. </Provider>
  86. );
  87. }
  88. );