@luizleon/sf.prefeiturasp.vuecomponents 0.0.64 → 0.0.66

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luizleon/sf.prefeiturasp.vuecomponents",
3
- "version": "0.0.64",
3
+ "version": "0.0.66",
4
4
  "type": "module",
5
5
  "main": "dist/sf.prefeiturasp.vuecomponents.umd.js",
6
6
  "module": "dist/sf.prefeiturasp.vuecomponents.es.js",
@@ -18,6 +18,7 @@
18
18
  "vue-currency-input": "3.0.2"
19
19
  },
20
20
  "devDependencies": {
21
+ "@noble/hashes": "1.5.0",
21
22
  "@types/dompurify": "3.0.5",
22
23
  "@vitejs/plugin-vue": "5.0.4",
23
24
  "base64-js": "1.5.1",
@@ -19,7 +19,6 @@ class AxiosClient {
19
19
  }
20
20
  keycloak: Keycloak;
21
21
  axios: AxiosInstance;
22
- private fileName: string = "silent-login.html";
23
22
 
24
23
  private ParseAppResult(data: any): AppResult<any> | null {
25
24
  try {
@@ -87,118 +86,11 @@ class AxiosClient {
87
86
  }
88
87
 
89
88
  private async SilentLogin() {
90
- return new Promise<boolean>(async (res) => {
91
- if (!this.isTokenExpired) {
92
- res(true);
93
- return;
94
- }
95
- const iframe = this.GenerateIframe();
96
- const fn = (e: MessageEvent) => {
97
- document.body.removeChild(iframe);
98
- window.removeEventListener("message", fn);
99
- if (e.origin !== location.origin) {
100
- res(false);
101
- return;
102
- }
103
- const action = e.data.action;
104
- const code = e.data.code;
105
- const state = e.data.state;
106
- const error = e.data.error;
107
- if (
108
- action !== "silent-login-iframe-result" ||
109
- error === "login_required" ||
110
- !code
111
- ) {
112
- res(false);
113
- return;
114
- }
115
- const req = new XMLHttpRequest();
116
- req.open("POST", this.tokenExchangeUrl, true);
117
- req.setRequestHeader(
118
- "Content-type",
119
- "application/x-www-form-urlencoded"
120
- );
121
- req.onreadystatechange = () => {
122
- if (req.readyState === 4) {
123
- if (req.status === 200) {
124
- const tokenResponse = JSON.parse(req.responseText);
125
- this.keycloak.setToken(
126
- tokenResponse.access_token,
127
- null,
128
- tokenResponse.id_token,
129
- new Date().getTime()
130
- );
131
- res(true);
132
- return;
133
- }
134
- res(false);
135
- }
136
- };
137
- req.send(this.TokenExchangeParams(code, state));
138
- };
139
- window.addEventListener("message", fn);
140
- }).then((r) => {
141
- if (!r) {
142
- this.keycloak.clearToken();
143
- this.keycloak.login();
144
- }
145
- return r;
89
+ return this.keycloak.updateToken(10).catch((_) => {
90
+ this.keycloak.clearToken();
91
+ this.keycloak.login();
146
92
  });
147
93
  }
148
-
149
- private GetCodeVerifierFromState(state: string) {
150
- const key = `kc-callback-${state}`;
151
- const stored = localStorage.getItem(key);
152
- if (stored) {
153
- localStorage.removeItem(key);
154
- return JSON.parse(stored).pkceCodeVerifier;
155
- }
156
- }
157
-
158
- private TokenExchangeParams(code: string, state: string) {
159
- let params = `code=${code}&grant_type=authorization_code`;
160
- params += `&client_id=${encodeURIComponent(
161
- this.keycloak.clientId!
162
- )}`;
163
- params += `&redirect_uri=${this.silentLoginRedirectUrl}`;
164
- params += `&code_verifier=${this.GetCodeVerifierFromState(
165
- state
166
- )}`;
167
- return params;
168
- }
169
-
170
- private GenerateIframe() {
171
- const iframe = document.createElement("iframe");
172
- iframe.src =
173
- this.keycloak.createLoginUrl({
174
- prompt: "none",
175
- redirectUri: this.silentLoginRedirectUrl,
176
- }) ?? "";
177
- document.body.appendChild(iframe);
178
- return iframe;
179
- }
180
-
181
- private get silentLoginRedirectUrl() {
182
- return `${location.origin}/${this.fileName}`;
183
- }
184
-
185
- private get tokenExchangeUrl() {
186
- return `${this.keycloak.authServerUrl}/realms/${this.keycloak.realm}/protocol/openid-connect/token`;
187
- }
188
-
189
- private get isTokenExpired() {
190
- const token = this.keycloak.tokenParsed;
191
- const exp = token ? token["exp"] : null;
192
-
193
- if (!token || !exp) return true;
194
-
195
- const expiresIn =
196
- exp -
197
- Math.ceil(new Date().getTime() / 1000) +
198
- (this.keycloak.timeSkew ?? 0);
199
-
200
- return expiresIn < 10;
201
- }
202
94
  }
203
95
 
204
96
  const UseAxiosClient = (keycloak: Keycloak, baseURL: string) =>
package/src/index.ts CHANGED
@@ -15,7 +15,10 @@ import {
15
15
  UseAlertService,
16
16
  UseConfirmService,
17
17
  } from "./services/dialogService";
18
- import { UseAuthService } from "./services/authService";
18
+ import {
19
+ StartAuthService,
20
+ UseAuthService,
21
+ } from "./services/authService";
19
22
  import { AppResult } from "./common/appResult";
20
23
 
21
24
  import "./style/componentes.scss";
@@ -32,6 +35,7 @@ export {
32
35
  SfMessage,
33
36
  SfTooltip,
34
37
  SfPanelMenu,
38
+ StartAuthService,
35
39
  UseAuthService,
36
40
  UseNavMenuService,
37
41
  UseConfirmService,