Make it more clear

This commit is contained in:
garronej 2023-03-04 20:26:13 +01:00
parent 00059fbb80
commit 598f164112
4 changed files with 11 additions and 38 deletions

View file

@ -23,7 +23,7 @@
"license": "MIT", "license": "MIT",
"keywords": [], "keywords": [],
"dependencies": { "dependencies": {
"evt": "^2.4.1", "evt": "^2.4.13",
"jwt-decode": "^3.1.2", "jwt-decode": "^3.1.2",
"keycloak-js": "^21.0.1", "keycloak-js": "^21.0.1",
"keycloakify": "^6.12.1", "keycloakify": "^6.12.1",

View file

@ -3,16 +3,15 @@ import logo from "./logo.svg";
import myimg from "./myimg.png"; import myimg from "./myimg.png";
import { createOidcClientProvider, useOidcClient } from "./oidc"; import { createOidcClientProvider, useOidcClient } from "./oidc";
import { addFooToQueryParams, addBarToQueryParams } from "../keycloak-theme/valuesTransferredOverUrl"; import { addFooToQueryParams, addBarToQueryParams } from "../keycloak-theme/valuesTransferredOverUrl";
import { Evt } from "evt";
import { id } from "tsafe/id";
import jwt_decode from "jwt-decode"; import jwt_decode from "jwt-decode";
const { OidcClientProvider } = createOidcClientProvider({ const { OidcClientProvider } = createOidcClientProvider({
url: "https://auth.code.gouv.fr/auth", url: "https://auth.code.gouv.fr/auth",
realm: "keycloakify", realm: "keycloakify",
clientId: "starter", clientId: "starter",
log: console.log, //This function will be called just before redirecting,
//The login pages will be in english. //it should return the current langue.
//kcContext.locale.currentLanguageTag will be what this function returned just before redirecting.
getUiLocales: () => "en", getUiLocales: () => "en",
transformUrlBeforeRedirect: url => transformUrlBeforeRedirect: url =>
[url] [url]
@ -21,13 +20,7 @@ const { OidcClientProvider } = createOidcClientProvider({
.map(url => addFooToQueryParams({ url, value: { foo: 42 } })) .map(url => addFooToQueryParams({ url, value: { foo: 42 } }))
.map(url => addBarToQueryParams({ url, value: "value of bar transferred to login page" })) .map(url => addBarToQueryParams({ url, value: "value of bar transferred to login page" }))
[0], [0],
// An event emitter that posts whenever the user interacts with the app log: console.log
// This is to tell if we should allow the token to expires.
evtUserActivity:
Evt.merge([
Evt.from(document, "mousemove"),
Evt.from(document, "keydown")
]).pipe(() => [id<void>(undefined)]),
}); });
export default function App() { export default function App() {
@ -49,7 +42,7 @@ function ContextualizedApp() {
{ {
oidcClient.isUserLoggedIn ? oidcClient.isUserLoggedIn ?
<> <>
<h1>You are authenticated</h1> <h1>You are authenticated !</h1>
<pre>{JSON.stringify(jwt_decode(oidcClient.accessToken))}</pre> <pre>{JSON.stringify(jwt_decode(oidcClient.accessToken))}</pre>
<button onClick={() => oidcClient.logout({ redirectTo: "home" })}>Logout</button> <button onClick={() => oidcClient.logout({ redirectTo: "home" })}>Logout</button>
</> </>

View file

@ -6,8 +6,8 @@ import type { ReturnType } from "tsafe/ReturnType";
import type { Param0 } from "tsafe/Param0"; import type { Param0 } from "tsafe/Param0";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import { createKeycloakAdapter } from "keycloakify"; import { createKeycloakAdapter } from "keycloakify";
import type { NonPostableEvt } from "evt";
import jwt_decode from "jwt-decode"; import jwt_decode from "jwt-decode";
import { Evt } from "evt";
export declare type OidcClient = OidcClient.LoggedIn | OidcClient.NotLoggedIn; export declare type OidcClient = OidcClient.LoggedIn | OidcClient.NotLoggedIn;
@ -34,7 +34,6 @@ type Params = {
realm: string; realm: string;
clientId: string; clientId: string;
transformUrlBeforeRedirect: (url: string) => string; transformUrlBeforeRedirect: (url: string) => string;
evtUserActivity: NonPostableEvt<void>;
getUiLocales: () => string; getUiLocales: () => string;
log?: typeof console.log; log?: typeof console.log;
}; };
@ -45,7 +44,6 @@ async function createKeycloakOidcClient(params: Params): Promise<OidcClient> {
realm, realm,
clientId, clientId,
transformUrlBeforeRedirect, transformUrlBeforeRedirect,
evtUserActivity,
getUiLocales, getUiLocales,
log log
} = params; } = params;
@ -135,7 +133,10 @@ async function createKeycloakOidcClient(params: Params): Promise<OidcClient> {
`OIDC access token will expire in ${minValiditySecond} seconds, waiting for user activity before renewing` `OIDC access token will expire in ${minValiditySecond} seconds, waiting for user activity before renewing`
); );
await evtUserActivity.waitFor(); await Evt.merge([
Evt.from(document, "mousemove"),
Evt.from(document, "keydown")
]).waitFor();
log?.("User activity detected. Refreshing access token now"); log?.("User activity detected. Refreshing access token now");

View file

@ -4314,15 +4314,6 @@ events@^3.2.0:
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400" resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q== integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==
evt@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/evt/-/evt-2.4.1.tgz#68beca2f7bd7eb755fdda5b263a80b934100e046"
integrity sha512-eo7sZcfDbiVWD5Aw6STkIEMmchYLdeGnJB6tVaM9AXZc7pViin3PmQhb6fgFIFHfl0re9zSEHtSjyu70Y7dRJg==
dependencies:
minimal-polyfills "^2.2.2"
run-exclusive "^2.2.16"
tsafe "^1.0.1"
evt@^2.4.13: evt@^2.4.13:
version "2.4.13" version "2.4.13"
resolved "https://registry.yarnpkg.com/evt/-/evt-2.4.13.tgz#5ef873159ce62e099d58801a3e4b8e0f5b648017" resolved "https://registry.yarnpkg.com/evt/-/evt-2.4.13.tgz#5ef873159ce62e099d58801a3e4b8e0f5b648017"
@ -7967,13 +7958,6 @@ rollup@^2.43.1:
optionalDependencies: optionalDependencies:
fsevents "~2.3.2" fsevents "~2.3.2"
run-exclusive@^2.2.16:
version "2.2.16"
resolved "https://registry.yarnpkg.com/run-exclusive/-/run-exclusive-2.2.16.tgz#8fa30a23037760af296c47872a5f6b38f25accf0"
integrity sha512-cdYv2LDvaBCRnrqXrwDFs1SgzGTx0EIsiEReTpsprEDR6hRUVlSyjoMYu+rez4S1gpz6YbOQxcmYFMXJQknVnQ==
dependencies:
minimal-polyfills "^2.2.1"
run-exclusive@^2.2.18: run-exclusive@^2.2.18:
version "2.2.18" version "2.2.18"
resolved "https://registry.yarnpkg.com/run-exclusive/-/run-exclusive-2.2.18.tgz#ec930edc3a7044750dc827df9372bde8f610f586" resolved "https://registry.yarnpkg.com/run-exclusive/-/run-exclusive-2.2.18.tgz#ec930edc3a7044750dc827df9372bde8f610f586"
@ -8713,11 +8697,6 @@ tryer@^1.0.1:
resolved "https://registry.yarnpkg.com/tryer/-/tryer-1.0.1.tgz#f2c85406800b9b0f74c9f7465b81eaad241252f8" resolved "https://registry.yarnpkg.com/tryer/-/tryer-1.0.1.tgz#f2c85406800b9b0f74c9f7465b81eaad241252f8"
integrity sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA== integrity sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA==
tsafe@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/tsafe/-/tsafe-1.0.1.tgz#c8c4eb2d75d1478418a4941307c5dd667fd76d23"
integrity sha512-FgJ1a4rE7YbmW5QIzpsfFl4tsAp0x74FH2bVE6qODb2U8jSrwTr5/ckIazeylme5zXndVbtgKm4BZdqmoGhiPw==
tsafe@^1.4.1: tsafe@^1.4.1:
version "1.4.1" version "1.4.1"
resolved "https://registry.yarnpkg.com/tsafe/-/tsafe-1.4.1.tgz#59cdad8ac41babf88e56dcd1a683ae2fb145d059" resolved "https://registry.yarnpkg.com/tsafe/-/tsafe-1.4.1.tgz#59cdad8ac41babf88e56dcd1a683ae2fb145d059"