@zvonimirsun/iszy-nuxt-auth-layer 0.4.0 → 0.5.0
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.
|
@@ -144,14 +144,22 @@ const ssoProvider = {
|
|
|
144
144
|
|
|
145
145
|
const binding = ref(false)
|
|
146
146
|
let pollIndex: number | null = null
|
|
147
|
+
let bindingChannel: BroadcastChannel | null = null
|
|
148
|
+
let bindingPopup: Window | null = null
|
|
147
149
|
|
|
148
150
|
onBeforeUnmount(() => {
|
|
151
|
+
cleanupBinding()
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
function cleanupBinding() {
|
|
155
|
+
bindingChannel?.close()
|
|
156
|
+
bindingChannel = null
|
|
157
|
+
bindingPopup = null
|
|
149
158
|
if (pollIndex != null) {
|
|
150
159
|
clearInterval(pollIndex)
|
|
151
160
|
pollIndex = null
|
|
152
161
|
}
|
|
153
|
-
|
|
154
|
-
})
|
|
162
|
+
}
|
|
155
163
|
|
|
156
164
|
async function bind(type: LoginProviderType, title = '绑定第三方登录', width = 500, height = 600) {
|
|
157
165
|
if (isThirdPartyBound(type)) {
|
|
@@ -162,11 +170,18 @@ async function bind(type: LoginProviderType, title = '绑定第三方登录', wi
|
|
|
162
170
|
return
|
|
163
171
|
}
|
|
164
172
|
binding.value = true
|
|
165
|
-
|
|
173
|
+
cleanupBinding()
|
|
174
|
+
const channelName = `iszy-oauth-${crypto.randomUUID()}`
|
|
175
|
+
bindingChannel = new BroadcastChannel(channelName)
|
|
176
|
+
bindingChannel.onmessage = e => void bindCallback(e.data)
|
|
166
177
|
const top = (window.screen.height - height) / 2
|
|
167
178
|
const left = (window.screen.width - width) / 2
|
|
168
|
-
const
|
|
179
|
+
const url = new URL(`/api/oauth/${type}/bind`, window.location.origin)
|
|
180
|
+
url.searchParams.set('popupChannel', channelName)
|
|
181
|
+
const page = window.open(url.href, title, `popup,width=${width},height=${height},top=${top},left=${left}`)
|
|
182
|
+
bindingPopup = page
|
|
169
183
|
if (!page) {
|
|
184
|
+
cleanupBinding()
|
|
170
185
|
binding.value = false
|
|
171
186
|
toast.add({ title: '登录失败', description: '请允许浏览器弹出窗口!', color: 'error' })
|
|
172
187
|
return
|
|
@@ -174,38 +189,30 @@ async function bind(type: LoginProviderType, title = '绑定第三方登录', wi
|
|
|
174
189
|
pollIndex = window.setInterval(() => {
|
|
175
190
|
if (page.closed) {
|
|
176
191
|
binding.value = false
|
|
177
|
-
|
|
178
|
-
clearInterval(pollIndex)
|
|
179
|
-
pollIndex = null
|
|
180
|
-
}
|
|
192
|
+
cleanupBinding()
|
|
181
193
|
}
|
|
182
194
|
}, 500)
|
|
183
195
|
}
|
|
184
196
|
|
|
185
|
-
async function bindCallback(
|
|
186
|
-
success
|
|
187
|
-
message?: string
|
|
188
|
-
}>) {
|
|
189
|
-
const page = e.source as Window
|
|
190
|
-
if (e.origin !== window.location.origin) {
|
|
191
|
-
return
|
|
192
|
-
}
|
|
193
|
-
if (e.data.success == null) {
|
|
197
|
+
async function bindCallback(data: unknown) {
|
|
198
|
+
if (!data || typeof data !== 'object' || !('success' in data) || typeof data.success !== 'boolean') {
|
|
194
199
|
return
|
|
195
200
|
}
|
|
201
|
+
const page = bindingPopup
|
|
202
|
+
cleanupBinding()
|
|
196
203
|
binding.value = false
|
|
197
|
-
|
|
198
|
-
if (
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
204
|
+
page?.close()
|
|
205
|
+
if (data.success) {
|
|
206
|
+
try {
|
|
207
|
+
await userStore.pullProfile(true)
|
|
208
|
+
toast.add({ title: '绑定成功', color: 'success' })
|
|
209
|
+
}
|
|
210
|
+
catch (e) {
|
|
211
|
+
toast.add({ title: '刷新账号信息失败', description: getErrorMessage(e), color: 'error' })
|
|
212
|
+
}
|
|
206
213
|
}
|
|
207
214
|
else {
|
|
208
|
-
toast.add({ title: '绑定失败', description:
|
|
215
|
+
toast.add({ title: '绑定失败', description: 'message' in data && typeof data.message === 'string' ? data.message : '操作失败', color: 'error' })
|
|
209
216
|
}
|
|
210
217
|
}
|
|
211
218
|
|
package/app/pages/login.vue
CHANGED
|
@@ -29,6 +29,9 @@ const error = ref<string>()
|
|
|
29
29
|
const loading = ref(false)
|
|
30
30
|
const activeLoginTab = ref(isSsoMode.value ? 'sso' : 'local')
|
|
31
31
|
let pollIndex: number | null = null
|
|
32
|
+
let popupChannel: BroadcastChannel | null = null
|
|
33
|
+
let loginPopup: Window | null = null
|
|
34
|
+
let popupPending = false
|
|
32
35
|
|
|
33
36
|
const query = route.query
|
|
34
37
|
if (query) {
|
|
@@ -97,12 +100,19 @@ const stopAutoRedirect = watch(() => mounted.value && userStore.logged, (logged)
|
|
|
97
100
|
})
|
|
98
101
|
|
|
99
102
|
onBeforeUnmount(() => {
|
|
103
|
+
cleanupPopup()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
function cleanupPopup() {
|
|
107
|
+
popupPending = false
|
|
108
|
+
popupChannel?.close()
|
|
109
|
+
popupChannel = null
|
|
110
|
+
loginPopup = null
|
|
100
111
|
if (pollIndex != null) {
|
|
101
112
|
clearInterval(pollIndex)
|
|
102
113
|
pollIndex = null
|
|
103
114
|
}
|
|
104
|
-
|
|
105
|
-
})
|
|
115
|
+
}
|
|
106
116
|
|
|
107
117
|
async function login(payload: FormSubmitEvent<Schema>) {
|
|
108
118
|
if (loading.value) {
|
|
@@ -133,24 +143,29 @@ function thirdPartyLogin(url: string, title = '第三方登录', width = 500, he
|
|
|
133
143
|
return
|
|
134
144
|
}
|
|
135
145
|
stopAutoRedirect()
|
|
146
|
+
cleanupPopup()
|
|
136
147
|
loading.value = true
|
|
137
148
|
error.value = undefined
|
|
138
|
-
|
|
149
|
+
popupPending = true
|
|
150
|
+
const popupUrl = new URL(url, window.location.origin)
|
|
151
|
+
const channelName = `iszy-oauth-${crypto.randomUUID()}`
|
|
152
|
+
popupChannel = new BroadcastChannel(channelName)
|
|
153
|
+
popupChannel.onmessage = e => void finishThirdPartyLogin(e.data)
|
|
154
|
+
popupUrl.searchParams.set('popupChannel', channelName)
|
|
139
155
|
const top = (window.screen.height - height) / 2
|
|
140
156
|
const left = (window.screen.width - width) / 2
|
|
141
|
-
const page = window.open(
|
|
157
|
+
const page = window.open(popupUrl.href, title, `popup,width=${width},height=${height},top=${top},left=${left}`)
|
|
158
|
+
loginPopup = page
|
|
142
159
|
if (!page) {
|
|
160
|
+
cleanupPopup()
|
|
143
161
|
loading.value = false
|
|
144
162
|
toast.add({ title: '登录失败', description: '请允许浏览器弹出窗口!', color: 'error' })
|
|
145
163
|
return
|
|
146
164
|
}
|
|
147
165
|
pollIndex = window.setInterval(() => {
|
|
148
166
|
if (page.closed) {
|
|
167
|
+
cleanupPopup()
|
|
149
168
|
loading.value = false
|
|
150
|
-
if (pollIndex != null) {
|
|
151
|
-
clearInterval(pollIndex)
|
|
152
|
-
pollIndex = null
|
|
153
|
-
}
|
|
154
169
|
}
|
|
155
170
|
}, 500)
|
|
156
171
|
}
|
|
@@ -159,27 +174,28 @@ function ssoLogin() {
|
|
|
159
174
|
thirdPartyLogin('/api/oauth/sso', `${authFeatures.sso.title} 登录`, 560, 760)
|
|
160
175
|
}
|
|
161
176
|
|
|
162
|
-
async function
|
|
163
|
-
success
|
|
164
|
-
message?: string
|
|
165
|
-
}>) {
|
|
166
|
-
const page = e.source as Window
|
|
167
|
-
if (e.origin !== window.location.origin || e.data.success == null) {
|
|
177
|
+
async function finishThirdPartyLogin(data: unknown) {
|
|
178
|
+
if (!popupPending || !data || typeof data !== 'object' || !('success' in data) || typeof data.success !== 'boolean') {
|
|
168
179
|
return
|
|
169
180
|
}
|
|
181
|
+
const page = loginPopup
|
|
182
|
+
cleanupPopup()
|
|
170
183
|
loading.value = false
|
|
171
|
-
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
184
|
+
page?.close()
|
|
185
|
+
if (data.success) {
|
|
186
|
+
try {
|
|
187
|
+
await userStore.pullProfile(true)
|
|
188
|
+
if (!userStore.logged) {
|
|
189
|
+
throw new Error('登录状态未生效,请重试')
|
|
190
|
+
}
|
|
191
|
+
_afterLogin()
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
error.value = e instanceof Error ? e.message : '获取登录状态失败,请刷新后重试'
|
|
195
|
+
}
|
|
180
196
|
}
|
|
181
197
|
else {
|
|
182
|
-
error.value =
|
|
198
|
+
error.value = 'message' in data && typeof data.message === 'string' ? data.message : '登录失败'
|
|
183
199
|
}
|
|
184
200
|
}
|
|
185
201
|
|
|
@@ -16,6 +16,13 @@ definePageMeta({
|
|
|
16
16
|
const { title } = usePublicConfig()
|
|
17
17
|
const route = useRoute()
|
|
18
18
|
const requestFetch = useRequestFetch()
|
|
19
|
+
const popupChannelName = typeof route.query.popupChannel === 'string'
|
|
20
|
+
&& /^iszy-oauth-[\w-]{1,64}$/.test(route.query.popupChannel)
|
|
21
|
+
? route.query.popupChannel
|
|
22
|
+
: undefined
|
|
23
|
+
const popupChannel = import.meta.client && popupChannelName
|
|
24
|
+
? new BroadcastChannel(popupChannelName)
|
|
25
|
+
: undefined
|
|
19
26
|
const step = ref<'choice' | 'bind' | 'create'>('choice')
|
|
20
27
|
const loading = ref(false)
|
|
21
28
|
const submitError = ref<string>()
|
|
@@ -23,6 +30,8 @@ const completionExpired = ref(false)
|
|
|
23
30
|
const flowId = typeof route.query.flow === 'string' ? route.query.flow : ''
|
|
24
31
|
const completionQuery = `?flow=${encodeURIComponent(flowId)}`
|
|
25
32
|
|
|
33
|
+
onBeforeUnmount(() => popupChannel?.close())
|
|
34
|
+
|
|
26
35
|
useSeoMeta({
|
|
27
36
|
title: () => `完成 SSO 登录 - ${title}`,
|
|
28
37
|
robots: 'noindex,nofollow',
|
|
@@ -130,8 +139,7 @@ async function submitCompletion(url: string, body: SsoCompletionBindBody | SsoCo
|
|
|
130
139
|
}
|
|
131
140
|
|
|
132
141
|
async function finishLogin() {
|
|
133
|
-
if (
|
|
134
|
-
window.opener.postMessage({ success: true }, window.location.origin)
|
|
142
|
+
if (notifyLoginPopup({ success: true })) {
|
|
135
143
|
window.close()
|
|
136
144
|
return
|
|
137
145
|
}
|
|
@@ -140,14 +148,21 @@ async function finishLogin() {
|
|
|
140
148
|
|
|
141
149
|
async function returnToLogin() {
|
|
142
150
|
const message = terminalError.value || submitError.value || 'SSO 登录未完成'
|
|
143
|
-
if (
|
|
144
|
-
window.opener.postMessage({ success: false, message }, window.location.origin)
|
|
151
|
+
if (notifyLoginPopup({ success: false, message })) {
|
|
145
152
|
window.close()
|
|
146
153
|
return
|
|
147
154
|
}
|
|
148
155
|
await navigateTo('/login')
|
|
149
156
|
}
|
|
150
157
|
|
|
158
|
+
function notifyLoginPopup(payload: { success: boolean, message?: string }) {
|
|
159
|
+
if (!popupChannel) {
|
|
160
|
+
return false
|
|
161
|
+
}
|
|
162
|
+
popupChannel.postMessage(payload)
|
|
163
|
+
return true
|
|
164
|
+
}
|
|
165
|
+
|
|
151
166
|
function getErrorStatus(error: unknown) {
|
|
152
167
|
const normalized = error as {
|
|
153
168
|
status?: number
|
package/package.json
CHANGED
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
import type { PublicUser, ResultDto } from '@zvonimirsun/iszy-common'
|
|
2
2
|
import type { H3Event } from 'h3'
|
|
3
3
|
|
|
4
|
-
function
|
|
4
|
+
function readPopupChannel(value: unknown) {
|
|
5
|
+
return typeof value === 'string' && /^iszy-oauth-[\w-]{1,64}$/.test(value) ? value : undefined
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function escapeHtml(value: string) {
|
|
9
|
+
return value.replace(/[&<>"']/g, char => ({
|
|
10
|
+
'&': '&',
|
|
11
|
+
'<': '<',
|
|
12
|
+
'>': '>',
|
|
13
|
+
'"': '"',
|
|
14
|
+
'\'': ''',
|
|
15
|
+
})[char]!)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function renderCallbackPage(title: string, body: string, payload: { success: boolean, message?: string }, popupChannel?: string) {
|
|
5
19
|
return `
|
|
6
20
|
<head>
|
|
7
|
-
<title>${title}</title>
|
|
21
|
+
<title>${escapeHtml(title)}</title>
|
|
8
22
|
<meta charset="UTF-8">
|
|
9
23
|
</head>
|
|
10
24
|
<body>
|
|
11
|
-
${body}
|
|
25
|
+
${escapeHtml(body)}
|
|
12
26
|
<script>
|
|
13
|
-
|
|
14
|
-
|
|
27
|
+
const popupChannel = ${JSON.stringify(popupChannel || '')};
|
|
28
|
+
if (popupChannel) {
|
|
29
|
+
const channel = new BroadcastChannel(popupChannel);
|
|
30
|
+
channel.postMessage(${JSON.stringify(payload).replace(/</g, '\\u003c')});
|
|
31
|
+
channel.close();
|
|
32
|
+
window.close();
|
|
15
33
|
}
|
|
16
34
|
</script>
|
|
17
35
|
</body>
|
|
@@ -23,7 +41,8 @@ export async function redirectToOAuthProvider(event: H3Event, provider: string)
|
|
|
23
41
|
const url = getRequestURL(event)
|
|
24
42
|
const state = randomState()
|
|
25
43
|
|
|
26
|
-
|
|
44
|
+
const popupChannel = readPopupChannel(getQuery(event).popupChannel)
|
|
45
|
+
await setState(state, { popupChannel })
|
|
27
46
|
|
|
28
47
|
return sendRedirect(event, `${apiOrigin}/oauth/${provider}?state=${state}&client=${encodeURIComponent(url.origin)}`)
|
|
29
48
|
}
|
|
@@ -38,8 +57,10 @@ export async function redirectToOAuthProviderBind(event: H3Event, provider: stri
|
|
|
38
57
|
const code = res.data!
|
|
39
58
|
const state = randomState()
|
|
40
59
|
|
|
60
|
+
const popupChannel = readPopupChannel(getQuery(event).popupChannel)
|
|
41
61
|
await setState(state, {
|
|
42
62
|
isBind: true,
|
|
63
|
+
popupChannel,
|
|
43
64
|
})
|
|
44
65
|
|
|
45
66
|
return sendRedirect(event, `${apiOrigin}/oauth/${provider}/bind?state=${state}&client=${encodeURIComponent(url.origin)}&access_token=${code}`)
|
|
@@ -47,7 +68,6 @@ export async function redirectToOAuthProviderBind(event: H3Event, provider: stri
|
|
|
47
68
|
|
|
48
69
|
export async function handleOAuthProviderCallback(event: H3Event, provider: string) {
|
|
49
70
|
const { title } = usePublicConfig()
|
|
50
|
-
const url = getRequestURL(event)
|
|
51
71
|
const query = getQuery(event)
|
|
52
72
|
const state = query.state as string
|
|
53
73
|
|
|
@@ -71,21 +91,22 @@ export async function handleOAuthProviderCallback(event: H3Event, provider: stri
|
|
|
71
91
|
if (stateData.isBind) {
|
|
72
92
|
const error = query.error as string | undefined
|
|
73
93
|
if (error) {
|
|
74
|
-
return renderCallbackPage(`${title} - 绑定失败`,
|
|
94
|
+
return renderCallbackPage(`${title} - 绑定失败`, error, {
|
|
75
95
|
success: false,
|
|
76
96
|
message: error,
|
|
77
|
-
})
|
|
97
|
+
}, stateData.popupChannel)
|
|
78
98
|
}
|
|
79
99
|
|
|
80
|
-
return renderCallbackPage(`${title} - 绑定成功`,
|
|
100
|
+
return renderCallbackPage(`${title} - 绑定成功`, '验证成功', {
|
|
81
101
|
success: true,
|
|
82
|
-
})
|
|
102
|
+
}, stateData.popupChannel)
|
|
83
103
|
}
|
|
84
104
|
|
|
85
105
|
const pendingToken = typeof query.pending_token === 'string' ? query.pending_token : undefined
|
|
86
106
|
if (provider === 'sso' && pendingToken) {
|
|
87
107
|
const completionId = await createSsoCompletion(event, pendingToken)
|
|
88
|
-
|
|
108
|
+
const channelQuery = stateData.popupChannel ? `&popupChannel=${encodeURIComponent(stateData.popupChannel)}` : ''
|
|
109
|
+
return sendRedirect(event, `/sso/complete?flow=${completionId}${channelQuery}`)
|
|
89
110
|
}
|
|
90
111
|
|
|
91
112
|
const code = query.code as string
|
|
@@ -121,13 +142,13 @@ export async function handleOAuthProviderCallback(event: H3Event, provider: stri
|
|
|
121
142
|
}
|
|
122
143
|
|
|
123
144
|
if (errorMessage) {
|
|
124
|
-
return renderCallbackPage(`${title} - 登录失败`,
|
|
145
|
+
return renderCallbackPage(`${title} - 登录失败`, errorMessage, {
|
|
125
146
|
success: false,
|
|
126
147
|
message: errorMessage,
|
|
127
|
-
})
|
|
148
|
+
}, stateData.popupChannel)
|
|
128
149
|
}
|
|
129
150
|
|
|
130
|
-
return renderCallbackPage(`${title} - 登录成功`,
|
|
151
|
+
return renderCallbackPage(`${title} - 登录成功`, '登录成功', {
|
|
131
152
|
success: true,
|
|
132
|
-
})
|
|
153
|
+
}, stateData.popupChannel)
|
|
133
154
|
}
|