@dbmx/confy 0.0.80 → 0.0.84

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 (147) hide show
  1. package/esm/components/Confy/index.js +1 -1
  2. package/esm/components/Confy/index.js.map +1 -1
  3. package/esm/components/SelectMemberMenu/index.js +1 -1
  4. package/esm/ducks/utils.js +1 -1
  5. package/esm/forms/utils.js +2 -2
  6. package/esm/hooks/roles.js +1 -1
  7. package/esm/node_modules/@dbmx/auth/dist/index.js +638 -0
  8. package/esm/node_modules/@dbmx/auth/dist/index.js.map +1 -0
  9. package/esm/node_modules/@dbmx/auth/dist/oidc-client.js +107 -0
  10. package/esm/node_modules/@dbmx/auth/dist/oidc-client.js.map +1 -0
  11. package/esm/node_modules/@dbmx/semver/dist/index.esm.js +960 -0
  12. package/esm/node_modules/@dbmx/semver/dist/index.esm.js.map +1 -0
  13. package/esm/node_modules/@dbmx/semver/node_modules/lru-cache/index.js +340 -0
  14. package/esm/node_modules/@dbmx/semver/node_modules/lru-cache/index.js.map +1 -0
  15. package/esm/node_modules/@dbmx/semver/node_modules/yallist/iterator.js +18 -0
  16. package/esm/node_modules/@dbmx/semver/node_modules/yallist/iterator.js.map +1 -0
  17. package/esm/node_modules/@dbmx/semver/node_modules/yallist/yallist.js +430 -0
  18. package/esm/node_modules/@dbmx/semver/node_modules/yallist/yallist.js.map +1 -0
  19. package/esm/node_modules/@dbmx/users/esm/index.js +251 -0
  20. package/esm/node_modules/@dbmx/users/esm/index.js.map +1 -0
  21. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/ap.js +44 -0
  22. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/ap.js.map +1 -0
  23. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/bind.js +36 -0
  24. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/bind.js.map +1 -0
  25. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/curry.js +53 -0
  26. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/curry.js.map +1 -0
  27. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/curryN.js +60 -0
  28. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/curryN.js.map +1 -0
  29. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/difference.js +50 -0
  30. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/difference.js.map +1 -0
  31. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/either.js +46 -0
  32. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/either.js.map +1 -0
  33. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/equals.js +37 -0
  34. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/equals.js.map +1 -0
  35. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/flip.js +37 -0
  36. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/flip.js.map +1 -0
  37. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/identity.js +29 -0
  38. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/identity.js.map +1 -0
  39. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/includes.js +32 -0
  40. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/includes.js.map +1 -0
  41. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_Set.js +201 -0
  42. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_Set.js.map +1 -0
  43. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_arity.js +65 -0
  44. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_arity.js.map +1 -0
  45. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_arrayFromIterator.js +13 -0
  46. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_arrayFromIterator.js.map +1 -0
  47. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_concat.js +37 -0
  48. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_concat.js.map +1 -0
  49. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curry1.js +23 -0
  50. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curry1.js.map +1 -0
  51. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curry2.js +35 -0
  52. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curry2.js.map +1 -0
  53. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curry3.js +53 -0
  54. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curry3.js.map +1 -0
  55. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curryN.js +46 -0
  56. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_curryN.js.map +1 -0
  57. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_dispatchable.js +50 -0
  58. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_dispatchable.js.map +1 -0
  59. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_equals.js +170 -0
  60. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_equals.js.map +1 -0
  61. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_filter.js +18 -0
  62. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_filter.js.map +1 -0
  63. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_functionName.js +8 -0
  64. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_functionName.js.map +1 -0
  65. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_has.js +6 -0
  66. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_has.js.map +1 -0
  67. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_identity.js +6 -0
  68. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_identity.js.map +1 -0
  69. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_includes.js +8 -0
  70. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_includes.js.map +1 -0
  71. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_includesWith.js +17 -0
  72. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_includesWith.js.map +1 -0
  73. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_indexOf.js +71 -0
  74. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_indexOf.js.map +1 -0
  75. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isArguments.js +16 -0
  76. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isArguments.js.map +1 -0
  77. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isArray.js +18 -0
  78. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isArray.js.map +1 -0
  79. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isArrayLike.js +58 -0
  80. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isArrayLike.js.map +1 -0
  81. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isFunction.js +7 -0
  82. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isFunction.js.map +1 -0
  83. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isInteger.js +14 -0
  84. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isInteger.js.map +1 -0
  85. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isPlaceholder.js +6 -0
  86. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isPlaceholder.js.map +1 -0
  87. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isString.js +6 -0
  88. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isString.js.map +1 -0
  89. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isTransformer.js +6 -0
  90. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_isTransformer.js.map +1 -0
  91. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_map.js +15 -0
  92. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_map.js.map +1 -0
  93. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_objectIs.js +17 -0
  94. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_objectIs.js.map +1 -0
  95. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_reduce.js +74 -0
  96. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_reduce.js.map +1 -0
  97. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_xfBase.js +11 -0
  98. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_xfBase.js.map +1 -0
  99. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_xmap.js +29 -0
  100. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_xmap.js.map +1 -0
  101. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_xwrap.js +28 -0
  102. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/internal/_xwrap.js.map +1 -0
  103. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/intersection.js +42 -0
  104. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/intersection.js.map +1 -0
  105. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/is.js +34 -0
  106. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/is.js.map +1 -0
  107. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/keys.js +92 -0
  108. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/keys.js.map +1 -0
  109. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/lift.js +34 -0
  110. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/lift.js.map +1 -0
  111. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/liftN.js +35 -0
  112. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/liftN.js.map +1 -0
  113. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/map.js +68 -0
  114. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/map.js.map +1 -0
  115. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/nth.js +39 -0
  116. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/nth.js.map +1 -0
  117. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/or.js +31 -0
  118. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/or.js.map +1 -0
  119. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/path.js +32 -0
  120. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/path.js.map +1 -0
  121. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/paths.js +47 -0
  122. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/paths.js.map +1 -0
  123. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/prop.js +33 -0
  124. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/prop.js.map +1 -0
  125. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/propEq.js +37 -0
  126. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/propEq.js.map +1 -0
  127. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/type.js +36 -0
  128. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/type.js.map +1 -0
  129. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/uniq.js +27 -0
  130. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/uniq.js.map +1 -0
  131. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/uniqBy.js +46 -0
  132. package/esm/node_modules/@dbmx/users/esm/node_modules/ramda/es/uniqBy.js.map +1 -0
  133. package/esm/node_modules/@dbmx/users/esm/roles.js +132 -0
  134. package/esm/node_modules/@dbmx/users/esm/roles.js.map +1 -0
  135. package/esm/node_modules/jsrsasign/lib/jsrsasign.js +248 -0
  136. package/esm/node_modules/jsrsasign/lib/jsrsasign.js.map +1 -0
  137. package/esm/node_modules/jwt-decode/build/esm/index.js +60 -0
  138. package/esm/node_modules/jwt-decode/build/esm/index.js.map +1 -0
  139. package/esm/node_modules/oidc-client-ts/dist/esm/oidc-client-ts.js +3591 -0
  140. package/esm/node_modules/oidc-client-ts/dist/esm/oidc-client-ts.js.map +1 -0
  141. package/esm/node_modules/ramda/es/internal/_objectAssign.js +32 -0
  142. package/esm/node_modules/ramda/es/internal/_objectAssign.js.map +1 -0
  143. package/esm/node_modules/ramda/es/merge.js +36 -0
  144. package/esm/node_modules/ramda/es/merge.js.map +1 -0
  145. package/esm/utils/authorizations.js +1 -1
  146. package/esm/utils/confy.js +1 -1
  147. package/package.json +3 -3
