login-authorization-v2 1.1.5 → 2.0.0-beta.2

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/index.js DELETED
@@ -1,304 +0,0 @@
1
- import axios from 'axios';
2
- import { Base64 } from 'js-base64';
3
- let tokenTimer = null;
4
- let idTokenFront = null;
5
- let idTokenBack = null;
6
- let refreshTokenFront = null;
7
- let refreshTokenBack = null;
8
- let userRequestUrl = null;
9
- let userLogoutUrl = null;
10
- let tenantId = '';
11
- let currentSystemType = '';
12
- let menuLists = [];
13
- let currentSystemName = '';
14
- // 获取、设置cookie
15
- function getCookie (c_name) {
16
- if (document.cookie.length > 0) {
17
- let c_start = document.cookie.indexOf(c_name + "=")
18
- if (c_start != -1) {
19
- c_start = c_start + c_name.length + 1
20
- let c_end = document.cookie.indexOf(";", c_start)
21
- if (c_end == -1) c_end = document.cookie.length
22
- return unescape(document.cookie.substring(c_start, c_end))
23
- }
24
- }
25
- return null
26
- }
27
- function setCookie (name, value, domain, path = '/', time = 30 * 24 * 60 * 60 * 1000) {
28
- if (value === undefined) return;
29
- let exp = new Date();
30
- exp.setTime(exp.getTime() + time);
31
- document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";domain=" + domain + ";path=" + path;
32
- }
33
- // 获取url参数中的某个值
34
- function getUrlParam (urlStr) {
35
- var query = window.location.href.split('?')[1];
36
- var vars = query ? query.split("&") : [];
37
- for (var i = 0; i < vars.length; i++) {
38
- var pair = vars[i].split("=");
39
- if (pair[0] == urlStr) { return pair[1]; }
40
- }
41
- return null;
42
- }
43
-
44
- if (getCookie('idTokenFront')) {
45
- idTokenFront = getCookie('idTokenFront');
46
- }
47
- if (getCookie('idTokenBack')) {
48
- idTokenBack = getCookie('idTokenBack');
49
- }
50
- if (getCookie('refreshTokenFront')) {
51
- refreshTokenFront = getCookie('refreshTokenFront');
52
- }
53
- if (getCookie('refreshTokenBack')) {
54
- refreshTokenBack = getCookie('refreshTokenBack');
55
- }
56
- // 获取用户信息
57
- function getUserInfo () {
58
- if (idTokenBack) {
59
- return JSON.parse(atob(idTokenBack.split('.')[0]))
60
- } else {
61
- return null
62
- }
63
- }
64
- // 获取idToken
65
- function getIdToken () {
66
- if (idTokenFront && idTokenBack) {
67
- idTokenFront = getCookie('idTokenFront');
68
- idTokenBack = getCookie('idTokenBack');
69
- }
70
- return (idTokenFront && idTokenBack) ? (idTokenFront + '.' + idTokenBack) : null
71
- }
72
- // 设置idToken
73
- function setIdToken (value) {
74
- idTokenFront = value.split('.')[0] + '.' + value.split('.')[1];
75
- idTokenBack = value.split('.')[2];
76
- setCookie('idTokenFront', idTokenFront, window.location.hostname.substring(window.location.hostname.indexOf('.')));
77
- setCookie('idTokenBack', idTokenBack, window.location.hostname.substring(window.location.hostname.indexOf('.')));
78
- }
79
- // 获取refreshToken
80
- function getRefreshToken () {
81
- return (refreshTokenFront && refreshTokenBack) ? (refreshTokenFront + '.' + refreshTokenBack) : null
82
- }
83
- // 设置refreshToken
84
- function setRefreshToken (value) {
85
- refreshTokenFront = value.split('.')[0] + '.' + value.split('.')[1];
86
- refreshTokenBack = value.split('.')[2] + '.' + value.split('.')[3] + '.' + value.split('.')[4];
87
- setCookie('refreshTokenFront', refreshTokenFront, window.location.hostname.substring(window.location.hostname.indexOf('.')));
88
- setCookie('refreshTokenBack', refreshTokenBack, window.location.hostname.substring(window.location.hostname.indexOf('.')));
89
- }
90
- // 清空登录相关cookie
91
- function clearLoginCookie () {
92
- if (currentSystemType === getCookie('currentSystemType')) {
93
- setCookie('idTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
94
- setCookie('idTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
95
- setCookie('refreshTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
96
- setCookie('refreshTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
97
- } else if (!getCookie('currentSystemType') || currentSystemName === 'commonLogin' || currentSystemName === 'App') {
98
- setCookie('idTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
99
- setCookie('idTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
100
- setCookie('refreshTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
101
- setCookie('refreshTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);
102
- }
103
- }
104
- // 定时刷新IdToken
105
- function refreshIdTokenTimer (time) {
106
- if (!getIdToken() || !getRefreshToken()) {
107
- return;
108
- }
109
- tokenTimer = setInterval(() => {
110
- axios({
111
- url: userRequestUrl + '/user-profile/refresh-token/refresh',
112
- method: 'post',
113
- data: {
114
- refreshToken: getRefreshToken()
115
- }
116
- }).then(res => {
117
- if (res.data.code === 200) {
118
- try {
119
- const userInfo = JSON.parse(Base64.decode(res.data.content.idToken.split('.')[1]))
120
- const groups = userInfo['cognito:groups'] || []
121
- if (!groups.includes(currentSystemName) && currentSystemName !== 'App' && currentSystemName !== 'commonLogin') {
122
- clearInterval(tokenTimer)
123
- clearLoginCookie();
124
- IsPC() ? createDoalog() : createDoalogMobile()
125
- return
126
- }
127
- } catch (e) {
128
- console.warn(e)
129
- }
130
- setIdToken(res.data.content.idToken);
131
- } else if (res.data.code === 500) {
132
- if (userLogoutUrl !== '' || userLogoutUrl !== undefined) {
133
- clearLoginCookie();
134
- window.location.href = userLogoutUrl;
135
- }
136
- }
137
- })
138
- }, time)
139
- }
140
- // 登出
141
- function logout () {
142
- return new Promise((resolve, reject) => {
143
- axios({
144
- url: userRequestUrl + '/user-profile/logout',
145
- method: 'post',
146
- data: {
147
- refreshToken: getRefreshToken()
148
- }
149
- }).then(res => {
150
- clearInterval(tokenTimer);
151
- clearLoginCookie();
152
- if (userLogoutUrl !== '' || userLogoutUrl !== undefined) {
153
- window.location.href = userLogoutUrl;
154
- }
155
- resolve(true)
156
- })
157
- })
158
- }
159
- // 无权限访问时弹窗显示
160
- function createDoalog(){
161
- let html = `<div id="confirm-container" style="box-sizing:border-box;position:fixed;width:615px;height:145px;box-shadow: 0 0 2px 2px #eeeeee;border-radius: 5px;z-index: 10000000;display: block;left: 35%;padding:20px 20px;top:10px;font-size: 16px;">
162
- <div id="href" style="color: rgb(32,33,36);"></div>
163
- <div style="margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;">You do not have access to the system.</div>
164
- <div style="display: flex;justify-content: flex-end;">
165
- <button id="cancel" style="padding: 8px 15px;background: #ffffff;color:rgb(26,115,232);border:1px solid #ccc;border-radius:5px;cursor: pointer;font-size:14px;">Cancel</button>
166
- <button id="confirm" style="padding: 8px 15px;background: rgb(26,115,232);color:#ffffff;border:none;border-radius:5px;margin-left: 10px;cursor: pointer;font-size:14px;">Sign in to another ZERO account</button>
167
- </div>
168
- </div>`
169
- let confirmDialog=document.getElementsByTagName('body')[0];
170
- confirmDialog.innerHTML = '';
171
- confirmDialog.insertAdjacentHTML("beforeend", html);
172
- document.getElementById('href').innerHTML = window.location.hostname;
173
- document.getElementById('confirm').onclick = confirms;
174
- document.getElementById('cancel').onclick = cancels;
175
- }
176
- function createDoalogMobile(){
177
- let html = `<div id="confirm-container" style="box-sizing:border-box;position:fixed;width:97.6vw;height:145px;box-shadow: 0 0 2px 2px #eeeeee;border-radius: 5px;z-index: 10000000;display: block;left:0px;padding:20px 20px;top:4px;margin:0 4px;font-size: 16px;">
178
- <div id="href" style="color: rgb(32,33,36);"></div>
179
- <div style="margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;">You do not have access to the system.</div>
180
- <div style="display: flex;justify-content: flex-end;">
181
- <div style="display: flex;justify-content: flex-end;">
182
- <button id="cancel" style="padding: 8px 15px;background: #ffffff;color:rgb(26,115,232);border:1px solid #ccc;border-radius:5px;cursor: pointer;font-size:14px;">Cancel</button>
183
- <button id="confirm" style="padding: 8px 15px;background: rgb(26,115,232);color:#ffffff;border:none;border-radius:5px;margin-left: 10px;cursor: pointer;font-size:14px;">Sign in to another ZERO account</button>
184
- </div>
185
- </div>
186
- </div>`
187
- let confirmDialog=document.getElementsByTagName('body')[0];
188
- confirmDialog.innerHTML = '';
189
- confirmDialog.insertAdjacentHTML("beforeend", html);
190
- document.getElementById('href').innerHTML = window.location.hostname;
191
- document.getElementById('confirm').onclick = confirms;
192
- document.getElementById('cancel').onclick = cancels;
193
- }
194
- function confirms() {
195
- clearLoginCookie();
196
- document.getElementById('confirm-container').style.display = 'none';
197
- window.location.href = userLogoutUrl;
198
- }
199
- function cancels() {
200
- document.getElementById('confirm-container').style.display = 'none';
201
- window.location.href = 'about:blank';
202
- }
203
- function IsPC () {
204
- var userAgentInfo = navigator.userAgent;
205
- var Agents = [
206
- 'Android',
207
- 'iPhone',
208
- 'SymbianOS',
209
- 'Windows Phone',
210
- 'iPad',
211
- 'iPod',
212
- ];
213
- var flag = true;
214
- for (var v = 0; v < Agents.length; v++) {
215
- if (userAgentInfo.indexOf(Agents[v]) > 0) {
216
- flag = false;
217
- break;
218
- }
219
- }
220
- return flag;
221
- }
222
- // 获取可访问系统列表
223
- function systemLists (systemName, idToken) {
224
- return new Promise((resolve, reject) => {
225
- axios({
226
- url: userRequestUrl + '/session/current/servers',
227
- method: 'get',
228
- headers: {
229
- authorization: 'Bearer ' + idToken,
230
- 'X-tenant-id': tenantId ? tenantId : ''
231
- }
232
- }).then(res => {
233
- if (res.data.code === 200) {
234
- menuLists = res.data.content;
235
- if (!systemName) {
236
- IsPC() ? createDoalog() : createDoalogMobile()
237
- } else if (systemName === 'commonLogin') {
238
- resolve(true);
239
- } else if (menuLists.length === 0) {
240
- IsPC() ? createDoalog() : createDoalogMobile()
241
- } else {
242
- let hasAccess = false;
243
- menuLists.forEach(item => {
244
- if (systemName.toLowerCase() === item.groupName.toLowerCase()) {
245
- resolve(true);
246
- hasAccess = true;
247
- currentSystemType = item.staffEndpoint ? 'staff' : 'client';
248
- }
249
- })
250
- if (!hasAccess) {
251
- IsPC() ? createDoalog() : createDoalogMobile()
252
- }
253
- }
254
- } else {
255
- resolve(false);
256
- }
257
- }).catch(() => {
258
- resolve(true);
259
- })
260
- })
261
- }
262
- // init初始化
263
- async function init (requestUrl, loginUrl, systemName, tenant, time = 1000 * 60 * 3) {
264
- tenantId = tenant ? tenant : '';
265
- userLogoutUrl = loginUrl;
266
- currentSystemName = systemName;
267
- return new Promise(async (resolve, reject) => {
268
- if (!getIdToken() || !getRefreshToken()) {
269
- resolve(false);
270
- }
271
- if (requestUrl) {
272
- userRequestUrl = requestUrl;
273
- refreshIdTokenTimer(time);
274
- if (systemName === 'App') {
275
- resolve(true);
276
- } else {
277
- let hasAuth = await systemLists(systemName, getIdToken());
278
- if (hasAuth) {
279
- resolve(true);
280
- } else {
281
- await logout();
282
- resolve(false);
283
- }
284
- }
285
- } else {
286
- alert('Not request url');
287
- reject();
288
- }
289
- })
290
- }
291
- export {
292
- init,
293
- getUserInfo,
294
- getIdToken,
295
- setIdToken,
296
- getRefreshToken,
297
- setRefreshToken,
298
- clearLoginCookie,
299
- refreshIdTokenTimer,
300
- logout,
301
- setCookie,
302
- getCookie,
303
- getUrlParam,
304
- }
package/webpack.config.js DELETED
@@ -1,9 +0,0 @@
1
- const path = require("path"); //Node.js内置模块
2
- module.exports = {
3
- mode: "development",
4
- entry: './index.js', //配置入口文件
5
- output: {
6
- path: path.resolve(__dirname, './dist'),
7
- filename: 'index.js',
8
- }
9
- }