@luizleon/sf.prefeiturasp.vuecomponents 0.0.35 → 0.0.37
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/dist/axios/axiosClient.d.ts +30 -0
- package/dist/index.d.ts +2 -1
- package/dist/lib.es.js +3522 -1988
- package/dist/lib.es.js.map +1 -1
- package/dist/lib.umd.js +40 -36
- package/dist/lib.umd.js.map +1 -1
- package/dist/style.css +1 -1
- package/package.json +2 -1
- package/src/axios/axiosClient.ts +207 -0
- package/src/index.ts +4 -23
- package/src/services/authService.ts +11 -1
- package/src/style/tema.scss +4 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luizleon/sf.prefeiturasp.vuecomponents",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.37",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/lib.umd.js",
|
|
6
6
|
"module": "dist/lib.es.js",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"air-datepicker": "3.3.1",
|
|
13
|
+
"axios": "1.6.5",
|
|
13
14
|
"date-fns": "2.29.3",
|
|
14
15
|
"sweetalert2": "11.4.8",
|
|
15
16
|
"vue-currency-input": "3.0.2"
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import _axios, { AxiosInstance, AxiosResponse } from "axios";
|
|
2
|
+
import { AppResult } from "../common/appResult";
|
|
3
|
+
import Keycloak from "../keycloak";
|
|
4
|
+
|
|
5
|
+
class AxiosClient {
|
|
6
|
+
constructor(keycloak: Keycloak) {
|
|
7
|
+
this.keycloak = keycloak;
|
|
8
|
+
this.axios = _axios.create();
|
|
9
|
+
this.axios.defaults.baseURL = "https://localhost:5001/api";
|
|
10
|
+
this.axios.defaults.headers.common["content-type"] =
|
|
11
|
+
"application/json";
|
|
12
|
+
this.axios.defaults.timeout = 1000 * 60 * 5;
|
|
13
|
+
this.axios.interceptors.request.use(async (config) => {
|
|
14
|
+
await this.SilentLogin();
|
|
15
|
+
const token = this.keycloak.token;
|
|
16
|
+
config.headers.Authorization = `Bearer ${token}`;
|
|
17
|
+
return config;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
keycloak: Keycloak;
|
|
21
|
+
axios: AxiosInstance;
|
|
22
|
+
private fileName: string = "silent-login.html";
|
|
23
|
+
|
|
24
|
+
private ParseAppResult(data: any): AppResult<any> | null {
|
|
25
|
+
try {
|
|
26
|
+
const check1 = typeof data.hasSuccess === "boolean";
|
|
27
|
+
const check2 = typeof data.hasError === "boolean";
|
|
28
|
+
const check3 = typeof data.httpStatusCode === "number";
|
|
29
|
+
const check4 = Array.isArray(data.errors);
|
|
30
|
+
if (check1 && check2 && check3 && check4) return data;
|
|
31
|
+
return null;
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Usar após 'then' (httpStatusCode 2xx) para obter o resultado da requisição.
|
|
39
|
+
* @param r
|
|
40
|
+
*/
|
|
41
|
+
HandleThen<T>(r: AxiosResponse<any, any>): AppResult<T> {
|
|
42
|
+
const result = new AppResult<T>();
|
|
43
|
+
const parsed = this.ParseAppResult(r.data);
|
|
44
|
+
if (parsed === null) {
|
|
45
|
+
// Foi recebido um httpStatusCode 2xx, mas não é um AppResult.
|
|
46
|
+
// Então, o resultado é o próprio 'data'.
|
|
47
|
+
// Por padrão, httpStatusCode é 200.
|
|
48
|
+
result.value = r.data;
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
if (parsed.hasError) {
|
|
52
|
+
// Foi recebido um httpStatusCode 2xx e é um AppResult.
|
|
53
|
+
// Porém, o resultado é um erro.
|
|
54
|
+
// Então, adiciona os erros e o httpStatusCode.
|
|
55
|
+
parsed.errors.forEach((e) => result.WithError(e));
|
|
56
|
+
result.httpStatusCode = parsed.httpStatusCode;
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
// Foi recebido um httpStatusCode 2xx e é um AppResult.
|
|
60
|
+
// O resultado é um sucesso.
|
|
61
|
+
// Então, adiciona o valor.
|
|
62
|
+
// Por padrão, httpStatusCode é 200.
|
|
63
|
+
result.value = r.data.value;
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Usar após 'catch' (httpStatusCode 4xx ou 5xx) para obter o resultado da requisição.
|
|
69
|
+
* @param r
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
HandleCatch<T>(r: any): AppResult<T> {
|
|
73
|
+
const result = new AppResult<T>();
|
|
74
|
+
const parsed = this.ParseAppResult(r.response?.data);
|
|
75
|
+
if (parsed === null) {
|
|
76
|
+
// Foi recebido um httpStatusCode 4xx ou 5xx, mas não é um AppResult.
|
|
77
|
+
// Então só adiciona o erro.
|
|
78
|
+
result.WithError(r.message);
|
|
79
|
+
} else {
|
|
80
|
+
// Foi recebido um httpStatusCode 4xx ou 5xx e é um AppResult.
|
|
81
|
+
// Então, adiciona os erros e o httpStatusCode.
|
|
82
|
+
result.WithErrors(parsed.errors);
|
|
83
|
+
result.httpStatusCode = parsed.httpStatusCode;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
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;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
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
|
+
}
|
|
203
|
+
|
|
204
|
+
const UseAxiosClient = (keycloak: Keycloak) =>
|
|
205
|
+
new AxiosClient(keycloak);
|
|
206
|
+
|
|
207
|
+
export { AxiosClient, UseAxiosClient };
|
package/src/index.ts
CHANGED
|
@@ -13,10 +13,9 @@ import { AppResult } from "./common/appResult";
|
|
|
13
13
|
|
|
14
14
|
import { Cor, Tamanho } from "./enum";
|
|
15
15
|
|
|
16
|
-
import { nextTick } from "vue";
|
|
17
|
-
|
|
18
16
|
import "./style/tema.scss";
|
|
19
17
|
import "./style/componentes.scss";
|
|
18
|
+
import { AxiosClient, UseAxiosClient } from "./axios/axiosClient";
|
|
20
19
|
|
|
21
20
|
export {
|
|
22
21
|
SfLayout,
|
|
@@ -25,33 +24,15 @@ export {
|
|
|
25
24
|
SfContent,
|
|
26
25
|
SfTabNavigation,
|
|
27
26
|
SfButton,
|
|
28
|
-
AppResult,
|
|
29
27
|
UseAuthService,
|
|
30
28
|
UseNavMenuService,
|
|
31
29
|
UseDialogService,
|
|
30
|
+
UseAxiosClient,
|
|
31
|
+
AppResult,
|
|
32
32
|
Cor,
|
|
33
33
|
Tamanho,
|
|
34
|
+
AxiosClient,
|
|
34
35
|
};
|
|
35
|
-
/**
|
|
36
|
-
* https://stackoverflow.com/questions/37112218/css3-100vh-not-constant-in-mobile-browser
|
|
37
|
-
*/
|
|
38
|
-
(() => {
|
|
39
|
-
function Ajusta() {
|
|
40
|
-
nextTick(() => {
|
|
41
|
-
setTimeout(() => {
|
|
42
|
-
const root = document.querySelector(":root");
|
|
43
|
-
root &&
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
root.style.setProperty(
|
|
46
|
-
"--window-height",
|
|
47
|
-
`${window.visualViewport?.height ?? window.innerHeight}px`
|
|
48
|
-
);
|
|
49
|
-
}, 1);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
Ajusta();
|
|
53
|
-
window.addEventListener("resize", Ajusta);
|
|
54
|
-
})();
|
|
55
36
|
|
|
56
37
|
/**
|
|
57
38
|
* Tema inicial
|
|
@@ -47,6 +47,12 @@ function AuthService(config: KeycloakConfig) {
|
|
|
47
47
|
) => {
|
|
48
48
|
const check = await CheckSilentLoginFile();
|
|
49
49
|
if (!check) return;
|
|
50
|
+
if (initOptions.onLoad === "login-required") {
|
|
51
|
+
document.documentElement.setAttribute(
|
|
52
|
+
"data-app-message",
|
|
53
|
+
"Autorizando..."
|
|
54
|
+
);
|
|
55
|
+
}
|
|
50
56
|
return keycloakInstance
|
|
51
57
|
.init(initOptions)
|
|
52
58
|
.then(async (authenticated) => {
|
|
@@ -54,10 +60,14 @@ function AuthService(config: KeycloakConfig) {
|
|
|
54
60
|
await FetchUserInfo();
|
|
55
61
|
}
|
|
56
62
|
if (callback && typeof callback === "function") callback();
|
|
63
|
+
document.documentElement.classList.add("app-mounted");
|
|
57
64
|
})
|
|
58
65
|
.catch((e: any) => {
|
|
59
66
|
console.error(e);
|
|
60
|
-
document.
|
|
67
|
+
document.documentElement.setAttribute(
|
|
68
|
+
"data-app-message",
|
|
69
|
+
"Não foi possível conectar no servidor de autenticação."
|
|
70
|
+
);
|
|
61
71
|
});
|
|
62
72
|
};
|
|
63
73
|
|
package/src/style/tema.scss
CHANGED
|
@@ -6,13 +6,15 @@
|
|
|
6
6
|
@import "./src/normalize";
|
|
7
7
|
|
|
8
8
|
:root {
|
|
9
|
-
--window-height:
|
|
9
|
+
--window-height: 100svh;
|
|
10
10
|
--font-family: "Roboto", "Helvetica", "Arial", sans-serif;
|
|
11
11
|
--font-size: 1rem;
|
|
12
12
|
--font-weight: 400;
|
|
13
13
|
--line-height: 1.15;
|
|
14
14
|
--header-height: 60px;
|
|
15
|
-
--main-height: calc(
|
|
15
|
+
--main-height: calc(
|
|
16
|
+
var(--window-height, 100vh) - var(--header-height)
|
|
17
|
+
);
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
html,
|