@@ -0,0 +1,638 @@
1
+ import urljoin from '../../../url-join/lib/url-join.js';
2
+ import axios from 'axios';
3
+ import { nanoid } from 'nanoid';
4
+ import { jws, hextob64u as hextob64u_1, crypto as crypto_1 } from '../../../jsrsasign/lib/jsrsasign.js';
5
+ import { AuthRedirect as AuthRedirect$1 } from './oidc-client.js';
6
+ import '../../../oidc-client-ts/dist/esm/oidc-client-ts.js';
7
+ import merge from '../../../ramda/es/merge.js';
8
+ import prop from '../../../ramda/es/prop.js';
9
+
10
+ let self;
11
+
12
+ const DEFAULT_CONFIG = {
13
+ response_type: 'code',
14
+ scope: 'openid offline_access',
15
+ automaticSilentRenew: true,
16
+ };
17
+
18
+ const SESSION_ENDING_TIMEOUT = 30;
19
+ const OTP_ACCESS_TOKEN_NAME = 'OTP_ACCESS_TOKEN';
20
+
21
+ class EventEmitter {
22
+ constructor() {
23
+ this.listeners = {};
24
+ }
25
+
26
+ on(type, cb) {
27
+ if (!(type in this.listeners)) {
28
+ this.listeners[type] = [];
29
+ }
30
+ this.listeners[type].push(cb);
31
+ }
32
+
33
+ emit(event) {
34
+ if (!(event.type in this.listeners)) return
35
+ const stack = this.listeners[event.type].slice();
36
+ for (let i = 0, l = stack.length; i < l; i++) {
37
+ stack[i].call(this, event);
38
+ }
39
+ }
40
+ }
41
+
42
+ class Auth extends EventEmitter {
43
+ constructor(options, tokens) {
44
+ super();
45
+ this.options = options;
46
+ this.tokens = tokens;
47
+ }
48
+
49
+ registerLogout(delay) {
50
+ if (!this.tokens) return
51
+ this.logoutTimer = setTimeout(() => this.signOut(), delay || this.tokens.expires_in * 1000);
52
+ }
53
+
54
+ registerEndSession() {
55
+ if (!this.tokens) return
56
+ if (this.refreshSessionTimer) clearTimeout(this.refreshSessionTimer);
57
+ this.refreshSessionTimer = setTimeout(() => {
58
+ this.checkSession()
59
+ .then(() => this.emit({ type: 'sessionEnding', timeout: SESSION_ENDING_TIMEOUT }))
60
+ .catch(() => this.signOut());
61
+ }, this.getSessionExpirationDelay(this.tokens));
62
+ }
63
+
64
+ async refreshTokens() {
65
+ if (!this.tokens) return
66
+ try {
67
+ const url = '/api/authOTP/refreshTokens';
68
+ const res = await fetch(url, {
69
+ method: 'POST',
70
+ headers: {
71
+ 'Content-Type': 'application/json',
72
+ Authorization: `Bearer ${this.tokens.access_token}`,
73
+ },
74
+ body: JSON.stringify({
75
+ refreshToken: this.tokens.refresh_token,
76
+ }),
77
+ });
78
+ if (!res.ok) {
79
+ const errorData = await res.json();
80
+ throw { response: { data: errorData }, status: res.status }
81
+ }
82
+ const { accessToken, refreshToken, idToken } = await res.json();
83
+ const id_token = jws.JWS.parse(idToken);
84
+ this.tokens = {
85
+ access_token: accessToken,
86
+ refresh_token: refreshToken,
87
+ expires_in: id_token.payloadObj.exp,
88
+ };
89
+
90
+ this.emit({
91
+ ...this.tokens,
92
+ type: 'tokensRefreshed',
93
+ payload: id_token.payloadObj,
94
+ });
95
+
96
+ this.setAccessTokenCookie(accessToken, this.tokens);
97
+ this.registerEndSession();
98
+
99
+ return { ...this.tokens, payload: id_token.payloadObj }
100
+ } catch (err) {
101
+ this.signOut();
102
+ if (err.response?.data) return this.emit({ type: 'error', ...err.response.data })
103
+ throw err
104
+ }
105
+ }
106
+
107
+ getSessionExpirationDelay(token) {
108
+ if (!token?.expires_in) return 3570000
109
+ return Math.max(0, token.expires_in * 1000 - Date.now() - SESSION_ENDING_TIMEOUT * 1000)
110
+ }
111
+
112
+ autoRefreshTokens() {
113
+ if (!this.tokens) return
114
+ if (this.autoRefreshTimer) clearTimeout(this.autoRefreshTimer); // autoRefreshTimer can be called by a client
115
+ this.autoRefreshTimer = setTimeout(() => this.refreshTokens(), this.getSessionExpirationDelay(this.tokens));
116
+ }
117
+
118
+ async signOut() {
119
+ await this.endSession();
120
+ // await this.revokeRefreshToken()
121
+ this.tokens = null;
122
+ this.logoutTimer = clearTimeout(this.logoutTimer);
123
+ this.autoRefreshTimer = clearTimeout(this.autoRefreshTimer);
124
+ this.refreshSessionTimer = clearTimeout(this.refreshSessionTimer);
125
+ }
126
+ }
127
+
128
+ class Oidc extends EventEmitter {
129
+ constructor(options, tokens) {
130
+ super();
131
+ if (!this.isValidConfig(options)) throw new Error(`Invalid setup`)
132
+ this.options = options;
133
+ this.tokens = tokens;
134
+ }
135
+
136
+ get configurationUrl() {
137
+ return urljoin(this.options.authority, '/.well-known/openid-configuration')
138
+ }
139
+
140
+ decodeState(state) {
141
+ return state && JSON.parse(atob(state))
142
+ }
143
+
144
+ makeState() {
145
+ const state = {
146
+ srcUrl: window.location.href,
147
+ };
148
+ return btoa(JSON.stringify(state))
149
+ }
150
+
151
+ getSessionExpirationDelay(token) {
152
+ if (!token?.expires_in) return 3570000
153
+ return token.expires_in * 1000 - SESSION_ENDING_TIMEOUT * 1000
154
+ }
155
+
156
+ get authorizationUrl() {
157
+ this.code_verifier = `${nanoid()}-${nanoid()}`;
158
+ localStorage.setItem('code_verifier', this.code_verifier);
159
+ this.code_challenge = hextob64u_1(crypto_1.Util.hashString(this.code_verifier, 'SHA256'));
160
+ return urljoin(
161
+ this.authConfig.authorization_endpoint,
162
+ `?client_id=${this.options.client_id}`,
163
+ `?scope=${this.options.scope}`,
164
+ `?redirect_uri=${this.options.redirect_uri}`,
165
+ `?response_type=${this.options.response_type}`,
166
+ `?nonce=${nanoid()}`,
167
+ `?code_challenge=${this.code_challenge}`,
168
+ `?code_challenge_method=S256`,
169
+ `?state=${this.makeState()}`,
170
+ )
171
+ }
172
+
173
+ get endSessionUrl() {
174
+ return urljoin(
175
+ this.authConfig.end_session_endpoint,
176
+ `?client_id=${this.options.client_id}`,
177
+ `?id_token_hint=${this.tokens?.id_token}`,
178
+ )
179
+ }
180
+
181
+ get endSessionUrlWithRedirect() {
182
+ return urljoin(this.endSessionUrl, `&post_logout_redirect_uri=${this.options.redirect_uri}`)
183
+ }
184
+
185
+ async getOpenIdConfiguration() {
186
+ try {
187
+ const res = await axios.get(this.configurationUrl).then(prop('data'));
188
+ this.emit({ type: 'configurationLoaded', config: this.authConfig });
189
+ return res
190
+ } catch (err) {
191
+ throw new Error(`Cannot load openid configuration from ${this.configurationUrl}`)
192
+ }
193
+ }
194
+
195
+ async revoke(type) {
196
+ const url = this.authConfig.revocation_endpoint;
197
+ const config = {
198
+ headers: {
199
+ 'Content-Type': 'application/x-www-form-urlencoded',
200
+ },
201
+ };
202
+ const params = new URLSearchParams();
203
+ params.append('client_id', this.options.client_id);
204
+ params.append('token', this.tokens[type]);
205
+ params.append('token_type_hint', type);
206
+
207
+ try {
208
+ await axios.post(url, params, config).then(prop('data'));
209
+ } catch (err) {
210
+ if (err.response?.data) return this.emit({ type: 'error', ...err.response.data })
211
+ throw err
212
+ }
213
+ }
214
+
215
+ revokeAccessToken() {
216
+ return this.revoke('access_token')
217
+ }
218
+
219
+ revokeRefreshToken() {
220
+ return this.revoke('refresh_token')
221
+ }
222
+
223
+ registerLogout(delay) {
224
+ if (!this.tokens) return
225
+ this.logoutTimer = setTimeout(() => this.signOut(), delay || this.tokens.expires_in * 1000);
226
+ }
227
+
228
+ autoRefreshTokens() {
229
+ if (!this.tokens) return
230
+ if (this.autoRefreshTimer) clearTimeout(this.autoRefreshTimer); // autoRefreshTimer can be called by a client
231
+ this.autoRefreshTimer = setTimeout(() => this.refreshTokens(), this.getSessionExpirationDelay(this.tokens));
232
+ }
233
+
234
+ async checkSession() {
235
+ if (!this.tokens) return
236
+ const url = urljoin(this.authConfig.userinfo_endpoint, `?access_token=${this.tokens.access_token}`);
237
+ return axios.get(url)
238
+ }
239
+
240
+ manageSession(delay = 60 * 1000) {
241
+ if (!this.tokens) return
242
+ setTimeout(() => {
243
+ this.checkSession()
244
+ .then(() => this.manageSession(delay))
245
+ .catch(() => this.signOut());
246
+ }, delay);
247
+ }
248
+
249
+ registerEndSession() {
250
+ if (!this.tokens) return
251
+ if (this.refreshSessionTimer) clearTimeout(this.refreshSessionTimer);
252
+ this.refreshSessionTimer = setTimeout(() => {
253
+ this.checkSession()
254
+ .then(() => this.emit({ type: 'sessionEnding', timeout: SESSION_ENDING_TIMEOUT }))
255
+ .catch(() => this.signOut());
256
+ }, this.getSessionExpirationDelay(this.tokens));
257
+ }
258
+
259
+ isValidConfig(config) {
260
+ return config?.authority
261
+ }
262
+
263
+ async getAccessTokenFromCode(code) {
264
+ const params = new URLSearchParams();
265
+ params.append('code', code);
266
+ params.append('client_id', this.options.client_id);
267
+ params.append('grant_type', 'authorization_code');
268
+ params.append('redirect_uri', this.options.redirect_uri);
269
+ params.append('code_verifier', this.code_verifier);
270
+ const config = {
271
+ headers: {
272
+ 'Content-Type': 'application/x-www-form-urlencoded',
273
+ },
274
+ };
275
+ const url = this.authConfig.token_endpoint;
276
+ try {
277
+ this.tokens = await axios.post(url, params, config).then(prop('data'));
278
+ const id_token = jws.JWS.parse(this.tokens.id_token);
279
+ if (this.options.autoRefresh) this.autoRefreshTokens();
280
+ else this.registerEndSession();
281
+ this.emit({ ...this.tokens, type: 'signedIn', payload: id_token.payloadObj });
282
+ return { ...this.tokens, payload: id_token.payloadObj }
283
+ } catch (err) {
284
+ if (err.response?.data) return this.emit({ type: 'error', ...err.response.data })
285
+ throw err
286
+ }
287
+ }
288
+
289
+ async refreshTokens() {
290
+ if (!this.tokens) return
291
+ const params = new URLSearchParams();
292
+ params.append('client_id', this.options.client_id);
293
+ params.append('grant_type', 'refresh_token');
294
+ params.append('refresh_token', this.tokens.refresh_token);
295
+ const config = {
296
+ headers: {
297
+ 'Content-Type': 'application/x-www-form-urlencoded',
298
+ },
299
+ };
300
+ const url = this.authConfig.token_endpoint;
301
+ try {
302
+ this.tokens = await axios.post(url, params, config).then(prop('data'));
303
+ const id_token = jws.JWS.parse(this.tokens.id_token);
304
+ if (this.options.autoRefresh) this.autoRefreshTokens();
305
+ else this.registerEndSession();
306
+ this.emit({ ...this.tokens, type: 'tokensRefreshed', payload: id_token.payloadObj });
307
+ return { ...this.tokens, payload: id_token.payloadObj }
308
+ } catch (err) {
309
+ this.signOut();
310
+ if (err.response?.data) return this.emit({ type: 'error', ...err.response.data })
311
+ throw err
312
+ }
313
+ }
314
+
315
+ async signOut() {
316
+ await this.endSession();
317
+ // await this.revokeRefreshToken()
318
+ this.tokens = null;
319
+ this.logoutTimer = clearTimeout(this.logoutTimer);
320
+ this.autoRefreshTimer = clearTimeout(this.autoRefreshTimer);
321
+ this.refreshSessionTimer = clearTimeout(this.refreshSessionTimer);
322
+ }
323
+
324
+ async init(tokens) {
325
+ this.authConfig = await this.getOpenIdConfiguration();
326
+ if (tokens || tokens === null) this.tokens = tokens;
327
+ this.emit({ type: 'configurationLoaded', config: this.authConfig });
328
+ return this
329
+ }
330
+ }
331
+
332
+ class OidcIFrame extends Oidc {
333
+ async signIn(params) {
334
+ const options = merge(this.options, params);
335
+ try {
336
+ if (!this.authConfig) await this.init();
337
+ let iframe;
338
+ if (!options.frame) iframe = this.loginIframe;
339
+ else {
340
+ iframe = window.document.getElementById(options.frame);
341
+ this.loginIframe = iframe;
342
+ }
343
+ if (!iframe) throw new Error(`Cannot get login iframe ${options.frame}`)
344
+ iframe.src = this.authorizationUrl;
345
+ } catch (err) {
346
+ this.emit({ type: 'error', error_description: err.message || err.toString() });
347
+ }
348
+ }
349
+
350
+ async endSession() {
351
+ if (!this.tokens) return
352
+ let iframe = document.querySelector('iframe[id=logout-frame]');
353
+ if (!iframe) {
354
+ iframe = window.document.createElement('iframe');
355
+ iframe.id = 'logout-frame';
356
+ iframe.style = 'position: absolute; width: 1px; height: 1px; inset: -9999px; display: none;';
357
+ window.document.body.appendChild(iframe);
358
+ }
359
+ iframe.src = this.endSessionUrl;
360
+ }
361
+ }
362
+
363
+ class OidcLib extends Oidc {
364
+ async init() {
365
+ try {
366
+ this.code_verifier = localStorage.getItem('code_verifier');
367
+ if (!this.authConfig) this.authConfig = await this.getOpenIdConfiguration();
368
+ this.emit({ type: 'configurationLoaded', config: this.authConfig });
369
+ } catch (err) {
370
+ this.emit({ type: 'error', error_description: err.message || err.toString() });
371
+ }
372
+ }
373
+
374
+ async signIn() {
375
+ try {
376
+ if (!this.authConfig) await this.init();
377
+ window.location.replace(this.authorizationUrl);
378
+ } catch (err) {
379
+ this.emit({ type: 'error', error_description: err.message || err.toString() });
380
+ }
381
+ }
382
+
383
+ async endSession() {
384
+ try {
385
+ if (!this.tokens) return
386
+ this.emit({ type: 'signedOut' });
387
+ window.location.replace(this.endSessionUrlWithRedirect);
388
+ } catch (err) {
389
+ this.emit({ type: 'error', error_description: err.message || err.toString() });
390
+ }
391
+ }
392
+ }
393
+
394
+ class OTP extends Auth {
395
+ init(tokens) {
396
+ this.tokens = tokens;
397
+ return this
398
+ }
399
+
400
+ async endSession() {
401
+ if (!this.tokens) return
402
+
403
+ try {
404
+ const url = '/api/authOTP/logout';
405
+ const res = await fetch(url, {
406
+ method: 'POST',
407
+ headers: {
408
+ 'Content-Type': 'application/json',
409
+ },
410
+ body: JSON.stringify({
411
+ accessToken: this.tokens.access_token,
412
+ }),
413
+ });
414
+
415
+ if (!res.ok) {
416
+ const errorData = await res.json();
417
+ throw { response: { data: errorData }, status: res.status }
418
+ }
419
+
420
+ this.removeAccessTokenCookie();
421
+ this.emit({ type: 'signedOut' });
422
+ } catch (err) {
423
+ if (err.response?.data) return this.emit({ type: 'error', ...err.response.data })
424
+ throw err
425
+ }
426
+ }
427
+
428
+ async checkSession() {
429
+ if (!this.tokens) return
430
+
431
+ const url = '/api/authOTP/getMe';
432
+
433
+ const res = await fetch(url, {
434
+ method: 'POST',
435
+ headers: {
436
+ 'Content-Type': 'application/json',
437
+ },
438
+ body: JSON.stringify({
439
+ accessToken: this.tokens.access_token,
440
+ }),
441
+ });
442
+
443
+ if (!res.ok) {
444
+ throw new Error(`Request failed with status ${res.status}`)
445
+ }
446
+
447
+ return res.json()
448
+ }
449
+
450
+ getHostname() {
451
+ return location.hostname
452
+ }
453
+
454
+ getBaseDomain() {
455
+ const hostname = this.getHostname();
456
+ const parts = hostname.split('.');
457
+ if (parts.length >= 2) {
458
+ // take the last two parts for base domain (x.com)
459
+ const base = parts.slice(-2).join('.');
460
+ return `.${base}`
461
+ }
462
+ return hostname
463
+ }
464
+
465
+ getCookieDomain() {
466
+ const hostname = this.getHostname();
467
+ return hostname === 'localhost' || hostname === '0.0.0.0' || hostname === '127.0.0.1'
468
+ ? ''
469
+ : `domain=${this.getBaseDomain()};`
470
+ }
471
+
472
+ setAccessTokenCookie(value, token) {
473
+ const nowSeconds = Math.floor(Date.now() / 1000);
474
+ const maxAge = Math.max(0, token.expires_in - nowSeconds);
475
+ const domain = this.getCookieDomain();
476
+ const addationalParams = domain ? 'Secure; SameSite=None;' : '';
477
+ document.cookie = `${OTP_ACCESS_TOKEN_NAME}=${value}; max-age=${maxAge}; ${domain} path=/; ${addationalParams}`;
478
+ }
479
+
480
+ getAccessTokenCookie() {
481
+ const cookies = document.cookie.split(';');
482
+ for (let cookie of cookies) {
483
+ const [cookieName, cookieValue] = cookie.split('=');
484
+ if (cookieName.trim() === OTP_ACCESS_TOKEN_NAME) {
485
+ const value = cookieValue?.trim();
486
+ return value ? value : null
487
+ }
488
+ }
489
+ return null
490
+ }
491
+
492
+ removeAccessTokenCookie() {
493
+ const domain = this.getCookieDomain();
494
+ document.cookie = `${OTP_ACCESS_TOKEN_NAME}=; ${domain} path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
495
+ }
496
+
497
+ async userTokens(tokens) {
498
+ const { accessToken, refreshToken, idToken } = tokens;
499
+
500
+ try {
501
+ const idTokenPayload = jws.JWS.parse(idToken);
502
+
503
+ this.tokens = {
504
+ access_token: accessToken,
505
+ refresh_token: refreshToken,
506
+ expires_in: idTokenPayload.payloadObj.exp,
507
+ };
508
+
509
+ const signedInOptions = {
510
+ ...this.tokens,
511
+ payload: idTokenPayload.payloadObj,
512
+ };
513
+
514
+ this.emit({
515
+ type: 'signedIn',
516
+ ...signedInOptions,
517
+ });
518
+
519
+ this.setAccessTokenCookie(accessToken, this.tokens);
520
+ this.registerEndSession();
521
+
522
+ return signedInOptions
523
+ } catch (error) {
524
+ // this.emit({ type: 'error', error_description: err.message || err.toString() })
525
+ this.emit({ type: 'error', error: 'failed' });
526
+ }
527
+ }
528
+ }
529
+
530
+ /**
531
+ * Factory function to create an instance of AuthIFrame, enabling authentication using an iframe
532
+ *
533
+ * @function AuthIFrame
534
+ * @tag auth
535
+ * @param {Object} - [options]: Configuration options for authentication
536
+ * @param {Function} - [onSignIn]: Callback function invoked on successful sign-in
537
+ * @param {Function} - [onSignOut]: Callback function invoked on sign-out
538
+ * @param {Function} - [onAuthError]: Callback function invoked on authentication error
539
+ * @param {string} - [options.authority]: The authority URL for authentication
540
+ * @param {string} - [options.redirect_uri]: The redirect URI for authentication
541
+ * @param {string} - [options.client_id]: The client ID for authentication
542
+ * @return {Auth} - An instance of Auth
543
+ */
544
+ const AuthIFrame = ({ onSignIn, onSignOut, onAuthError, ...config } = {}) => {
545
+ let signInTimer;
546
+ if (!self) {
547
+ const oidc = new OidcIFrame(Object.assign({}, DEFAULT_CONFIG, config, { redirect_uri: config.redirect_uri }));
548
+ if (onSignIn) oidc.on('signedIn', onSignIn);
549
+ if (onSignOut) oidc.on('signedOut', onSignOut);
550
+ if (onAuthError) oidc.on('error', onAuthError);
551
+ window.onmessage = e => {
552
+ if (e.origin === new URL(config.authority).origin) {
553
+ switch (e.data.type) {
554
+ case 'code':
555
+ oidc.getAccessTokenFromCode(e.data.code);
556
+ break
557
+ case 'signedOut':
558
+ oidc.emit({ type: 'signedOut' });
559
+ break
560
+ case 'session_not_found':
561
+ oidc.emit({
562
+ type: 'error',
563
+ error: 'session_not_found',
564
+ error_description: 'Session was expired, please sign in again',
565
+ });
566
+ if (signInTimer) clearTimeout(signInTimer);
567
+ signInTimer = setTimeout(() => oidc.signIn(), 1000);
568
+ break
569
+ default:
570
+ oidc.emit({ ...e.data, type: 'error' });
571
+ }
572
+ }
573
+ };
574
+ self = oidc;
575
+ }
576
+ return self
577
+ };
578
+
579
+ /**
580
+ * Factory function to create an instance of AuthRedirect, enabling redirect authentication.
581
+ *
582
+ * @function AuthRedirect
583
+ * @tag auth
584
+ * @param {Object} - [options]: Configuration options for authentication
585
+ * @param {Function} - [onSignIn]: Callback function invoked on successful sign-in
586
+ * @param {Function} - [onSignOut]: Callback function invoked on sign-out
587
+ * @param {Function} - [onAuthError]: Callback function invoked on authentication error
588
+ * @param {string} - [options.redirect_uri]: The redirect URI for authentication
589
+ * @return {AuthRedirect} - An instance of Auth
590
+ */
591
+ const AuthRedirect = ({ onSignIn, onSignOut, onAuthError, ...config } = {}) => {
592
+ if (config.lib === 'oidc-client-ts') return AuthRedirect$1({ onSignIn, onSignOut, onAuthError, ...config })
593
+
594
+ if (!self) {
595
+ const oidc = new OidcLib(Object.assign({}, DEFAULT_CONFIG, config, { redirect_uri: config.redirect_uri }));
596
+ if (onSignIn) oidc.on('signedIn', onSignIn);
597
+ if (onSignOut) oidc.on('signedOut', onSignOut);
598
+ if (onAuthError) oidc.on('error', onAuthError);
599
+
600
+ window.onmessage = async e => {
601
+ switch (e.data.type) {
602
+ case 'code':
603
+ await oidc.init();
604
+ oidc.getAccessTokenFromCode(e.data.code);
605
+ break
606
+ }
607
+ };
608
+
609
+ self = oidc;
610
+ }
611
+ return self
612
+ };
613
+
614
+ /**
615
+ * Factory function to create an instance of AuthOTP, enabling OTP authentication.
616
+ *
617
+ * @function AuthOTP
618
+ * @tag auth
619
+ * @param {Object} - [options]: Configuration options for authentication
620
+ * @param {Function} - [onSignIn]: Callback function invoked on successful sign-in
621
+ * @param {Function} - [onSignOut]: Callback function invoked on sign-out
622
+ * @param {Function} - [onAuthError]: Callback function invoked on authentication error
623
+ * @return {AuthOTP} - An instance of AuthOTP
624
+ */
625
+ const AuthOTP = ({ onSignIn, onSignOut, onAuthError, ...config } = {}) => {
626
+ if (!self) {
627
+ const otp = new OTP(Object.assign({}, config));
628
+ if (onSignIn) otp.on('signedIn', onSignIn);
629
+ if (onSignOut) otp.on('signedOut', onSignOut);
630
+ if (onAuthError) otp.on('error', onAuthError);
631
+
632
+ self = otp;
633
+ }
634
+ return self
635
+ };
636
+
637
+ export { AuthIFrame, AuthOTP, AuthRedirect, self };
638
+ //# sourceMappingURL=index.js.map