feat(32.2-02): admin consent card at /connect posts the decision and leaves for redirect_uri

- /connect route without shell and without public, so login round-trips back
- ConnectView ready state: client mark, fact box, catalog-order scope picker
  with the token-dialog * rule, Stay connected, Deny and Allow
- Allow/Deny POST /mcp/consent/{request}; the browser leaves through
  window.location.assign after refusing javascript:, data: and vbscript:
- MCPConsent, MCPConsentDecision and AccountItem type aliases
- backend::lang.mcp_connect copy in en and pl; connect page title
- smoke test: ticks, POST body, assign called, router never used
This commit is contained in:
Jakub Zych
2026-11-21 00:42:01 +01:00
parent 1d3e6df6ec
commit f46c90f7fb
7 changed files with 476 additions and 0 deletions

View File

@@ -0,0 +1,330 @@
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { Bot, Info, LoaderCircle, Sun } from '@lucide/vue'
import { api } from '../api/client'
import type { MCPConsent } from '../api/types'
import { t } from '../app/i18n'
import { currentUser } from '../state/useAuth'
// The MCP consent card (32.2 UI-SPEC C1). A standalone screen in the login
// flow: the same navy page and white card as LoginView, with light literal
// colours inside the card so it looks the same in light and dark mode. The
// decision is posted with fetch and the browser then leaves for the
// server-issued redirect_uri with window.location.assign: never the router,
// never safeRedirect, never a native form POST (CSP form-action 'self').
type State = 'loading' | 'ready'
const route = useRoute()
const state = ref<State>('loading')
const consent = ref<MCPConsent | null>(null)
const checked = ref<Set<string>>(new Set())
const offline = ref(true)
const busy = ref<'allow' | 'deny' | null>(null)
let loadSeq = 0
const requestId = computed(() => {
const value = route.query.request
return typeof value === 'string' ? value.trim() : ''
})
const scopes = computed(() => consent.value?.scopes ?? [])
const starChecked = computed(() => checked.value.has('*'))
const selected = computed(() => {
if (starChecked.value) {
return ['*']
}
return scopes.value.map((scope) => scope.name).filter((name) => checked.value.has(name))
})
const canAllow = computed(() => busy.value === null && selected.value.length > 0)
const clientName = computed(() => consent.value?.client.name || consent.value?.client.id || '')
const redirectHost = computed(() => consent.value?.client.redirect_host ?? '')
/** A logo only from a same-origin path or an https: URL (T-32.2-10). */
const logoUrl = computed(() => {
const raw = consent.value?.client.logo_url?.trim() ?? ''
if (raw === '') {
return ''
}
if (raw.startsWith('/') && !raw.startsWith('//') && !raw.startsWith('/\\')) {
return raw
}
try {
return new URL(raw).protocol === 'https:' ? raw : ''
} catch {
return ''
}
})
/** The signed-in administrator: the API's view, else the session profile. */
const adminValue = computed(() => {
const admin = consent.value?.admin
let name = admin?.name?.trim() ?? ''
let email = admin?.email?.trim() ?? ''
let login = admin?.login?.trim() ?? ''
if (!admin || (name === '' && email === '' && login === '')) {
const user = currentUser.value
name = user ? `${user.first_name} ${user.last_name}`.trim() : ''
email = user?.email?.trim() ?? ''
login = user?.login ?? ''
}
if (name !== '' && email !== '') {
return t('backend::lang.mcp_connect.admin_with_email', { name, email })
}
return name !== '' ? name : login
})
/** Every shown scope starts ticked except *, unless * is the only one. */
function initialTicks(view: MCPConsent): Set<string> {
const names = view.scopes.map((scope) => scope.name)
if (names.length === 1 && names[0] === '*') {
return new Set(names)
}
return new Set(names.filter((name) => name !== '*'))
}
async function load(): Promise<void> {
const seq = ++loadSeq
state.value = 'loading'
consent.value = null
const id = requestId.value
if (id === '') {
return
}
const result = await api.GET('/mcp/consent/{request}', { params: { path: { request: id } } })
if (seq !== loadSeq || !result.data) {
return
}
const view = result.data.data
consent.value = view
checked.value = initialTicks(view)
offline.value = true
state.value = 'ready'
}
function toggleScope(name: string, on: boolean): void {
const next = new Set(checked.value)
if (name === '*') {
if (on) {
next.clear()
next.add('*')
} else {
next.delete('*')
}
} else if (!starChecked.value) {
if (on) {
next.add(name)
} else {
next.delete(name)
}
}
checked.value = next
}
function scopeDisabled(name: string): boolean {
return starChecked.value && name !== '*'
}
/** The server-issued redirect, refused when its scheme could run script. */
function safeDestination(uri: string): string | null {
let url: URL
try {
url = new URL(uri)
} catch {
return null
}
if (url.protocol === 'javascript:' || url.protocol === 'data:' || url.protocol === 'vbscript:') {
return null
}
return uri
}
async function decide(decision: 'allow' | 'deny'): Promise<void> {
if (busy.value !== null || consent.value === null) {
return
}
if (decision === 'allow' && !canAllow.value) {
return
}
busy.value = decision
try {
const result = await api.POST('/mcp/consent/{request}', {
params: { path: { request: consent.value.request_id || requestId.value } },
body:
decision === 'allow'
? {
decision,
scopes: selected.value,
offline_access: consent.value.offline_access ? offline.value : false,
}
: { decision, scopes: [], offline_access: false },
})
const destination = result.data ? safeDestination(result.data.data.redirect_uri) : null
if (destination !== null) {
window.location.assign(destination)
}
} finally {
busy.value = null
}
}
onMounted(() => {
void load()
})
watch(requestId, () => {
void load()
})
</script>
<template>
<main
class="flex min-h-screen items-center justify-center px-4"
style="background: radial-gradient(ellipse at 50% 35%, #2b3a5c 0%, #1d2740 60%)"
>
<div class="flex w-full max-w-[480px] flex-col gap-6">
<div class="flex items-center justify-center gap-3">
<span class="flex size-[34px] items-center justify-center rounded-full bg-accent-soft">
<Sun :size="20" class="text-accent" aria-hidden="true" />
</span>
<span class="text-[18px] font-bold text-white">Summer<span class="text-accent">CMS</span></span>
</div>
<form
v-if="state === 'ready' && consent"
data-connect-state="ready"
class="flex flex-col gap-[18px] rounded-card bg-white p-8 text-[#141b2d] shadow-login"
novalidate
@submit.prevent="decide('allow')"
>
<img v-if="logoUrl" :src="logoUrl" alt="" class="size-12 rounded-inner object-contain" />
<span v-else class="flex size-12 items-center justify-center rounded-full bg-[#f3f5f8]">
<Bot :size="24" class="text-[#566175]" aria-hidden="true" />
</span>
<div class="flex flex-col gap-2">
<h1 tabindex="-1" class="text-[22px] font-bold tracking-[-0.02em] [overflow-wrap:anywhere]">
{{ t('backend::lang.mcp_connect.heading', { client: clientName }) }}
</h1>
<p class="text-[#566175]">{{ t('backend::lang.mcp_connect.subline') }}</p>
<p
v-if="consent.consent_required === false"
data-previously-allowed
class="flex items-center gap-2 text-[13px] text-[#566175] [overflow-wrap:anywhere]"
>
<Info :size="14" class="shrink-0" aria-hidden="true" />
<span>{{ t('backend::lang.mcp_connect.previously_allowed', { client: clientName }) }}</span>
</p>
</div>
<dl class="flex flex-col gap-3 rounded-inner bg-[#f3f5f8] px-4 py-3">
<div class="flex flex-col gap-1">
<dt class="font-semibold">{{ t('backend::lang.mcp_connect.signed_in_as') }}</dt>
<dd class="flex flex-col items-start gap-1">
<span data-connect-admin class="[overflow-wrap:anywhere]">{{ adminValue }}</span>
</dd>
</div>
<div class="flex flex-col gap-1">
<dt class="font-semibold">{{ t('backend::lang.mcp_connect.returns_to') }}</dt>
<dd data-connect-host class="font-mono text-[13px] [overflow-wrap:anywhere]">{{ redirectHost }}</dd>
</div>
</dl>
<fieldset :disabled="busy !== null" class="flex min-w-0 flex-col gap-[18px]">
<div class="flex min-w-0 flex-col gap-1.5">
<span id="connect-scopes-label" class="font-semibold">{{ t('backend::lang.api_tokens.scopes') }}</span>
<div
role="group"
aria-labelledby="connect-scopes-label"
data-scope-picker
class="overflow-hidden rounded-inner border border-[#e6e9ef]"
>
<label
v-for="scope in scopes"
:key="scope.name"
:data-scope="scope.name"
:class="scopeDisabled(scope.name) ? 'cursor-not-allowed opacity-60' : 'cursor-pointer'"
class="flex min-h-[56px] items-start gap-3 border-t border-[#e6e9ef] px-4 py-3 first:border-t-0"
>
<input
type="checkbox"
class="mt-0.5 size-5 shrink-0 rounded-[6px] accent-[#22304d]"
:checked="checked.has(scope.name)"
:disabled="scopeDisabled(scope.name)"
@change="toggleScope(scope.name, ($event.target as HTMLInputElement).checked)"
/>
<span class="flex min-w-0 flex-col">
<span class="flex flex-wrap items-center gap-1">
<span class="font-semibold [overflow-wrap:anywhere]">{{ scope.label }}</span>
<span class="font-mono text-[13px] text-[#566175] [overflow-wrap:anywhere]">{{ scope.name }}</span>
<span
v-if="scope.read_only"
class="inline-flex h-6 items-center rounded-pill bg-[#f3f5f8] px-2.5 text-[13px] font-semibold whitespace-nowrap text-[#566175]"
>{{ t('backend::lang.api_tokens.read_only') }}</span
>
</span>
<span class="text-[13px] text-[#566175] [overflow-wrap:anywhere]">{{ scope.description }}</span>
</span>
</label>
</div>
<p v-if="starChecked" data-scope-all-note class="flex items-center gap-2 text-[13px] text-[#566175]">
<Info :size="14" class="shrink-0" aria-hidden="true" />
<span>{{ t('backend::lang.api_tokens.scope_all_includes') }}</span>
</p>
</div>
<div v-if="consent.offline_access" class="overflow-hidden rounded-inner border border-[#e6e9ef]">
<label data-connect-offline class="flex min-h-[56px] cursor-pointer items-start gap-3 px-4 py-3">
<input
v-model="offline"
type="checkbox"
class="mt-0.5 size-5 shrink-0 rounded-[6px] accent-[#22304d]"
/>
<span class="flex min-w-0 flex-col">
<span class="font-semibold">{{ t('backend::lang.mcp_connect.offline') }}</span>
<span class="text-[13px] text-[#566175] [overflow-wrap:anywhere]">{{
t('backend::lang.mcp_connect.offline_description')
}}</span>
</span>
</label>
</div>
</fieldset>
<div class="flex gap-2.5">
<button
type="button"
data-action="deny"
:disabled="busy !== null"
class="inline-flex h-input flex-1 items-center justify-center gap-2 rounded-control border border-[#d2d8e2] bg-white font-semibold text-[#141b2d] hover:bg-[#f1f3f7] disabled:opacity-60"
@click="decide('deny')"
>
<LoaderCircle v-if="busy === 'deny'" :size="16" class="animate-spin" aria-hidden="true" />
{{ t('backend::lang.mcp_connect.deny') }}
</button>
<button
type="submit"
data-action="allow"
:disabled="!canAllow"
class="inline-flex h-input flex-1 items-center justify-center gap-2 rounded-control bg-[#22304d] font-semibold text-white hover:opacity-90 disabled:opacity-60"
>
<LoaderCircle v-if="busy === 'allow'" :size="16" class="animate-spin" aria-hidden="true" />
{{ busy === 'allow' ? t('backend::lang.mcp_connect.allowing') : t('backend::lang.mcp_connect.allow') }}
</button>
</div>
</form>
<div
v-else
data-connect-state="loading"
aria-busy="true"
class="flex flex-col gap-[18px] rounded-card bg-white p-8 text-[#141b2d] shadow-login"
>
<h1 class="sr-only">{{ t('backend::lang.mcp_connect.page_title') }}</h1>
<span class="size-12 rounded-full bg-[#eceff4]" aria-hidden="true" />
</div>
</div>
</main>
</template>