login-authorization-v2 2.0.0-beta.1 → 2.0.0-beta.11

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 (81) hide show
  1. package/README.md +45 -47
  2. package/dist/index.d.ts +14 -4
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.esm.js +413 -497
  5. package/dist/index.umd.js +432 -537
  6. package/dist/src/compatible.d.ts +1 -1
  7. package/dist/src/compatible.d.ts.map +1 -1
  8. package/dist/src/cookie.d.ts +2 -2
  9. package/dist/src/cookie.d.ts.map +1 -1
  10. package/dist/src/request.d.ts.map +1 -1
  11. package/package.json +29 -25
  12. package/babel.config.js +0 -6
  13. package/coverage/clover.xml +0 -140
  14. package/coverage/coverage-final.json +0 -8
  15. package/coverage/lcov-report/base.css +0 -224
  16. package/coverage/lcov-report/block-navigation.js +0 -87
  17. package/coverage/lcov-report/common-authorization-v2/index.html +0 -116
  18. package/coverage/lcov-report/common-authorization-v2/index.ts.html +0 -547
  19. package/coverage/lcov-report/common-authorization-v2/src/compatible.ts.html +0 -223
  20. package/coverage/lcov-report/common-authorization-v2/src/constance.ts.html +0 -106
  21. package/coverage/lcov-report/common-authorization-v2/src/cookie.ts.html +0 -346
  22. package/coverage/lcov-report/common-authorization-v2/src/dom.ts.html +0 -262
  23. package/coverage/lcov-report/common-authorization-v2/src/index.html +0 -206
  24. package/coverage/lcov-report/common-authorization-v2/src/request.ts.html +0 -361
  25. package/coverage/lcov-report/common-authorization-v2/src/shares.ts.html +0 -163
  26. package/coverage/lcov-report/common-authorization-v2/src/types.ts.html +0 -412
  27. package/coverage/lcov-report/common-authorization-v2/src/utils.ts.html +0 -145
  28. package/coverage/lcov-report/compatible.ts.html +0 -223
  29. package/coverage/lcov-report/constance.ts.html +0 -106
  30. package/coverage/lcov-report/cookie.ts.html +0 -310
  31. package/coverage/lcov-report/dom.ts.html +0 -262
  32. package/coverage/lcov-report/favicon.png +0 -0
  33. package/coverage/lcov-report/index.html +0 -206
  34. package/coverage/lcov-report/prettify.css +0 -1
  35. package/coverage/lcov-report/prettify.js +0 -2
  36. package/coverage/lcov-report/shares.ts.html +0 -163
  37. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  38. package/coverage/lcov-report/sorter.js +0 -210
  39. package/coverage/lcov-report/types.ts.html +0 -412
  40. package/coverage/lcov-report/utils.ts.html +0 -145
  41. package/coverage/lcov.info +0 -268
  42. package/dist/compatible.d.ts +0 -10
  43. package/dist/compatible.d.ts.map +0 -1
  44. package/dist/constance.d.ts +0 -6
  45. package/dist/constance.d.ts.map +0 -1
  46. package/dist/cookie.d.ts +0 -8
  47. package/dist/cookie.d.ts.map +0 -1
  48. package/dist/dom.d.ts +0 -2
  49. package/dist/dom.d.ts.map +0 -1
  50. package/dist/index.js.LICENSE.txt +0 -1
  51. package/dist/request.d.ts +0 -36
  52. package/dist/request.d.ts.map +0 -1
  53. package/dist/shares.d.ts +0 -10
  54. package/dist/shares.d.ts.map +0 -1
  55. package/dist/test/cookie.test.d.ts +0 -2
  56. package/dist/test/cookie.test.d.ts.map +0 -1
  57. package/dist/test/shares.test.d.ts +0 -2
  58. package/dist/test/shares.test.d.ts.map +0 -1
  59. package/dist/types.d.ts +0 -89
  60. package/dist/types.d.ts.map +0 -1
  61. package/dist/utils.d.ts +0 -2
  62. package/dist/utils.d.ts.map +0 -1
  63. package/index.html +0 -28
  64. package/index.ts +0 -154
  65. package/jest.config.js +0 -6
  66. package/src/compatible.ts +0 -46
  67. package/src/constance.ts +0 -7
  68. package/src/cookie.ts +0 -75
  69. package/src/dom.ts +0 -59
  70. package/src/request.ts +0 -99
  71. package/src/shares.ts +0 -26
  72. package/src/types.ts +0 -109
  73. package/src/utils.ts +0 -20
  74. package/tests/compatible.test.ts +0 -420
  75. package/tests/cookie.test.ts +0 -78
  76. package/tests/dom.test.ts +0 -95
  77. package/tests/index.test.ts +0 -126
  78. package/tests/shares.test.ts +0 -52
  79. package/tests/utils.test.ts +0 -79
  80. package/tsconfig.json +0 -49
  81. package/webpack.config.mjs +0 -47
