2024-08-27 22:26:56 -04:00
|
|
|
import { createGrpcTransport, GrpcTransportOptions } from "@connectrpc/connect-node";
|
2024-05-28 11:24:12 -04:00
|
|
|
import { importPKCS8, SignJWT } from "jose";
|
2025-01-02 14:44:37 +01:00
|
|
|
import { NewAuthorizationBearerInterceptor } from "./interceptors";
|
2024-05-28 11:24:12 -04:00
|
|
|
|
|
|
|
|
/**
|
2025-01-08 14:20:16 +01:00
|
|
|
* Create a server transport using grpc with the given token and configuration options.
|
2024-05-28 11:24:12 -04:00
|
|
|
* @param token
|
|
|
|
|
* @param opts
|
|
|
|
|
*/
|
2024-08-27 22:26:56 -04:00
|
|
|
export function createServerTransport(token: string, opts: GrpcTransportOptions) {
|
2024-07-16 09:20:40 -04:00
|
|
|
return createGrpcTransport({
|
2024-05-28 11:24:12 -04:00
|
|
|
...opts,
|
2024-08-27 22:26:56 -04:00
|
|
|
interceptors: [...(opts.interceptors || []), NewAuthorizationBearerInterceptor(token)],
|
2024-05-28 11:24:12 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 15:22:14 +01:00
|
|
|
export async function newSystemToken({
|
|
|
|
|
audience,
|
|
|
|
|
subject,
|
|
|
|
|
key,
|
|
|
|
|
expirationTime,
|
|
|
|
|
}: {
|
|
|
|
|
audience: string;
|
|
|
|
|
subject: string;
|
|
|
|
|
key: string;
|
|
|
|
|
expirationTime?: number | string | Date;
|
|
|
|
|
}) {
|
2024-05-28 11:24:12 -04:00
|
|
|
return await new SignJWT({})
|
|
|
|
|
.setProtectedHeader({ alg: "RS256" })
|
|
|
|
|
.setIssuedAt()
|
2025-01-20 15:22:14 +01:00
|
|
|
.setExpirationTime(expirationTime ?? "1h")
|
|
|
|
|
.setIssuer(subject)
|
|
|
|
|
.setSubject(subject)
|
|
|
|
|
.setAudience(audience)
|
|
|
|
|
.sign(await importPKCS8(key, "RS256"));
|
2024-05-28 11:24:12 -04:00
|
|
|
}
|