Matthieu Sieben 7f26b17652
Add OAuth tests (#2874)
* Improve error message when using invalid client_id during code exchange

* Extract SPA example OAuth client in own package

* wip

* remove dependency on get-port

* Properly configure jest to only transpile "get-port" from node_modules

https://jestjs.io/docs/configuration#transformignorepatterns-arraystring

* Use dynamically assigned port number during tests

* use puppeteer to run tests

* remove login input "id" attribute

* code style

* add missing declaration

* tidy

* headless

* remove get-port dependency

* fix tests/proxied/admin.test.ts

* fix tests

* Allow unsecure oauth providers through configuration

* transpile "lande" during ozone tests

* Cache Puppeteer browser binaries

* Use puppeteer cache during all workflow steps

* remove use of set-output

* use get-port in xrpc-server tests

* Renamed to allowHttp

* tidy

* tidy
2024-10-18 15:40:05 +02:00

56 lines
1.4 KiB
TypeScript

import * as http from 'node:http'
import { once } from 'node:events'
import express from 'express'
import * as xrpc from '../src'
import { AuthRequiredError } from '../src'
export async function createServer({
router,
}: xrpc.Server): Promise<http.Server> {
const app = express()
app.use(router)
const httpServer = app.listen(0)
await once(httpServer, 'listening')
return httpServer
}
export async function closeServer(httpServer: http.Server) {
await new Promise((r) => {
httpServer.close(() => r(undefined))
})
}
export function createBasicAuth(allowed: {
username: string
password: string
}) {
return function (ctx: { req: http.IncomingMessage }) {
const header = ctx.req.headers.authorization ?? ''
if (!header.startsWith('Basic ')) {
throw new AuthRequiredError()
}
const original = header.replace('Basic ', '')
const [username, password] = Buffer.from(original, 'base64')
.toString()
.split(':')
if (username !== allowed.username || password !== allowed.password) {
throw new AuthRequiredError()
}
return {
credentials: { username },
artifacts: { original },
}
}
}
export function basicAuthHeaders(creds: {
username: string
password: string
}) {
return {
authorization:
'Basic ' +
Buffer.from(`${creds.username}:${creds.password}`).toString('base64'),
}
}