package/src/compatible.ts DELETED
@@ -1,46 +0,0 @@
1
- /**
2
- * 这个文件是写一些兼容的方法,避免各个项目做出大量调整
3
- */
4
-
5
- import { getAccessToken, setAccessToken, setRefreshToken } from './cookie'
6
- import Cookie from 'js-cookie'
7
-
8
- export const setIdToken = setAccessToken
9
- export const getIdToken = getAccessToken
10
-
11
- export const setCookie = (
12
- name: string,
13
- value: string | undefined | null,
14
- domain: string,
15
- path: string = '/',
16
- time = 30 * 24 * 60 * 60 * 1000
17
- ) => {
18
- if (value === null || value === undefined) {
19
- return Cookie.remove(name)
20
- }
21
- Cookie.set(name, value, {
22
- domain,
23
- path,
24
- expires: time / (24 * 60 * 60 * 1000)
25
- })
26
- }
27
- export const clearLoginCookie = () => {
28
- setAccessToken(null)
29
- setRefreshToken(null)
30
- }
31
-
32
- export const getCookie = (c_name: string) => {
33
- if (!c_name) return null
34
- const reg = new RegExp('(?:^|; )' + encodeURIComponent(c_name) + '=([^;]*)')
35
- const result = reg.exec(document.cookie)
36
- return result ? decodeURIComponent(result[1] || '') : null
37
- }
38
-
39
-
40
- export const getUrlParam = (key: string, href?: string) => {
41
- const search = '?' + ((href || window.location.href).split('?')[1] || '')
42
-
43
- const searchParams = new URLSearchParams(search)
44
-
45
- return searchParams.get(key)
46
- }
package/src/constance.ts DELETED
@@ -1,7 +0,0 @@
1
- export const NO_ACCESS_TOKEN = 'Access token not found'
2
- export const INVALID_ACCESS_TOKEN = 'Invalid access token'
3
- export const INVALID_REFRESH_TOKEN = 'Invalid refresh token'
4
- export const NO_REFRESH_TOKEN = 'Refresh token not found'
5
- export const NO_MODULE_BASE_URL = 'Module base URL is required'
6
- export const INVALID_TENANT_ID = 'Invalid tenant ID'
7
- export const INVALID_BRAND = 'Invalid brand'
package/src/cookie.ts DELETED
@@ -1,75 +0,0 @@
1
- import Cookie from 'js-cookie'
2
- import type Cookies from 'js-cookie'
3
- import { INVALID_ACCESS_TOKEN, INVALID_REFRESH_TOKEN } from './constance'
4
-
5
- export type CookieKeys =
6
- 'access_token'
7
- | 'refresh_token'
8
- | 'idTokenFront'
9
- | 'idTokenBack'
10
- | 'refreshTokenFront'
11
- | 'refreshTokenBack'
12
- | 'currentSystemType'
13
-
14
- const makeSetCookieFn = (key: CookieKeys, options?: Cookies.CookieAttributes) => {
15
- const index = window.location.hostname.indexOf('.')
16
- const domain =
17
- index === -1
18
- ? window.location.hostname
19
- : window.location.hostname.slice(index)
20
-
21
- return (value: string | null) => {
22
- if (value === null) {
23
- return Cookie.remove(key, { domain, ...options } )
24
- }
25
- Cookie.set(key, value, {
26
- domain,
27
- expires: 30,
28
- ...options
29
- })
30
- }
31
- }
32
-
33
- const setIdTokenFront = makeSetCookieFn('idTokenFront')
34
- const setIdTokenBack = makeSetCookieFn('idTokenBack')
35
- const setRefreshTokenFront = makeSetCookieFn('refreshTokenFront')
36
- const setRefreshTokenBack = makeSetCookieFn('refreshTokenBack')
37
- export const setRefreshToken = (token: string | null) => {
38
- if (!token) {
39
- setIdTokenBack(null)
40
- setIdTokenFront(null)
41
- makeSetCookieFn('refresh_token')(null)
42
- return
43
- }
44
- const splits = token.split('.')
45
- if (splits.length < 5) throw new Error(INVALID_REFRESH_TOKEN)
46
- const refreshTokenFront = splits[0] + '.' + splits[1]
47
- const refreshTokenBack = splits[2] + '.' + splits[3] + '.' + splits[4]
48
-
49
- setRefreshTokenFront(refreshTokenFront)
50
- setRefreshTokenBack(refreshTokenBack)
51
- makeSetCookieFn('refresh_token')(token)
52
- }
53
- export const setAccessToken = (token: string | null) => {
54
- if (!token) {
55
- setIdTokenBack(null)
56
- setIdTokenFront(null)
57
- makeSetCookieFn('access_token')(null)
58
- return
59
- }
60
- const splits = token.split('.')
61
- if (splits.length < 3) throw new Error(INVALID_ACCESS_TOKEN)
62
- const idTokenFront = splits[0] + '.' + splits[1]
63
- const idTokenBack = splits[2] || ''
64
-
65
- setIdTokenFront(idTokenFront)
66
- setIdTokenBack(idTokenBack)
67
-
68
- makeSetCookieFn('access_token')(token)
69
- }
70
-
71
- export const getAccessToken = () => Cookie.get('access_token')
72
- export const getRefreshToken = () => Cookie.get('refresh_token')
73
-
74
- export const setSystemType = makeSetCookieFn('currentSystemType')
75
- export const getSystemType = () => Cookie.get('currentSystemType')
package/src/dom.ts DELETED
@@ -1,59 +0,0 @@
1
- import { isMobile } from './utils'
2
- import { loginPageUrl } from './shares'
3
-
4
- const createPcDialog = () => {
5
- return `<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;">
6
- <div id="href" style="color: rgb(32,33,36);"></div>
7
- <div style="margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;">You do not have access to the system.</div>
8
- <div style="display: flex;justify-content: flex-end;">
9
- <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>
10
- <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>
11
- </div>
12
- </div>`
13
- }
14
-
15
- const createMobileDialog = () => {
16
- return `<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;">
17
- <div id="href" style="color: rgb(32,33,36);"></div>
18
- <div style="margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;">You do not have access to the system.</div>
19
- <div style="display: flex;justify-content: flex-end;">
20
- <div style="display: flex;justify-content: flex-end;">
21
- <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>
22
- <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>
23
- </div>
24
- </div>
25
- </div>`
26
- }
27
-
28
- const onConfirmHandler = (evt: Event) => {
29
- closeDialog()
30
- window.location.href = loginPageUrl
31
- }
32
-
33
- const onCancelHandler = () => {
34
- closeDialog()
35
- window.location.href = 'about:blank'
36
- }
37
-
38
- export const closeDialog = () => {
39
- const container = document.getElementById('confirm-container')
40
-
41
- if (!container || !container.parentNode) return
42
- document.body.removeChild(container.parentNode)
43
- }
44
-
45
- export const openDialog = () => {
46
- const container = document.createElement('div')
47
-
48
- isMobile()
49
- ? container.innerHTML = createMobileDialog()
50
- : container.innerHTML = createPcDialog()
51
-
52
- const confirmButton = container.querySelector('#confirm')
53
- const cancelButton = container.querySelector('#cancel')
54
-
55
- confirmButton?.addEventListener('click', onConfirmHandler)
56
- cancelButton?.addEventListener('click', onCancelHandler)
57
-
58
- document.body.appendChild(container)
59
- }
package/src/request.ts DELETED
@@ -1,99 +0,0 @@
1
- import axios from 'axios'
2
- import { tenantId, brand, moduleBaseUrl } from './shares'
3
- import type { ModuleName, UserInfo } from './types'
4
- import { getAccessToken } from './cookie'
5
- import type { AxiosInstance, AxiosPromise } from 'axios'
6
- import { getUserInfo } from './utils'
7
-
8
- let instance!: AxiosInstance
9
-
10
- /**
11
- * 没找到更好的方式,只能这样做了
12
- */
13
- export const initInstance = async () => {
14
- let userInfo: UserInfo | null = null
15
- try {
16
- userInfo = await getUserInfo()
17
- } catch (error) {
18
- console.warn('initInstance getUserInfo error:', error)
19
- }
20
- instance = axios.create({
21
- baseURL: moduleBaseUrl,
22
- timeout: 1.5e4,
23
- headers: {
24
- 'X-tenant-id': userInfo ? userInfo['custom:tenant_id'].toString() : tenantId.toString(),
25
- 'X-brand': brand.toString(),
26
- 'Authorization': `Bearer ${getAccessToken()}`,
27
- }
28
- })
29
-
30
- instance.interceptors.request.use(
31
- (config) => {
32
- return config
33
- },
34
- (error) => {
35
- return Promise.reject(error)
36
- }
37
- )
38
-
39
- instance.interceptors.response.use(
40
- (response) => {
41
- if (response.data.code !== 200) {
42
- return Promise.reject(response.data.msg)
43
- }
44
- return response
45
- },
46
- (error) => {
47
- return Promise.reject(error)
48
- }
49
- )
50
- }
51
-
52
- type RefreshTokenResponse = {
53
- code: string
54
- msg: string
55
- content: {
56
- accessToken: string
57
- expiresIn: number
58
- idToken: string
59
- newDeviceMetadata: null
60
- refreshToken: string
61
- tokenType: 'Bearer'
62
- }
63
- }
64
- export const fetchRefreshTokenHTTP = (refreshToken: string) => {
65
- return instance({
66
- method: 'post',
67
- url: '/user-profile/refresh-token/refresh',
68
- data: {
69
- refreshToken
70
- }
71
- }) as AxiosPromise<RefreshTokenResponse>
72
- }
73
-
74
- export const fetchLogoutHTTP = () => {
75
- return instance({
76
- method: 'get',
77
- url: '/user-profile/logout'
78
- })
79
- }
80
-
81
- type Content = {
82
- groupName: ModuleName
83
- icon: string
84
- id: number
85
- label: string
86
- staffEndpoint: boolean
87
- url: string
88
- }
89
- type ServerListResponse = {
90
- content: Array<Content>
91
- code: number
92
- totalElements: number
93
- }
94
- export const fetchServerListHTTP = () => {
95
- return instance({
96
- method: 'get',
97
- url: '/session/current/servers'
98
- }) as AxiosPromise<ServerListResponse>
99
- }
package/src/shares.ts DELETED
@@ -1,26 +0,0 @@
1
- import { Tenant, Brand } from './types'
2
- import { INVALID_BRAND, INVALID_TENANT_ID } from './constance'
3
-
4
- export let tenantId!: Tenant
5
- export let brand!: Brand
6
- export let moduleBaseUrl: string = ''
7
- export let loginPageUrl: string = ''
8
-
9
- export const setTenantId = (id: Tenant) => {
10
- if (!Object.values(Tenant).includes(id)) {
11
- throw new Error(INVALID_TENANT_ID)
12
- }
13
- tenantId = id
14
- }
15
- export const setBrand = (id: Brand) => {
16
- if (!Object.values(Brand).includes(id)) {
17
- throw new Error(INVALID_BRAND)
18
- }
19
- brand = id
20
- }
21
- export const setModuleBaseUrl = (url: string) => {
22
- moduleBaseUrl = url
23
- }
24
- export const setLoginPageUrl = (url: string) => {
25
- loginPageUrl = url
26
- }
package/src/types.ts DELETED
@@ -1,109 +0,0 @@
1
- const modules = [
2
- {
3
- href: '',
4
- name: 'commonLogin'
5
- },
6
- {
7
- href: '',
8
- name: 'App'
9
- },
10
- {
11
- href: '',
12
- name: 'Portal'
13
- },
14
- {
15
- href: '',
16
- name: 'AgentPortal'
17
- },
18
- {
19
- href: '',
20
- name: 'AdminPortal'
21
- },
22
- {
23
- href: '',
24
- name: 'CRM'
25
- },
26
- {
27
- href: '',
28
- name: 'Finance'
29
- },
30
- {
31
- href: '',
32
- name: 'FundingAdmin'
33
- },
34
- {
35
- href: '',
36
- name: 'MessageTemplate'
37
- },
38
- {
39
- href: '',
40
- name: 'Translate'
41
- },
42
- {
43
- href: '',
44
- name: 'TradeConfig'
45
- },
46
- {
47
- href: '',
48
- name: 'Ticket'
49
- },
50
- {
51
- href: '',
52
- name: 'IBPortalAdmin'
53
- },
54
- {
55
- href: '',
56
- name: 'OPPortal'
57
- },
58
- {
59
- href: '',
60
- name: 'ClientInfoHub'
61
- },
62
- {
63
- href: '',
64
- name: 'Promotion'
65
- }
66
- ] as const
67
-
68
- export type ModuleName = typeof modules[number]['name']
69
-
70
- export type UserInfo = {
71
- 'aud': string
72
- 'aud_time': number
73
- 'cognito:groups': ModuleName[]
74
- 'cognito:username': string
75
- 'custom:encrypt_email_var': string
76
- 'custom:tenant_id': Tenant
77
- 'custom:user_id': string
78
- 'email': string
79
- 'email_verified': boolean
80
- 'exp': number
81
- 'event_id': string
82
- 'iat': number
83
- 'iss': string
84
- 'jti': string
85
- 'origin_jti': string
86
- 'preferred_username': string
87
- 'sub': string
88
- 'token_use': string
89
- }
90
-
91
- export enum Brand {
92
- ZERO = 1,
93
- HEDGEHOOD = 2,
94
- NISE = 3
95
- }
96
-
97
- export enum Tenant {
98
- ZERO_INT = 1,
99
- HEDGEHOOD = 2,
100
- ZERO_NZ = 3,
101
- ZERO_LA = 4,
102
- ZERO_BR = 4,
103
- NISE_EU = 5
104
- }
105
-
106
- export enum SystemType {
107
- Staff = 'staff',
108
- Client = 'client'
109
- }
package/src/utils.ts DELETED
@@ -1,20 +0,0 @@
1
- import type { UserInfo } from './types'
2
- import { getAccessToken } from './cookie'
3
- import { NO_ACCESS_TOKEN } from './constance'
4
- import { Base64 } from 'js-base64'
5
-
6
- export const isMobile = ():boolean => {
7
- const ua = navigator.userAgent;
8
- return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua);
9
- }
10
-
11
- export const getUserInfo = (): Promise<UserInfo> => {
12
- return new Promise((resolve, reject) => {
13
- const token = getAccessToken()
14
- if (!token) return reject(new Error(NO_ACCESS_TOKEN))
15
- const splits = token.split('.')
16
- if (splits.length < 3) return reject(new Error(NO_ACCESS_TOKEN))
17
-
18
- resolve(JSON.parse(Base64.decode(splits[1] as string)) as UserInfo)
19
- })
20
- }