UNPKG

private-registry-mock

Version:

Simple mocked server and package of an npm private registry.

23 lines (22 loc) 855 B
import { base64ToString } from "uint8array-extras"; import bearerToken from "polka-bearer-token"; import basicAuth from "basic-auth"; export const auth = async (req, res, next) => { const { type: tokenType, value: token } = res.ctx.token; if (tokenType === "bearer") { const bearerMiddleware = bearerToken(); await bearerMiddleware(req, res, () => { }); if (req.token !== token) { res.forbidden(`Invalid token - expected ${token}`); } } else { const authToken = base64ToString(token); const [username, password] = authToken.split(":"); const authentication = basicAuth(req); if (authentication?.name !== username || authentication?.pass !== password) { res.forbidden(`Invalid credentials - expected ${authToken}`); } } void next(); };