107 lines
6.4 KiB
JavaScript
107 lines
6.4 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { webcrypto } from 'node:crypto'
|
|
import { beginGroupLogin, consumeGroupCallback, exchangeGroupCode, fixedOrigin, groupLoginDestination, GROUP_TRANSACTION_KEY, pkceChallenge } from '../src/utils/groupSso.mjs'
|
|
|
|
const identityOrigin = 'http://127.0.0.1:5173'
|
|
const hospitalOrigin = 'http://127.0.0.1:8090'
|
|
const verifier = 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'
|
|
function store() {
|
|
const values = new Map()
|
|
return { getItem: (key) => values.get(key) ?? null, setItem: (key, value) => values.set(key, value), removeItem: (key) => values.delete(key) }
|
|
}
|
|
async function transaction() {
|
|
const storage = store()
|
|
const target = new URL(await beginGroupLogin({ identityOrigin, hospitalOrigin, storage, cryptoApi: webcrypto, now: 1000 }))
|
|
const history = { state: null, calls: [], replaceState(...args) { this.calls.push(args) } }
|
|
const url = new URL('/admin/group-login', hospitalOrigin)
|
|
url.search = new URLSearchParams({ code: 'c'.repeat(64), state: target.searchParams.get('state') }).toString()
|
|
return { storage, target, history, url }
|
|
}
|
|
function consume(fixture, overrides = {}) {
|
|
return consumeGroupCallback({ ...fixture, url: fixture.url.href, identityOrigin, hospitalOrigin, now: 1500, ...overrides })
|
|
}
|
|
|
|
test('S256 对照 RFC 7636 向量', async () => {
|
|
assert.equal(await pkceChallenge(verifier, webcrypto), 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM')
|
|
})
|
|
test('只允许 HTTPS 或回环 HTTP 固定 origin', () => {
|
|
assert.equal(fixedOrigin(identityOrigin), identityOrigin)
|
|
for (const value of ['http://example.com', identityOrigin + '/bad', identityOrigin + '?redirect=x', 'https://u:p@example.com', 'javascript:alert(1)']) {
|
|
assert.throws(() => fixedOrigin(value))
|
|
}
|
|
})
|
|
test('初始化在医院存 verifier,授权 URL 仅有 S256 challenge', async () => {
|
|
const fixture = await transaction()
|
|
const saved = JSON.parse(fixture.storage.getItem(GROUP_TRANSACTION_KEY))
|
|
assert.equal(saved.verifier.length, 43)
|
|
assert.equal(saved.state.length, 43)
|
|
assert.equal(fixture.target.pathname, '/sso/authorize')
|
|
assert.equal(fixture.target.searchParams.get('redirect_uri'), hospitalOrigin + '/admin/group-login')
|
|
assert.equal(fixture.target.searchParams.get('code_challenge'), await pkceChallenge(saved.verifier, webcrypto))
|
|
assert.equal(fixture.target.href.includes(saved.verifier), false)
|
|
})
|
|
test('回调同步清 URL 和消费本地事务,不能重放', async () => {
|
|
const fixture = await transaction()
|
|
const result = consume(fixture)
|
|
assert.equal(result.code, 'c'.repeat(64))
|
|
assert.equal(fixture.history.calls[0][2], '/admin/group-login')
|
|
assert.equal(fixture.storage.getItem(GROUP_TRANSACTION_KEY), null)
|
|
assert.throws(() => consume(fixture))
|
|
})
|
|
test('拒绝错误 state、过期、未来事务、任意 redirect、重复参数和错误医院地址', async () => {
|
|
for (const mutate of [
|
|
(f) => f.url.searchParams.set('state', 'x'.repeat(43)),
|
|
(f) => f.url.searchParams.set('redirect', 'https://example.com'),
|
|
(f) => f.url.searchParams.append('code', 'x'.repeat(64)),
|
|
(f) => { f.url.pathname = '/admin/login' },
|
|
(f) => { f.url.hash = 'unexpected' },
|
|
(f) => { const saved = JSON.parse(f.storage.getItem(GROUP_TRANSACTION_KEY)); saved.createdAt = -400000; f.storage.setItem(GROUP_TRANSACTION_KEY, JSON.stringify(saved)) },
|
|
(f) => { const saved = JSON.parse(f.storage.getItem(GROUP_TRANSACTION_KEY)); saved.createdAt = 2000; f.storage.setItem(GROUP_TRANSACTION_KEY, JSON.stringify(saved)) }
|
|
]) {
|
|
const fixture = await transaction()
|
|
mutate(fixture)
|
|
assert.throws(() => consume(fixture))
|
|
assert.equal(fixture.history.calls.length, 1)
|
|
assert.equal(fixture.storage.getItem(GROUP_TRANSACTION_KEY), null)
|
|
}
|
|
})
|
|
test('只执行中央/医院两次 POST,不携带旧 token、拒绝响应指定任意兑换地址', async () => {
|
|
const fixture = await transaction()
|
|
const current = consume(fixture)
|
|
const calls = []
|
|
const responses = [{ ticket: 't'.repeat(64), exchange_path: '/adminapi/identity.hospitalSso/exchange' }, { token: 'fixture-hospital-session-only', is_paw: 1 }]
|
|
const result = await exchangeGroupCode({ transaction: current, identityOrigin, hospitalOrigin, fetcher: async (url, options) => {
|
|
calls.push({ url, options }); return { ok: true, json: async () => ({ code: 1, data: responses.shift() }) }
|
|
} })
|
|
assert.equal(result.is_paw, 1)
|
|
assert.equal(calls.length, 2)
|
|
assert.equal(calls[0].url, identityOrigin + '/adminapi/group/sso/exchange')
|
|
assert.equal(calls[1].url, hospitalOrigin + '/adminapi/identity.hospitalSso/exchange')
|
|
assert.equal(calls[0].options.credentials, 'omit')
|
|
assert.equal(calls[1].options.credentials, 'same-origin')
|
|
assert.equal(calls.every(({ options }) => options.method === 'POST' && options.redirect === 'error' && !options.headers.Authorization && !options.headers.token), true)
|
|
let rejectedCalls = 0
|
|
await assert.rejects(exchangeGroupCode({ transaction: current, identityOrigin, hospitalOrigin, fetcher: async () => {
|
|
rejectedCalls++; return { ok: true, json: async () => ({ code: 1, data: { ticket: 't'.repeat(64), exchange_path: 'https://example.com' } }) }
|
|
} }))
|
|
assert.equal(rejectedCalls, 1)
|
|
})
|
|
test('网络失败不重试,首次改密与企微门禁保留', async () => {
|
|
const fixture = await transaction()
|
|
let count = 0
|
|
await assert.rejects(exchangeGroupCode({ transaction: consume(fixture), identityOrigin, hospitalOrigin, fetcher: async () => { count++; throw new Error('fixture offline') } }))
|
|
assert.equal(count, 1)
|
|
const secondFixture = await transaction()
|
|
count = 0
|
|
await assert.rejects(exchangeGroupCode({ transaction: consume(secondFixture), identityOrigin, hospitalOrigin, fetcher: async () => {
|
|
count++
|
|
if (count === 2) throw new Error('fixture hospital offline')
|
|
return { ok: true, json: async () => ({ code: 1, data: { ticket: 't'.repeat(64), exchange_path: '/adminapi/identity.hospitalSso/exchange' } }) }
|
|
} }))
|
|
assert.equal(count, 2)
|
|
assert.equal(groupLoginDestination({ is_paw: 0, need_bind_work_wechat: true }), '/admin/change-password')
|
|
assert.equal(groupLoginDestination({ is_paw: 1, need_bind_work_wechat: true }), '/admin/bind-work-wechat')
|
|
assert.equal(groupLoginDestination({ is_paw: 1 }), '/admin/')
|
|
})
|