@multiplatform.one/keycloak 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/esm/hooks/useKeycloak/index.js +1 -1
  2. package/esm/hooks/useKeycloak/index.js.map +1 -1
  3. package/esm/hooks/useKeycloak/useKeycloak.native.js +4 -4
  4. package/esm/hooks/useKeycloak/useKeycloak.native.js.map +1 -1
  5. package/esm/provider/keycloakProvider.native.js +1 -1
  6. package/esm/provider/keycloakProvider.native.js.map +1 -1
  7. package/lib/hooks/useKeycloak/index.d.ts +1 -1
  8. package/lib/hooks/useKeycloak/index.js.map +1 -1
  9. package/lib/hooks/useKeycloak/useKeycloak.native.d.ts +2 -4
  10. package/lib/hooks/useKeycloak/useKeycloak.native.js +10 -7
  11. package/lib/hooks/useKeycloak/useKeycloak.native.js.map +1 -1
  12. package/lib/provider/keycloakProvider.native.js +5 -5
  13. package/lib/provider/keycloakProvider.native.js.map +1 -1
  14. package/lib/tsconfig.build.tsbuildinfo +1 -1
  15. package/package.json +13 -7
  16. package/{esm/expo/index.js → src/@types/coreJsPure.d.ts} +12 -7
  17. package/src/{expo/index.ts → @types/expoKeycloakAuth.d.ts} +7 -6
  18. package/src/hooks/useKeycloak/index.ts +1 -1
  19. package/src/hooks/useKeycloak/useKeycloak.native.ts +6 -6
  20. package/src/provider/keycloakProvider.native.tsx +1 -1
  21. package/esm/expo/ExpoKeycloakContext.js +0 -28
  22. package/esm/expo/ExpoKeycloakContext.js.map +0 -1
  23. package/esm/expo/ExpoKeycloakProvider.js +0 -113
  24. package/esm/expo/ExpoKeycloakProvider.js.map +0 -1
  25. package/esm/expo/const.js +0 -42
  26. package/esm/expo/const.js.map +0 -1
  27. package/esm/expo/helpers.js +0 -56
  28. package/esm/expo/helpers.js.map +0 -1
  29. package/esm/expo/index.js.map +0 -1
  30. package/esm/expo/useExpoKeycloak.js +0 -44
  31. package/esm/expo/useExpoKeycloak.js.map +0 -1
  32. package/esm/expo/useTokenStorage.js +0 -153
  33. package/esm/expo/useTokenStorage.js.map +0 -1
  34. package/lib/expo/ExpoKeycloakContext.d.ts +0 -32
  35. package/lib/expo/ExpoKeycloakContext.js +0 -47
  36. package/lib/expo/ExpoKeycloakContext.js.map +0 -1
  37. package/lib/expo/ExpoKeycloakProvider.d.ts +0 -37
  38. package/lib/expo/ExpoKeycloakProvider.js +0 -130
  39. package/lib/expo/ExpoKeycloakProvider.js.map +0 -1
  40. package/lib/expo/const.d.ts +0 -33
  41. package/lib/expo/const.js +0 -66
  42. package/lib/expo/const.js.map +0 -1
  43. package/lib/expo/helpers.d.ts +0 -41
  44. package/lib/expo/helpers.js +0 -77
  45. package/lib/expo/helpers.js.map +0 -1
  46. package/lib/expo/index.d.ts +0 -26
  47. package/lib/expo/index.js +0 -50
  48. package/lib/expo/index.js.map +0 -1
  49. package/lib/expo/useExpoKeycloak.d.ts +0 -31
  50. package/lib/expo/useExpoKeycloak.js +0 -62
  51. package/lib/expo/useExpoKeycloak.js.map +0 -1
  52. package/lib/expo/useTokenStorage.d.ts +0 -36
  53. package/lib/expo/useTokenStorage.js +0 -166
  54. package/lib/expo/useTokenStorage.js.map +0 -1
  55. package/src/expo/ExpoKeycloakContext.ts +0 -41
  56. package/src/expo/ExpoKeycloakProvider.tsx +0 -139
  57. package/src/expo/NOTES +0 -1
  58. package/src/expo/const.ts +0 -42
  59. package/src/expo/helpers.ts +0 -74
  60. package/src/expo/useExpoKeycloak.ts +0 -44
  61. package/src/expo/useTokenStorage.ts +0 -168
