feat: add control plane authentication baseline
This commit is contained in:
1 parent
a4c93ff1a7
commit
0d10e2d124
21 files changed
+1403
-18
No files matched your search
@@ -0,0 +1,81 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import {
|
||||
AuthError,
|
||||
AuthService,
|
||||
MemoryAuthRepository,
|
||||
hashSecret,
|
||||
} from '../src/auth.js'
|
||||
|
||||
function createHarness() {
|
||||
const repository = new MemoryAuthRepository()
|
||||
const auth = new AuthService(repository, {
|
||||
sessionTtlHours: 24,
|
||||
passwordPepper: 'test-pepper',
|
||||
})
|
||||
return { repository, auth }
|
||||
}
|
||||
|
||||
test('administrator creates an invitation and a user redeems it once', async () => {
|
||||
const { auth } = createHarness()
|
||||
await auth.ensureAdmin('admin@moyi.test', 'admin-password', '测试管理员')
|
||||
const adminLogin = await auth.login({ account: 'admin@moyi.test', password: 'admin-password' }, {})
|
||||
const created = await auth.createInvitation(adminLogin.session, { label: '首批用户', maxUses: 1 }, {})
|
||||
|
||||
assert.match(created.code, /^moyi_/)
|
||||
const registered = await auth.register({
|
||||
invitationCode: created.code,
|
||||
account: 'user@moyi.test',
|
||||
password: 'user-password',
|
||||
displayName: '测试用户',
|
||||
}, {})
|
||||
|
||||
assert.equal(registered.user.role, 'user')
|
||||
assert.equal(registered.user.displayName, '测试用户')
|
||||
await assert.rejects(
|
||||
auth.register({
|
||||
invitationCode: created.code,
|
||||
account: 'second@moyi.test',
|
||||
password: 'user-password',
|
||||
}, {}),
|
||||
(error: unknown) => error instanceof AuthError && error.code === 'INVITATION_EXHAUSTED',
|
||||
)
|
||||
})
|
||||
|
||||
test('ordinary users cannot access administrator operations', async () => {
|
||||
const { auth } = createHarness()
|
||||
await auth.ensureAdmin('admin@moyi.test', 'admin-password', '测试管理员')
|
||||
const adminLogin = await auth.login({ account: 'admin@moyi.test', password: 'admin-password' }, {})
|
||||
const invitation = await auth.createInvitation(adminLogin.session, {}, {})
|
||||
const registered = await auth.register({
|
||||
invitationCode: invitation.code,
|
||||
account: 'user@moyi.test',
|
||||
password: 'user-password',
|
||||
}, {})
|
||||
|
||||
await assert.rejects(
|
||||
auth.listUsers(registered.session),
|
||||
(error: unknown) => error instanceof AuthError && error.statusCode === 403,
|
||||
)
|
||||
})
|
||||
|
||||
test('logout revokes the server-side session', async () => {
|
||||
const { auth } = createHarness()
|
||||
await auth.ensureAdmin('admin@moyi.test', 'admin-password', '测试管理员')
|
||||
const login = await auth.login({ account: 'admin@moyi.test', password: 'admin-password' }, {})
|
||||
|
||||
assert.equal((await auth.currentUser(login.session))?.role, 'admin')
|
||||
await auth.revoke(login.session, {})
|
||||
assert.equal(await auth.currentUser(login.session), undefined)
|
||||
})
|
||||
|
||||
test('stored invitation values are hashes rather than plaintext', async () => {
|
||||
const { auth, repository } = createHarness()
|
||||
await auth.ensureAdmin('admin@moyi.test', 'admin-password', '测试管理员')
|
||||
const login = await auth.login({ account: 'admin@moyi.test', password: 'admin-password' }, {})
|
||||
const created = await auth.createInvitation(login.session, {}, {})
|
||||
const stored = await repository.findInvitationByHash(hashSecret(created.code))
|
||||
|
||||
assert.equal(stored?.codeHash, hashSecret(created.code))
|
||||
assert.notEqual(stored?.codeHash, created.code)
|
||||
})
|
||||
Reference in new issue
Block a user