@zvonimirsun/iszy-nuxt-auth-layer 0.2.0 → 0.2.1
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/app/pages/login.vue
CHANGED
|
@@ -4,7 +4,7 @@ import type { LocationQuery } from 'vue-router'
|
|
|
4
4
|
import * as z from 'zod'
|
|
5
5
|
import { useUserStore } from '##stores/user'
|
|
6
6
|
|
|
7
|
-
const { title } = usePublicConfig()
|
|
7
|
+
const { title, apiOrigin } = usePublicConfig()
|
|
8
8
|
const router = useRouter()
|
|
9
9
|
const route = useRoute()
|
|
10
10
|
const toast = useToast()
|
|
@@ -156,9 +156,18 @@ function ssoLogin() {
|
|
|
156
156
|
async function thirdPartyLoginCallback(e: MessageEvent<{
|
|
157
157
|
success: boolean
|
|
158
158
|
message?: string
|
|
159
|
+
type?: string
|
|
160
|
+
data?: {
|
|
161
|
+
pending_token?: string
|
|
162
|
+
pendingToken?: string
|
|
163
|
+
}
|
|
159
164
|
}>) {
|
|
160
165
|
const page = e.source as Window
|
|
161
|
-
if (e.origin
|
|
166
|
+
if (!isAllowedOAuthMessageOrigin(e.origin)) {
|
|
167
|
+
return
|
|
168
|
+
}
|
|
169
|
+
if (e.data.type === 'sso_completion_required') {
|
|
170
|
+
await startSsoCompletion(page, e.data.data?.pending_token || e.data.data?.pendingToken)
|
|
162
171
|
return
|
|
163
172
|
}
|
|
164
173
|
if (e.data.success == null) {
|
|
@@ -180,6 +189,56 @@ async function thirdPartyLoginCallback(e: MessageEvent<{
|
|
|
180
189
|
}
|
|
181
190
|
}
|
|
182
191
|
|
|
192
|
+
async function startSsoCompletion(page: Window, pendingToken?: string) {
|
|
193
|
+
if (!pendingToken) {
|
|
194
|
+
loading.value = false
|
|
195
|
+
error.value = 'SSO 登录缺少待绑定凭证'
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
const res = await $fetch<{
|
|
201
|
+
success: boolean
|
|
202
|
+
message: string
|
|
203
|
+
data?: {
|
|
204
|
+
flow: string
|
|
205
|
+
}
|
|
206
|
+
}>('/api/oauth/sso/completion/start', {
|
|
207
|
+
method: 'POST',
|
|
208
|
+
body: {
|
|
209
|
+
pendingToken,
|
|
210
|
+
},
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
if (!res.success || !res.data?.flow) {
|
|
214
|
+
throw new Error(res.message || 'SSO 登录流程创建失败')
|
|
215
|
+
}
|
|
216
|
+
page.location.href = `/sso/complete?flow=${encodeURIComponent(res.data.flow)}`
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
loading.value = false
|
|
220
|
+
window.removeEventListener('message', thirdPartyLoginCallback)
|
|
221
|
+
if (pollIndex != null) {
|
|
222
|
+
clearInterval(pollIndex)
|
|
223
|
+
pollIndex = null
|
|
224
|
+
}
|
|
225
|
+
error.value = err instanceof Error ? err.message : 'SSO 登录流程创建失败'
|
|
226
|
+
page.close()
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function isAllowedOAuthMessageOrigin(origin: string) {
|
|
231
|
+
if (origin === window.location.origin) {
|
|
232
|
+
return true
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
return origin === new URL(apiOrigin).origin
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
return false
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
183
242
|
function _afterLogin() {
|
|
184
243
|
toast.add({ title: '登录成功', description: '欢迎回来!', color: 'success' })
|
|
185
244
|
router.push({ path: redirect.value || '/', query: otherQuery.value })
|
package/package.json
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ResultDto } from '@zvonimirsun/iszy-common'
|
|
2
|
+
|
|
3
|
+
interface SsoCompletionStartBody {
|
|
4
|
+
pendingToken?: string
|
|
5
|
+
pending_token?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface SsoCompletionStartResult {
|
|
9
|
+
flow: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default defineEventHandler(async (event): Promise<ResultDto<SsoCompletionStartResult>> => {
|
|
13
|
+
const body = await readBody<SsoCompletionStartBody>(event)
|
|
14
|
+
const pendingToken = body?.pendingToken || body?.pending_token
|
|
15
|
+
|
|
16
|
+
if (!pendingToken) {
|
|
17
|
+
throw createError({
|
|
18
|
+
statusCode: 400,
|
|
19
|
+
message: '缺少 SSO 待绑定凭证',
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const flow = await createSsoCompletion(event, pendingToken)
|
|
24
|
+
return {
|
|
25
|
+
success: true,
|
|
26
|
+
message: 'SSO 登录流程已创建',
|
|
27
|
+
data: {
|
|
28
|
+
flow,
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
})
|