@@ -1,166 +0,0 @@
1
- (function (global, factory) {
2
- if (typeof define === "function" && define.amd) {
3
- define(["exports", "react-native", "expo-auth-session", "./const", "./helpers", "@react-native-async-storage/async-storage", "react"], factory);
4
- } else if (typeof exports !== "undefined") {
5
- factory(exports, require("react-native"), require("expo-auth-session"), require("./const"), require("./helpers"), require("@react-native-async-storage/async-storage"), require("react"));
6
- } else {
7
- var mod = {
8
- exports: {}
9
- };
10
- factory(mod.exports, global.reactNative, global.expoAuthSession, global._const, global.helpers, global.asyncStorage, global.react);
11
- global.useTokenStorage = mod.exports;
12
- }
13
- })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _reactNative, _expoAuthSession, _const, _helpers, _asyncStorage, _react) {
14
- "use strict";
15
-
16
- Object.defineProperty(_exports, "__esModule", {
17
- value: true
18
- });
19
- _exports.useTokenStorage = useTokenStorage;
20
- /**
21
- * File: /src/expo/useTokenStorage.ts
22
- * Project: @multiplatform.one/keycloak
23
- * File Created: 26-11-2022 06:58:16
24
- * Author: Clay Risser
25
- * -----
26
- * Last Modified: 26-11-2022 08:08:00
27
- * Modified By: Clay Risser
28
- * -----
29
- * Risser Labs LLC (c) Copyright 2022
30
- *
31
- * Licensed under the Apache License, Version 2.0 (the "License");
32
- * you may not use this file except in compliance with the License.
33
- * You may obtain a copy of the License at
34
- *
35
- * http://www.apache.org/licenses/LICENSE-2.0
36
- *
37
- * Unless required by applicable law or agreed to in writing, software
38
- * distributed under the License is distributed on an "AS IS" BASIS,
39
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40
- * See the License for the specific language governing permissions and
41
- * limitations under the License.
42
- */
43
-
44
- const logger = console;
45
- function useTokenStorage(_ref, config, discovery) {
46
- let {
47
- disableAutoRefresh = false,
48
- refreshTimeBuffer = _const.REFRESH_TIME_BUFFER,
49
- tokenStorageKey = _const.TOKEN_STORAGE_KEY
50
- } = _ref;
51
- const [token, setToken] = (0, _react.useState)();
52
- const {
53
- getItem,
54
- setItem,
55
- removeItem
56
- } = (0, _asyncStorage.useAsyncStorage)(tokenStorageKey);
57
- const refreshHandler = (0, _react.useRef)(null);
58
- const appState = (0, _react.useRef)(_reactNative.AppState.currentState);
59
- const refreshTime = (0, _react.useRef)(null);
60
- const tokenData = (0, _react.useRef)(null);
61
- async function updateAndSaveToken(newToken) {
62
- try {
63
- setToken(newToken);
64
- if (newToken !== null) {
65
- const stringifiedValue = JSON.stringify(newToken);
66
- await setItem(stringifiedValue);
67
- } else {
68
- await removeItem();
69
- }
70
- } catch (error) {
71
- logger.error(error);
72
- }
73
- }
74
- async function handleTokenRefresh(token) {
75
- try {
76
- const tokenResponse = await (0, _expoAuthSession.refreshAsync)({
77
- refreshToken: token === null || token === void 0 ? void 0 : token.refreshToken,
78
- ...config
79
- }, discovery);
80
- updateAndSaveToken(tokenResponse);
81
- } catch (err) {
82
- updateAndSaveToken(null);
83
- }
84
- }
85
- (0, _react.useEffect)(() => {
86
- function handleAppState(nextAppState) {
87
- if (appState.current.match(/inactive|background/) && nextAppState === "active") {
88
- if (refreshHandler.current !== null) {
89
- clearTimeout(refreshHandler.current);
90
- const now = (0, _helpers.getCurrentTimeInSeconds)();
91
- if (Number(refreshTime.current) <= now) {
92
- setToken(null);
93
- } else {
94
- const timeout = 1000 * (Number(refreshTime.current) - now);
95
- // @ts-ignore
96
- refreshHandler.current = setTimeout(() => {
97
- handleTokenRefresh(tokenData.current);
98
- }, timeout);
99
- }
100
- }
101
- }
102
- appState.current = nextAppState;
103
- }
104
- const subscription = _reactNative.AppState.addEventListener("change", handleAppState);
105
- return () => {
106
- if (subscription) {
107
- subscription.remove();
108
- } else {
109
- // @ts-ignore
110
- _reactNative.AppState.removeEventListener("change", handleAppState);
111
- }
112
- };
113
- }, []);
114
- (0, _react.useEffect)(() => {
115
- async function getTokenFromStorage() {
116
- try {
117
- const tokenFromStorage = await getItem();
118
- if (!tokenFromStorage) throw new Error("No token in storage");
119
- const token = JSON.parse(tokenFromStorage);
120
- if (!_expoAuthSession.TokenResponse.isTokenFresh(token, -refreshTimeBuffer)) {
121
- handleTokenRefresh(token);
122
- } else {
123
- setToken(token);
124
- }
125
- } catch (error) {
126
- setToken(null);
127
- }
128
- }
129
- if (discovery) getTokenFromStorage();
130
- }, [discovery]);
131
- (0, _react.useEffect)(() => {
132
- // trigger every token update
133
- // @ts-ignore
134
- tokenData.current = token || null;
135
- if (token !== undefined && !disableAutoRefresh) {
136
- if (refreshHandler.current !== null) {
137
- clearTimeout(refreshHandler.current);
138
- }
139
- if (token !== null && token !== void 0 && token.expiresIn) {
140
- const now = (0, _helpers.getCurrentTimeInSeconds)();
141
- // @ts-ignore
142
- refreshTime.current = token.issuedAt + token.expiresIn - refreshTimeBuffer;
143
- const timeout = 1000 * (refreshTime.current - now);
144
- // @ts-ignore
145
- refreshHandler.current = setTimeout(() => {
146
- handleTokenRefresh(token);
147
- }, timeout);
148
- }
149
- if (token === null && tokenData.current !== null) {
150
- var _tokenData$current;
151
- (0, _expoAuthSession.revokeAsync)({
152
- token: (_tokenData$current = tokenData.current) === null || _tokenData$current === void 0 ? void 0 : _tokenData$current.accessToken,
153
- ...config
154
- }, discovery);
155
- _reactNative.Platform.OS === "ios" && (0, _expoAuthSession.dismiss)();
156
- // @ts-ignore
157
- refreshTime.current = null;
158
- // @ts-ignore
159
- tokenData.current = null;
160
- }
161
- }
162
- }, [token]);
163
- return [token || null, updateAndSaveToken];
164
- }
165
- });
166
- //# sourceMappingURL=useTokenStorage.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useTokenStorage.js","names":["logger","console","useTokenStorage","config","discovery","disableAutoRefresh","refreshTimeBuffer","REFRESH_TIME_BUFFER","tokenStorageKey","TOKEN_STORAGE_KEY","token","setToken","useState","getItem","setItem","removeItem","useAsyncStorage","refreshHandler","useRef","appState","AppState","currentState","refreshTime","tokenData","updateAndSaveToken","newToken","stringifiedValue","JSON","stringify","error","handleTokenRefresh","tokenResponse","refreshAsync","refreshToken","err","useEffect","handleAppState","nextAppState","current","match","clearTimeout","now","getCurrentTimeInSeconds","Number","timeout","setTimeout","subscription","addEventListener","remove","removeEventListener","getTokenFromStorage","tokenFromStorage","Error","parse","TokenResponse","isTokenFresh","undefined","expiresIn","issuedAt","revokeAsync","accessToken","Platform","OS","dismiss"],"sources":["../../src/expo/useTokenStorage.ts"],"sourcesContent":["/**\n * File: /src/expo/useTokenStorage.ts\n * Project: @multiplatform.one/keycloak\n * File Created: 26-11-2022 06:58:16\n * Author: Clay Risser\n * -----\n * Last Modified: 26-11-2022 08:08:00\n * Modified By: Clay Risser\n * -----\n * Risser Labs LLC (c) Copyright 2022\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AppState, AppStateStatus, Platform } from \"react-native\";\nimport { DiscoveryDocument, refreshAsync } from \"expo-auth-session\";\nimport { REFRESH_TIME_BUFFER, TOKEN_STORAGE_KEY } from \"./const\";\nimport { dismiss, revokeAsync, TokenResponse } from \"expo-auth-session\";\nimport { getCurrentTimeInSeconds } from \"./helpers\";\nimport { useAsyncStorage } from \"@react-native-async-storage/async-storage\";\nimport { useEffect, useRef, useState } from \"react\";\n\nconst logger = console;\n\nexport function useTokenStorage(\n {\n disableAutoRefresh = false,\n refreshTimeBuffer = REFRESH_TIME_BUFFER,\n tokenStorageKey = TOKEN_STORAGE_KEY,\n },\n config: {\n clientId: string;\n extraParams?: Record<string, string>;\n realm: string;\n redirectUri: string;\n url: string;\n },\n discovery: DiscoveryDocument\n): [TokenResponse | null, (token: TokenResponse | null) => Promise<void>] {\n const [token, setToken] = useState<TokenResponse | null>();\n const { getItem, setItem, removeItem } = useAsyncStorage(tokenStorageKey);\n const refreshHandler = useRef<NodeJS.Timeout>(null);\n const appState = useRef(AppState.currentState);\n const refreshTime = useRef<number>(null);\n const tokenData = useRef<TokenResponse>(null);\n\n async function updateAndSaveToken(newToken: TokenResponse | null) {\n try {\n setToken(newToken);\n if (newToken !== null) {\n const stringifiedValue = JSON.stringify(newToken);\n await setItem(stringifiedValue);\n } else {\n await removeItem();\n }\n } catch (error) {\n logger.error(error);\n }\n }\n\n async function handleTokenRefresh(token: TokenResponse | null) {\n try {\n const tokenResponse = await refreshAsync(\n { refreshToken: token?.refreshToken, ...config },\n discovery\n );\n updateAndSaveToken(tokenResponse);\n } catch (err) {\n updateAndSaveToken(null);\n }\n }\n\n useEffect(() => {\n function handleAppState(nextAppState: AppStateStatus) {\n if (\n appState.current.match(/inactive|background/) &&\n nextAppState === \"active\"\n ) {\n if (refreshHandler.current !== null) {\n clearTimeout(refreshHandler.current);\n const now = getCurrentTimeInSeconds();\n if (Number(refreshTime.current) <= now) {\n setToken(null);\n } else {\n const timeout = 1000 * (Number(refreshTime.current) - now);\n // @ts-ignore\n refreshHandler.current = setTimeout(() => {\n handleTokenRefresh(tokenData.current);\n }, timeout);\n }\n }\n }\n appState.current = nextAppState;\n }\n const subscription = AppState.addEventListener(\"change\", handleAppState);\n return () => {\n if (subscription) {\n subscription.remove();\n } else {\n // @ts-ignore\n AppState.removeEventListener(\"change\", handleAppState);\n }\n };\n }, []);\n\n useEffect(() => {\n async function getTokenFromStorage() {\n try {\n const tokenFromStorage = await getItem();\n if (!tokenFromStorage) throw new Error(\"No token in storage\");\n const token = JSON.parse(tokenFromStorage);\n if (!TokenResponse.isTokenFresh(token, -refreshTimeBuffer)) {\n handleTokenRefresh(token);\n } else {\n setToken(token);\n }\n } catch (error) {\n setToken(null);\n }\n }\n if (discovery) getTokenFromStorage();\n }, [discovery]);\n\n useEffect(() => {\n // trigger every token update\n // @ts-ignore\n tokenData.current = token || null;\n if (token !== undefined && !disableAutoRefresh) {\n if (refreshHandler.current !== null) {\n clearTimeout(refreshHandler.current);\n }\n if (token?.expiresIn) {\n const now = getCurrentTimeInSeconds();\n // @ts-ignore\n refreshTime.current =\n token.issuedAt + token.expiresIn - refreshTimeBuffer;\n const timeout = 1000 * (refreshTime.current - now);\n // @ts-ignore\n refreshHandler.current = setTimeout(() => {\n handleTokenRefresh(token);\n }, timeout);\n }\n if (token === null && tokenData.current !== null) {\n revokeAsync(\n { token: tokenData.current?.accessToken, ...config },\n discovery\n );\n Platform.OS === \"ios\" && dismiss();\n // @ts-ignore\n refreshTime.current = null;\n // @ts-ignore\n tokenData.current = null;\n }\n }\n }, [token]);\n\n return [token || null, updateAndSaveToken];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;EAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;EAUA,MAAMA,MAAM,GAAGC,OAAO;EAEf,SAASC,eAAe,OAM7BC,MAMC,EACDC,SAA4B,EAC4C;IAAA,IAbxE;MACEC,kBAAkB,GAAG,KAAK;MAC1BC,iBAAiB,GAAGC,0BAAmB;MACvCC,eAAe,GAAGC;IACpB,CAAC;IAUD,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAG,IAAAC,eAAQ,GAAwB;IAC1D,MAAM;MAAEC,OAAO;MAAEC,OAAO;MAAEC;IAAW,CAAC,GAAG,IAAAC,6BAAe,EAACR,eAAe,CAAC;IACzE,MAAMS,cAAc,GAAG,IAAAC,aAAM,EAAiB,IAAI,CAAC;IACnD,MAAMC,QAAQ,GAAG,IAAAD,aAAM,EAACE,qBAAQ,CAACC,YAAY,CAAC;IAC9C,MAAMC,WAAW,GAAG,IAAAJ,aAAM,EAAS,IAAI,CAAC;IACxC,MAAMK,SAAS,GAAG,IAAAL,aAAM,EAAgB,IAAI,CAAC;IAE7C,eAAeM,kBAAkB,CAACC,QAA8B,EAAE;MAChE,IAAI;QACFd,QAAQ,CAACc,QAAQ,CAAC;QAClB,IAAIA,QAAQ,KAAK,IAAI,EAAE;UACrB,MAAMC,gBAAgB,GAAGC,IAAI,CAACC,SAAS,CAACH,QAAQ,CAAC;UACjD,MAAMX,OAAO,CAACY,gBAAgB,CAAC;QACjC,CAAC,MAAM;UACL,MAAMX,UAAU,EAAE;QACpB;MACF,CAAC,CAAC,OAAOc,K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hD,KAAK,CAAC+C,SAAS,GAAGnD,iBAAiB;UACtD,MAAMsC,OAAO,GAAG,IAAI,IAAItB,WAAW,CAACgB,OAAO,GAAGG,GAAG,CAAC;UAClD;UACAxB,cAAc,CAACqB,OAAO,GAAGO,UAAU,CAAC,MAAM;YACxCf,kBAAkB,CAACpB,KAAK,CAAC;UAC3B,CAAC,EAAEkC,OAAO,CAAC;QACb;QACA,IAAIlC,KAAK,KAAK,IAAI,IAAIa,SAAS,CAACe,OAAO,KAAK,IAAI,EAAE;UAAA;UAChD,IAAAqB,4BAAW,EACT;YAAEjD,KAAK,wBAAEa,SAAS,CAACe,OAAO,uDAAjB,mBAAmBsB,WAAW;YAAE,GAAGzD;UAAO,CAAC,EACpDC,SAAS,CACV;UACDyD,qBAAQ,CAACC,EAAE,KAAK,KAAK,IAAI,IAAAC,wBAAO,GAAE;UAClC;UACAzC,WAAW,CAACgB,OAAO,GAAG,IAAI;UAC1B;UACAf,SAAS,CAACe,OAAO,GAAG,IAAI;QAC1B;MACF;IACF,CAAC,EAAE,CAAC5B,KAAK,CAAC,CAAC;IAEX,OAAO,CAACA,KAAK,IAAI,IAAI,EAAEc,kBAAkB,CAAC;EAC5C;AAAC"}
@@ -1,41 +0,0 @@
1
- /**
2
- * File: /src/expo/ExpoKeycloakContext.ts
3
- * Project: @multiplatform.one/keycloak
4
- * File Created: 26-11-2022 06:58:16
5
- * Author: Clay Risser
6
- * -----
7
- * Last Modified: 26-11-2022 08:38:25
8
- * Modified By: Clay Risser
9
- * -----
10
- * Risser Labs LLC (c) Copyright 2022
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- import {
26
- AuthRequestPromptOptions,
27
- AuthSessionResult,
28
- TokenResponse,
29
- } from "expo-auth-session";
30
- import { createContext } from "react";
31
- import { KC_INITIAL_VALUE } from "./const";
32
-
33
- export const ExpoKeycloakContext = createContext<{
34
- ready: boolean;
35
- isLoggedIn?: boolean;
36
- login: (
37
- options?: AuthRequestPromptOptions
38
- ) => Promise<AuthSessionResult | undefined>;
39
- logout: () => undefined;
40
- token: TokenResponse | null;
41
- }>(KC_INITIAL_VALUE);
@@ -1,139 +0,0 @@
1
- /**
2
- * File: /src/expo/KeycloakProvider.ts
3
- * Project: @multiplatform.one/keycloak
4
- * File Created: 26-11-2022 06:58:16
5
- * Author: Clay Risser
6
- * -----
7
- * Last Modified: 26-11-2022 08:06:40
8
- * Modified By: Clay Risser
9
- * -----
10
- * Risser Labs LLC (c) Copyright 2022
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- import React, { ReactNode, useCallback, useEffect } from "react";
26
- import { Platform } from "react-native";
27
- import * as AuthSession from "expo-auth-session";
28
- import {
29
- useAuthRequest,
30
- useAutoDiscovery,
31
- AuthRequestConfig,
32
- } from "expo-auth-session";
33
- import { ExpoKeycloakContext } from "./ExpoKeycloakContext";
34
- import { useTokenStorage } from "./useTokenStorage";
35
- import { handleTokenExchange, getRealmURL } from "./helpers";
36
- import { NATIVE_REDIRECT_PATH } from "./const";
37
-
38
- export interface IKeycloakConfiguration extends Partial<AuthRequestConfig> {
39
- children: ReactNode;
40
- clientId: string;
41
- disableAutoRefresh?: boolean;
42
- nativeRedirectPath?: string;
43
- realm: string;
44
- refreshTimeBuffer?: number;
45
- scheme?: string;
46
- tokenStorageKey?: string;
47
- url: string;
48
- }
49
-
50
- export const ExpoKeycloakProvider = ({
51
- realm,
52
- clientId,
53
- url,
54
- extraParams,
55
- children,
56
- ...options
57
- }: IKeycloakConfiguration) => {
58
- const discovery = useAutoDiscovery(getRealmURL({ realm, url }));
59
- const redirectUri = AuthSession.makeRedirectUri({
60
- native: `${options.scheme ?? "exp"}://${
61
- options.nativeRedirectPath ?? NATIVE_REDIRECT_PATH
62
- }`,
63
- useProxy: !options.scheme,
64
- });
65
- const config = {
66
- clientId,
67
- extraParams,
68
- realm,
69
- redirectUri,
70
- url,
71
- };
72
- const [request, response, promptAsync] = useAuthRequest(
73
- { usePKCE: false, ...config },
74
- discovery
75
- );
76
- const [currentToken, updateToken] = useTokenStorage(
77
- options,
78
- config,
79
- discovery!
80
- );
81
-
82
- const handleLogin = useCallback(
83
- (options: AuthSession.AuthRequestPromptOptions = {}) =>
84
- promptAsync(options),
85
- [request]
86
- );
87
-
88
- function handleLogout() {
89
- if (!currentToken) throw new Error("Not logged in.");
90
- try {
91
- if (discovery?.revocationEndpoint) {
92
- AuthSession.revokeAsync(
93
- { token: currentToken?.accessToken, ...config },
94
- discovery
95
- );
96
- }
97
- if (discovery?.endSessionEndpoint) {
98
- fetch(`${discovery.endSessionEndpoint}`, {
99
- method: "POST",
100
- headers: {
101
- "Content-Type": "application/x-www-form-urlencoded",
102
- },
103
- body: `client_id=${clientId}&refresh_token=${currentToken.refreshToken}`,
104
- });
105
- }
106
- if (Platform.OS === "ios") {
107
- AuthSession.dismiss();
108
- }
109
- } catch (error) {
110
- console.log(error);
111
- }
112
- updateToken(null);
113
- return undefined;
114
- }
115
-
116
- useEffect(() => {
117
- if (response) {
118
- handleTokenExchange({ response, discovery: discovery!, config }).then(
119
- updateToken
120
- );
121
- }
122
- }, [response]);
123
-
124
- return (
125
- <ExpoKeycloakContext.Provider
126
- // eslint-disable-next-line react/jsx-no-constructed-context-values
127
- value={{
128
- isLoggedIn: currentToken === undefined ? undefined : !!currentToken,
129
- login: handleLogin,
130
- logout: handleLogout,
131
- ready:
132
- discovery !== null && request !== null && currentToken !== undefined,
133
- token: currentToken,
134
- }}
135
- >
136
- {children}
137
- </ExpoKeycloakContext.Provider>
138
- );
139
- };
package/src/expo/NOTES DELETED
@@ -1 +0,0 @@
1
- This folder contains code by rubhiauliatirta (https://github.com/rubhiauliatirta)
package/src/expo/const.ts DELETED
@@ -1,42 +0,0 @@
1
- /**
2
- * File: /src/expo/const.ts
3
- * Project: @multiplatform.one/keycloak
4
- * File Created: 26-11-2022 06:58:16
5
- * Author: Clay Risser
6
- * -----
7
- * Last Modified: 26-11-2022 08:17:10
8
- * Modified By: Clay Risser
9
- * -----
10
- * Risser Labs LLC (c) Copyright 2022
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- const logger = console;
26
-
27
- export const KC_INITIAL_VALUE = {
28
- ready: false,
29
- isLoggedIn: false,
30
- login: async () => {
31
- logger.error("kc not initialized");
32
- return undefined;
33
- },
34
- logout: () => {
35
- logger.error("not logged in");
36
- return undefined;
37
- },
38
- token: null,
39
- };
40
- export const NATIVE_REDIRECT_PATH = "auth/redirect";
41
- export const TOKEN_STORAGE_KEY = "keycloak_token";
42
- export const REFRESH_TIME_BUFFER = 20;
@@ -1,74 +0,0 @@
1
- /**
2
- * File: /src/expo/helpers.ts
3
- * Project: @multiplatform.one/keycloak
4
- * File Created: 26-11-2022 06:58:16
5
- * Author: Clay Risser
6
- * -----
7
- * Last Modified: 26-11-2022 08:11:15
8
- * Modified By: Clay Risser
9
- * -----
10
- * Risser Labs LLC (c) Copyright 2022
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- import {
26
- AccessTokenRequestConfig,
27
- exchangeCodeAsync,
28
- DiscoveryDocument,
29
- AuthSessionResult,
30
- } from "expo-auth-session";
31
-
32
- export function getRealmURL(config: RealmURLConfig) {
33
- const { url, realm } = config;
34
- const slash = url.endsWith("/") ? "" : "/";
35
- return `${url + slash}realms/${encodeURIComponent(realm)}`;
36
- }
37
-
38
- export function getCurrentTimeInSeconds() {
39
- return Math.floor(Date.now() / 1000);
40
- }
41
-
42
- export async function handleTokenExchange({
43
- response,
44
- discovery,
45
- config,
46
- }: TokenExchange) {
47
- try {
48
- if (response?.type === "success" && !!discovery?.tokenEndpoint) {
49
- return exchangeCodeAsync(
50
- { code: response.params.code, ...config },
51
- discovery
52
- );
53
- }
54
- if (response?.type === "error") return null;
55
- return null;
56
- } catch (error) {
57
- return null;
58
- }
59
- }
60
-
61
- export interface TokenExchange {
62
- response: AuthSessionResult;
63
- discovery: Pick<DiscoveryDocument, "tokenEndpoint">;
64
- config: Omit<AccessTokenRequestConfig, "code"> & { code?: string };
65
- }
66
-
67
- export interface TokenExchangeDiscovery {
68
- tokenEndpoint?: string;
69
- }
70
-
71
- export interface RealmURLConfig {
72
- realm: string;
73
- url: string;
74
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * File: /src/expo/useExpoKeycloak.ts
3
- * Project: @multiplatform.one/keycloak
4
- * File Created: 26-11-2022 06:58:16
5
- * Author: Clay Risser
6
- * -----
7
- * Last Modified: 26-11-2022 08:38:57
8
- * Modified By: Clay Risser
9
- * -----
10
- * Risser Labs LLC (c) Copyright 2022
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
-
25
- import { useContext } from "react";
26
- import { ExpoKeycloakContext } from "./ExpoKeycloakContext";
27
-
28
- export function useExpoKeycloak() {
29
- const {
30
- isLoggedIn,
31
- login,
32
- logout,
33
- ready = false,
34
- token = null,
35
- } = useContext(ExpoKeycloakContext);
36
- return {
37
- isLoggedIn,
38
- login,
39
- logout,
40
- ready,
41
- token: token?.accessToken ?? null,
42
- refreshToken: token?.refreshToken ?? null,
43
- };
44
- }