mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 08:03:46 +00:00
fix: refine sign-in page behavior
This commit is contained in:
4 files changed
+94
-88
No files matched your search
@@ -119,7 +119,6 @@ func GetStatus(c *gin.Context) {
|
||||
"passkey_display_name": passkeySetting.RPDisplayName,
|
||||
"passkey_rp_id": passkeySetting.EffectiveRPID(),
|
||||
"passkey_rp_ids": passkeySetting.RelyingPartyIDs(),
|
||||
"passkey_origins": passkeySetting.Origins,
|
||||
"passkey_allow_insecure": passkeySetting.AllowInsecureOrigin,
|
||||
"passkey_user_verification": passkeySetting.UserVerification,
|
||||
"passkey_attachment": passkeySetting.AttachmentPreference,
|
||||
|
||||
@@ -48,6 +48,44 @@ type passkeyDomainBegin struct {
|
||||
} `json:"options"`
|
||||
}
|
||||
|
||||
func TestGetStatusDoesNotExposePasskeyOrigins(t *testing.T) {
|
||||
settings := system_setting.GetPasskeySettings()
|
||||
originalSettings := *settings
|
||||
originalOptionMap := common.OptionMap
|
||||
originalServerAddress := system_setting.ServerAddress
|
||||
t.Cleanup(func() {
|
||||
*settings = originalSettings
|
||||
common.OptionMap = originalOptionMap
|
||||
system_setting.ServerAddress = originalServerAddress
|
||||
})
|
||||
common.OptionMap = map[string]string{}
|
||||
system_setting.ServerAddress = "https://www.example.com"
|
||||
*settings = system_setting.PasskeySettings{Enabled: true, RPID: "example.com"}
|
||||
|
||||
for _, origins := range []string{"https://www.example.com,https://private.example.com", "", "[]"} {
|
||||
t.Run("origins="+origins, func(t *testing.T) {
|
||||
settings.Origins = origins
|
||||
response := httptest.NewRecorder()
|
||||
context, _ := gin.CreateTestContext(response)
|
||||
context.Request = httptest.NewRequest(http.MethodGet, "/api/status", nil)
|
||||
|
||||
GetStatus(context)
|
||||
|
||||
require.Equal(t, http.StatusOK, response.Code)
|
||||
var payload struct {
|
||||
Success bool `json:"success"`
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
require.NoError(t, common.Unmarshal(response.Body.Bytes(), &payload))
|
||||
require.True(t, payload.Success)
|
||||
assert.NotContains(t, payload.Data, "passkey_origins")
|
||||
assert.NotContains(t, response.Body.String(), "private.example.com")
|
||||
assert.Equal(t, true, payload.Data["passkey_login"])
|
||||
assert.Equal(t, origins, settings.Origins)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func passkeyDomainRequest(t *testing.T, path string, payload any, identity service.AuthIdentity, origin string, handler gin.HandlerFunc) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
body, err := common.Marshal(payload)
|
||||
|
||||
@@ -23,6 +23,7 @@ import userEvent from '@testing-library/user-event'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { api } from '@/lib/api'
|
||||
import { STATUS_QUERY_KEY } from '@/lib/status-query'
|
||||
import type { AuthBundle } from '@/stores/auth-store'
|
||||
|
||||
import { verify, verifyLogin } from '../../secure-verification/api'
|
||||
@@ -66,7 +67,6 @@ function setupPasskeyNetwork() {
|
||||
url === '/api/status'
|
||||
? {
|
||||
passkey_rp_ids: ['example.com', 'www.example.com'],
|
||||
passkey_origins: 'https://example.com,https://www.example.com',
|
||||
}
|
||||
: {
|
||||
scope: operation.scope,
|
||||
@@ -386,7 +386,6 @@ it('keeps case-sensitive historical domains selectable and labels the distinctio
|
||||
success: true,
|
||||
data: {
|
||||
passkey_rp_ids: ['www.example.com', 'WWW.example.com'],
|
||||
passkey_origins: 'https://www.example.com',
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -417,3 +416,32 @@ it('keeps case-sensitive historical domains selectable and labels the distinctio
|
||||
)
|
||||
expect(changed).toHaveBeenCalledExactlyOnceWith('WWW.example.com')
|
||||
})
|
||||
|
||||
it.each([
|
||||
{ rpIDs: ['api.example.com'], canSwitch: false },
|
||||
{ rpIDs: ['example.com'], canSwitch: false },
|
||||
{ rpIDs: ['example.com', 'www.example.com'], canSwitch: true },
|
||||
])('does not display origins from cached status with $rpIDs', async (test) => {
|
||||
const client = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
})
|
||||
client.setQueryData(STATUS_QUERY_KEY, {
|
||||
passkey_rp_ids: test.rpIDs,
|
||||
passkey_origins: 'https://www.example.com,https://api.example.com',
|
||||
})
|
||||
const user = userEvent.setup()
|
||||
render(
|
||||
<QueryClientProvider client={client}>
|
||||
<PasskeyDomainSelector onChange={vi.fn()} />
|
||||
</QueryClientProvider>
|
||||
)
|
||||
|
||||
if (test.canSwitch) {
|
||||
await user.click(screen.getByRole('button'))
|
||||
expect(screen.getByRole('combobox')).toBeVisible()
|
||||
} else {
|
||||
expect(screen.queryByRole('button')).not.toBeInTheDocument()
|
||||
}
|
||||
expect(screen.queryAllByRole('link')).toHaveLength(0)
|
||||
expect(screen.queryByText('https://api.example.com')).not.toBeInTheDocument()
|
||||
})
|
||||
@@ -58,40 +58,7 @@ export function PasskeyDomainSelector(props: PasskeyDomainSelectorProps) {
|
||||
window.location.hostname.endsWith(`.${domain}`)
|
||||
)
|
||||
})
|
||||
const otherWebsites: string[] = []
|
||||
if (typeof status?.passkey_origins === 'string') {
|
||||
for (const origin of status.passkey_origins.split(',')) {
|
||||
try {
|
||||
const url = new URL(origin.trim())
|
||||
if (
|
||||
url.protocol !== 'https:' &&
|
||||
!(url.protocol === 'http:' && url.hostname === 'localhost')
|
||||
) {
|
||||
continue
|
||||
}
|
||||
if (
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.origin === window.location.origin
|
||||
) {
|
||||
continue
|
||||
}
|
||||
if (
|
||||
!configured.some(
|
||||
(rpID) =>
|
||||
url.hostname === rpID.toLowerCase() ||
|
||||
url.hostname.endsWith(`.${rpID.toLowerCase()}`)
|
||||
)
|
||||
) {
|
||||
continue
|
||||
}
|
||||
if (!otherWebsites.includes(url.origin)) otherWebsites.push(url.origin)
|
||||
} catch {
|
||||
/* Ignore malformed status entries. */
|
||||
}
|
||||
}
|
||||
}
|
||||
if (available.length < 2 && otherWebsites.length === 0) return null
|
||||
if (available.length < 2) return null
|
||||
const value = props.value ?? props.domains?.rpID ?? available[0]
|
||||
return (
|
||||
<div className='space-y-2 text-sm'>
|
||||
@@ -109,62 +76,36 @@ export function PasskeyDomainSelector(props: PasskeyDomainSelectorProps) {
|
||||
</Button>
|
||||
{expanded && (
|
||||
<div id={id} className='space-y-2'>
|
||||
{available.length > 1 && (
|
||||
<>
|
||||
<Label htmlFor={`${id}-domain`}>
|
||||
{t('Passkey website domain')}
|
||||
</Label>
|
||||
<Select
|
||||
value={available.includes(value ?? '') ? value : available[0]}
|
||||
onValueChange={(value) => {
|
||||
if (value) props.onChange(value)
|
||||
}}
|
||||
disabled={props.disabled}
|
||||
>
|
||||
<SelectTrigger id={`${id}-domain`}>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{available.map((domain) => (
|
||||
<SelectItem key={domain} value={domain}>
|
||||
{domain}
|
||||
{domain !== domain.toLowerCase() &&
|
||||
available.some(
|
||||
(other) =>
|
||||
other !== domain &&
|
||||
other.toLowerCase() === domain.toLowerCase()
|
||||
) && <> · {t('Historical capitalization')}</>}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</>
|
||||
)}
|
||||
<Label htmlFor={`${id}-domain`}>{t('Passkey website domain')}</Label>
|
||||
<Select
|
||||
value={available.includes(value ?? '') ? value : available[0]}
|
||||
onValueChange={(value) => {
|
||||
if (value) props.onChange(value)
|
||||
}}
|
||||
disabled={props.disabled}
|
||||
>
|
||||
<SelectTrigger id={`${id}-domain`}>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{available.map((domain) => (
|
||||
<SelectItem key={domain} value={domain}>
|
||||
{domain}
|
||||
{domain !== domain.toLowerCase() &&
|
||||
available.some(
|
||||
(other) =>
|
||||
other !== domain &&
|
||||
other.toLowerCase() === domain.toLowerCase()
|
||||
) && <> · {t('Historical capitalization')}</>}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<p className='text-muted-foreground'>
|
||||
{t(
|
||||
'Choose the domain used when this Passkey was created, then try again.'
|
||||
)}
|
||||
</p>
|
||||
{otherWebsites.length > 0 && (
|
||||
<div className='space-y-1'>
|
||||
<p>
|
||||
{t(
|
||||
'If your Passkey belongs to another website, start sign-in there:'
|
||||
)}
|
||||
</p>
|
||||
{otherWebsites.map((origin) => (
|
||||
<Button
|
||||
key={origin}
|
||||
variant='link'
|
||||
size='sm'
|
||||
className='h-auto p-0 break-all'
|
||||
render={<a href={`${origin}/sign-in`} />}
|
||||
>
|
||||
{origin}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user