HTTP Proxy - Client
The AuthX proxy for resources is a flexible HTTP proxy that can inject access tokens into a request. It is designed to be deployed alongside an app or worker, and maintains an in-memory cache of fresh access tokens to add the minimun amount of latency. It relies on refresh tokens specified in the configuration or provided by rules.
Example
Here is a typical use case:
We have a resource – often an API – which is accessed by a client. The route /something
is special, and we only want to give access to authorized users.
import AuthXAuthorizationProxy from "@authx/http-proxy-client";
proxy = new AuthXAuthorizationProxy({
authxUrl: `http://localhost:${mockAuthX.port}`,
clientId: "b22282bf-1b78-4ffc-a0d6-2da5465895d0",
clientSecret: "de2c693f-b654-4cf2-b3db-eb37a36bc7a9",
readinessEndpoint: "/_ready",
rules: [
{
test({ url }) {
return url === "/no-token";
},
behavior: {
proxyOptions: { target: `http://localhost:${mockTarget.port}` },
},
},
{
test({ url }) {
return url === "/with-static-token-and-scopes";
},
behavior: {
proxyOptions: { target: `http://localhost:${mockTarget.port}` },
refreshToken: process.env.REFRESH_TOKEN,
sendTokenToTargetWithScopes: ["foo:**:**"],
},
},
{
test({ url }) {
return url === "/with-dynamic-token-and-scopes";
},
behavior(request) {
const refreshToken = request.headers["x-oauth-refresh-token"];
delete request.headers["x-oauth-refresh-token"];
return {
proxyOptions: { target: `http://localhost:${mockTarget.port}` },
refreshToken,
sendTokenToTargetWithScopes: ["**:**:**"],
};
},
},
],
});
Configuration
The proxy is configured with an array of rules, which are checked in order against the request URL until a match is found. If no match is found, the proxy will respond with a status of 404
.
Config
interface Config {
readonly authxUrl: string;
readonly clientId: string;
readonly clientSecret: string;
readonly readinessEndpoint?: string;
readonly refreshCachedTokensAtRemainingLife?: number;
readonly refreshCachedTokensRequestTimeout?: number;
readonly refreshCachedTokensRetryInterval?: number;
readonly evictDormantCachedTokensThreshold?: number;
readonly rules: Rule[];
}
Rule
interface Rule {
readonly test: (request: IncomingMessage) => boolean;
readonly behavior:
| Behavior
| ((
request: IncomingMessage,
response: ServerResponse,
) => Behavior | undefined);
}
Behavior
interface Behavior {
readonly proxyOptions: ServerOptions;
readonly refreshToken?: string;
readonly sendTokenToTargetWithScopes?: string[];
}
Development
Scripts
These scripts can be run using npm run <script>
.
format
Use prettier to format the code in this package.
lint
Check the contents of this package against prettier and eslint rules.
prepare
Build the files from /src
to the /dist
directory with optimizations.
prepare:development
Build the files from /src
to the /dist
directory, and re-build as changes are made to source files.
test
Run all tests from the /dist
directory.
test:development
Run all tests from the /dist
directory, and re-run a test when it changes.
Files
This holds the source code for the proxy.
The compiled and bundled code ends up here for distribution. This is ignored by git.