feat(auth-ui): add loopback MCP web login authorization callback (/mcp-auth)
This commit is contained in:
parent
ae6a4f6aac
commit
7af960eff6
@ -15,8 +15,8 @@ export default {
|
||||
smsLoginClient: 'app:app', // 验证码登录的 client 信息
|
||||
socialLoginClient: 'social:social', // 社交登录的 client 信息
|
||||
websocket: false, // 是否开启websocket
|
||||
whiteList: ['/login', '/404', '/401', '/lock'], // 配置无权限可以访问的页面
|
||||
whiteTagList: ['/login', '/404', '/401', '/lock'], // 配置不添加tags页面 ('/advanced-router/mutative-detail/*'——*为通配符)
|
||||
whiteList: ['/login', '/mcp-auth', '/404', '/401', '/lock'], // 配置无权限可以访问的页面
|
||||
whiteTagList: ['/login', '/mcp-auth', '/404', '/401', '/lock'], // 配置不添加tags页面 ('/advanced-router/mutative-detail/*'——*为通配符)
|
||||
fistPage: {
|
||||
label: i18n.t('wel.homePage'),
|
||||
value: '/wel/index',
|
||||
|
||||
352
src/page/login/mcpAuth.vue
Normal file
352
src/page/login/mcpAuth.vue
Normal file
@ -0,0 +1,352 @@
|
||||
<template>
|
||||
<div class="mcp-auth-container">
|
||||
<div class="mcp-auth-card">
|
||||
<div class="mcp-auth-header">
|
||||
<h2>MES 本机 MCP 登录授权</h2>
|
||||
<span
|
||||
v-if="profile"
|
||||
class="mcp-env-badge"
|
||||
:class="isProdProfile ? 'is-prod' : 'is-dev'"
|
||||
>
|
||||
{{ profile }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<el-alert
|
||||
v-if="errorMessage"
|
||||
:title="errorMessage"
|
||||
type="error"
|
||||
:closable="false"
|
||||
show-icon
|
||||
class="mcp-alert"
|
||||
/>
|
||||
|
||||
<div
|
||||
v-else-if="loading"
|
||||
class="mcp-loading"
|
||||
>
|
||||
<i class="el-icon-loading" />
|
||||
<span>正在核验当前 MES 登录态...</span>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-else
|
||||
class="mcp-auth-body"
|
||||
>
|
||||
<el-alert
|
||||
v-if="isProdProfile"
|
||||
title="当前为生产环境授权,请确认您正在操作本人电脑上的可信 AI 客户端。"
|
||||
type="warning"
|
||||
:closable="false"
|
||||
show-icon
|
||||
class="mcp-alert"
|
||||
/>
|
||||
|
||||
<div class="mcp-info-list">
|
||||
<div class="mcp-info-item">
|
||||
<span class="mcp-info-label">当前账号</span>
|
||||
<span class="mcp-info-value">{{ displayUser }}</span>
|
||||
</div>
|
||||
<div class="mcp-info-item">
|
||||
<span class="mcp-info-label">所属租户</span>
|
||||
<span class="mcp-info-value">{{ tenantId || '*' }}</span>
|
||||
</div>
|
||||
<div class="mcp-info-item">
|
||||
<span class="mcp-info-label">回调地址</span>
|
||||
<span class="mcp-info-value mcp-mono">{{ callbackUrl }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="mcp-tip">
|
||||
授权仅将当前登录令牌回传至您本机 <code>127.0.0.1</code> 回环端口,不会发送给任何外部服务器。
|
||||
</p>
|
||||
|
||||
<div class="mcp-actions">
|
||||
<el-button
|
||||
type="primary"
|
||||
:loading="submitting"
|
||||
@click="handleAuthorize"
|
||||
>
|
||||
确认授权给本机 MCP
|
||||
</el-button>
|
||||
<el-button
|
||||
:disabled="submitting"
|
||||
@click="handleSwitchAccount"
|
||||
>
|
||||
切换账号重新登录
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { mapGetters } from 'vuex'
|
||||
import { getStore } from '@/util/store'
|
||||
|
||||
const PENDING_KEY = 'nalinv_mcp_auth_pending'
|
||||
const LOOPBACK_CALLBACK_REGEX = /^http:\/\/(127\.0\.0\.1|localhost):\d{1,5}\/callback$/
|
||||
|
||||
export default {
|
||||
name: 'McpAuth',
|
||||
data() {
|
||||
return {
|
||||
loading: true,
|
||||
submitting: false,
|
||||
errorMessage: '',
|
||||
callbackUrl: '',
|
||||
stateToken: '',
|
||||
profile: '',
|
||||
tenantId: ''
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
...mapGetters(['access_token', 'refresh_token', 'userInfo']),
|
||||
isProdProfile() {
|
||||
return /prd|prod/i.test(this.profile || '')
|
||||
},
|
||||
displayUser() {
|
||||
const info = this.userInfo || {}
|
||||
const username = info.username || ''
|
||||
const realName = info.name || info.nickName || ''
|
||||
if (realName && username && realName !== username) {
|
||||
return `${realName} (${username})`
|
||||
}
|
||||
return username || realName || '当前已登录用户'
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.initAuthFlow()
|
||||
},
|
||||
methods: {
|
||||
readParams() {
|
||||
const q = this.$route.query || {}
|
||||
if (q.callback && q.state) {
|
||||
const payload = {
|
||||
callback: String(q.callback),
|
||||
state: String(q.state),
|
||||
profile: q.profile ? String(q.profile) : ''
|
||||
}
|
||||
sessionStorage.setItem(PENDING_KEY, JSON.stringify(payload))
|
||||
return payload
|
||||
}
|
||||
try {
|
||||
const saved = sessionStorage.getItem(PENDING_KEY)
|
||||
return saved ? JSON.parse(saved) : null
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
},
|
||||
isValidLoopbackCallback(url) {
|
||||
return LOOPBACK_CALLBACK_REGEX.test(url || '')
|
||||
},
|
||||
async initAuthFlow() {
|
||||
const params = this.readParams()
|
||||
if (!params || !params.callback || !params.state) {
|
||||
this.loading = false
|
||||
this.errorMessage = '缺少 MCP 授权回调参数(callback / state),请回到 AI 工具重新发起登录。'
|
||||
return
|
||||
}
|
||||
if (!this.isValidLoopbackCallback(params.callback)) {
|
||||
sessionStorage.removeItem(PENDING_KEY)
|
||||
this.loading = false
|
||||
this.errorMessage = '出于安全限制,仅允许回调至本机 http://127.0.0.1:<端口>/callback 地址。'
|
||||
return
|
||||
}
|
||||
|
||||
this.callbackUrl = params.callback
|
||||
this.stateToken = params.state
|
||||
this.profile = params.profile || ''
|
||||
|
||||
if (!this.access_token) {
|
||||
this.$router.replace({ path: '/login', query: { from: 'mcp-auth' } })
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await this.$store.dispatch('GetUserInfo')
|
||||
const storedTenant =
|
||||
getStore({ name: 'tenantId', type: 'session' }) ||
|
||||
getStore({ name: 'tenantId' }) ||
|
||||
(this.userInfo && this.userInfo.tenantId)
|
||||
this.tenantId = storedTenant ? String(storedTenant) : '*'
|
||||
this.loading = false
|
||||
} catch (e) {
|
||||
await this.$store.dispatch('FedLogOut')
|
||||
this.$router.replace({ path: '/login', query: { from: 'mcp-auth' } })
|
||||
}
|
||||
},
|
||||
handleAuthorize() {
|
||||
if (!this.isValidLoopbackCallback(this.callbackUrl)) {
|
||||
this.errorMessage = '非法的本机回调地址,已中止授权。'
|
||||
return
|
||||
}
|
||||
if (!this.access_token) {
|
||||
this.$router.replace({ path: '/login', query: { from: 'mcp-auth' } })
|
||||
return
|
||||
}
|
||||
|
||||
this.submitting = true
|
||||
sessionStorage.removeItem(PENDING_KEY)
|
||||
|
||||
// Use hidden form POST to loopback callback: works cleanly from HTTPS public domain without CORS/Mixed Content
|
||||
const form = document.createElement('form')
|
||||
form.method = 'POST'
|
||||
form.action = this.callbackUrl
|
||||
form.style.display = 'none'
|
||||
|
||||
const fields = {
|
||||
state: this.stateToken,
|
||||
profile: this.profile,
|
||||
access_token: this.access_token,
|
||||
refresh_token: this.refresh_token || '',
|
||||
tenant_id: this.tenantId || '*',
|
||||
username: (this.userInfo && this.userInfo.username) || ''
|
||||
}
|
||||
|
||||
Object.keys(fields).forEach((key) => {
|
||||
const input = document.createElement('input')
|
||||
input.type = 'hidden'
|
||||
input.name = key
|
||||
input.value = fields[key]
|
||||
form.appendChild(input)
|
||||
})
|
||||
|
||||
document.body.appendChild(form)
|
||||
form.submit()
|
||||
},
|
||||
async handleSwitchAccount() {
|
||||
sessionStorage.setItem(
|
||||
PENDING_KEY,
|
||||
JSON.stringify({
|
||||
callback: this.callbackUrl,
|
||||
state: this.stateToken,
|
||||
profile: this.profile
|
||||
})
|
||||
)
|
||||
try {
|
||||
await this.$store.dispatch('LogOut')
|
||||
} catch (e) {
|
||||
await this.$store.dispatch('FedLogOut')
|
||||
}
|
||||
this.$router.replace({ path: '/login', query: { from: 'mcp-auth' } })
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.mcp-auth-container {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
background: #f0f2f5;
|
||||
}
|
||||
|
||||
.mcp-auth-card {
|
||||
width: 100%;
|
||||
max-width: 460px;
|
||||
background: #ffffff;
|
||||
border-radius: 10px;
|
||||
padding: 28px;
|
||||
box-shadow: 0 8px 28px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.mcp-auth-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 18px;
|
||||
|
||||
h2 {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
color: #1f2937;
|
||||
}
|
||||
}
|
||||
|
||||
.mcp-env-badge {
|
||||
padding: 3px 10px;
|
||||
border-radius: 999px;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
|
||||
&.is-prod {
|
||||
background: #fef2f2;
|
||||
color: #dc2626;
|
||||
border: 1px solid #fecaca;
|
||||
}
|
||||
|
||||
&.is-dev {
|
||||
background: #ecfdf5;
|
||||
color: #059669;
|
||||
border: 1px solid #a7f3d0;
|
||||
}
|
||||
}
|
||||
|
||||
.mcp-alert {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.mcp-loading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 24px 0;
|
||||
color: #6b7280;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.mcp-info-list {
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 8px;
|
||||
padding: 12px 14px;
|
||||
margin-bottom: 14px;
|
||||
background: #f9fafb;
|
||||
}
|
||||
|
||||
.mcp-info-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 6px 0;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.mcp-info-label {
|
||||
color: #6b7280;
|
||||
flex-shrink: 0;
|
||||
margin-right: 12px;
|
||||
}
|
||||
|
||||
.mcp-info-value {
|
||||
color: #111827;
|
||||
font-weight: 500;
|
||||
text-align: right;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.mcp-mono {
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.mcp-tip {
|
||||
font-size: 12px;
|
||||
color: #6b7280;
|
||||
line-height: 1.6;
|
||||
margin: 0 0 18px;
|
||||
}
|
||||
|
||||
.mcp-actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
|
||||
.el-button {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@ -165,7 +165,11 @@ export default {
|
||||
} else {
|
||||
me.$router.push({ path: me.tagWel.value });
|
||||
}*/
|
||||
me.$router.push({ path: me.tagWel.value });
|
||||
if (sessionStorage.getItem('nalinv_mcp_auth_pending')) {
|
||||
me.$router.push({ path: '/mcp-auth' })
|
||||
} else {
|
||||
me.$router.push({ path: me.tagWel.value })
|
||||
}
|
||||
},
|
||||
})
|
||||
}).catch((res) => {
|
||||
|
||||
@ -18,6 +18,8 @@ router.beforeEach((to, from, next) => {
|
||||
next({ path: '/lock' })
|
||||
} else if (to.path === '/login') {
|
||||
next({ path: '/' })
|
||||
} else if (to.path === '/mcp-auth') {
|
||||
next()
|
||||
} else {
|
||||
const value = to.query.src || to.fullPath
|
||||
const label = to.query.name || i18n.t(to.name)
|
||||
|
||||
@ -22,6 +22,16 @@ export default [
|
||||
isAuth: false
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/mcp-auth',
|
||||
name: 'mcp-auth',
|
||||
component: () => import(/* webpackChunkName: "page" */ '@/page/login/mcpAuth'),
|
||||
meta: {
|
||||
keepAlive: false,
|
||||
isTab: false,
|
||||
isAuth: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/wel',
|
||||
component: Layout,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user