@krak-stack/auth 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ export { AuthService } from "../service.js";
2
2
  export { AuthClientConfig } from "../config.js";
3
3
  export { ServerDomainsApiGroup, ServerOrganizationsApiGroup, ServerUsersApiGroup, } from "./api.group.js";
4
4
  export { proxyAuthHttpEffect, proxyAuthRequest, proxyAuthRequestEffect, } from "./proxy.js";
5
+ export { AuthProxyError, authProxyKeyHeader, proxyOriginHeaders, readProxyOrigin, stripProxyOriginHeaders, } from "./proxy-origin.js";
5
6
  export { ActorRequired } from "./actor.js";
6
7
  export { ApiKeySecurity, AuthMiddleware, makeAuthenticationLive, } from "./middleware.js";
7
8
  export { ServerActiveOrganization, ServerCreateDomainPayload, ServerDomain, ServerDomainHostParams, ServerDomainIdParams, ServerDomainRecordsResponse, ServerMembersResponse, ServerOrganizationChildrenResponse, ServerOrganizationsResponse, ServerUpdateDomainPayload, ServerUsersResponse, } from "./schema.js";
@@ -0,0 +1,17 @@
1
+ import { Effect, Option, Redacted } from "effect";
2
+ declare const AuthProxyError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
3
+ readonly _tag: "AuthProxyError";
4
+ } & Readonly<A>;
5
+ export declare class AuthProxyError extends AuthProxyError_base<{
6
+ readonly reason: "invalid" | "configuration" | "upstream";
7
+ }> {
8
+ }
9
+ export declare const authProxyKeyHeader = "x-krakstack-proxy-key";
10
+ export declare const stripProxyOriginHeaders: (request: Request) => Headers;
11
+ export declare const proxyOriginHeaders: (request: Request, apiKey?: Redacted.Redacted<string> | undefined) => Effect.Effect<Headers, AuthProxyError, never>;
12
+ export declare const readProxyOrigin: (request: Request) => Effect.Effect<Option.Option<{
13
+ readonly apiKey: Redacted.Redacted<string>;
14
+ readonly host: string;
15
+ readonly protocol: "http" | "https";
16
+ }>, AuthProxyError, never>;
17
+ export {};
@@ -0,0 +1,41 @@
1
+ import { Data, Effect, Option, Redacted, Schema } from "effect";
2
+ import { AuthProxyOrigin } from "./schema.js";
3
+ export class AuthProxyError extends Data.TaggedError("AuthProxyError") {
4
+ }
5
+ export const authProxyKeyHeader = "x-krakstack-proxy-key";
6
+ export const stripProxyOriginHeaders = (request) => {
7
+ const headers = new Headers(request.headers);
8
+ for (const name of Array.from(headers.keys())) {
9
+ if (name === "forwarded" ||
10
+ name.startsWith("x-forwarded-") ||
11
+ name.startsWith("x-krakstack-"))
12
+ headers.delete(name);
13
+ }
14
+ return headers;
15
+ };
16
+ export const proxyOriginHeaders = Effect.fn("AuthProxy.originHeaders")(function* (request, apiKey) {
17
+ const url = new URL(request.url);
18
+ const origin = yield* Schema.decodeUnknownEffect(AuthProxyOrigin)({
19
+ host: url.host,
20
+ protocol: url.protocol.slice(0, -1),
21
+ apiKey: apiKey
22
+ ? Redacted.value(apiKey)
23
+ : process.env.KRAKSTACK_AUTH_SERVICE_API_KEY,
24
+ }).pipe(Effect.mapError(() => new AuthProxyError({ reason: "configuration" })));
25
+ return new Headers({
26
+ "x-krakstack-forwarded-host": origin.host,
27
+ "x-krakstack-forwarded-proto": origin.protocol,
28
+ [authProxyKeyHeader]: Redacted.value(origin.apiKey),
29
+ });
30
+ });
31
+ export const readProxyOrigin = Effect.fn("AuthProxy.readOrigin")(function* (request) {
32
+ if (!Array.from(request.headers.keys()).some((name) => name.startsWith("x-krakstack-"))) {
33
+ return Option.none();
34
+ }
35
+ const origin = yield* Schema.decodeUnknownEffect(AuthProxyOrigin)({
36
+ host: request.headers.get("x-krakstack-forwarded-host"),
37
+ protocol: request.headers.get("x-krakstack-forwarded-proto"),
38
+ apiKey: request.headers.get(authProxyKeyHeader),
39
+ }).pipe(Effect.mapError(() => new AuthProxyError({ reason: "invalid" })));
40
+ return Option.some(origin);
41
+ });
@@ -1,8 +1,10 @@
1
- import { Effect } from "effect";
1
+ import { Effect, type Redacted } from "effect";
2
2
  import { HttpClient, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
3
+ import { AuthProxyError } from "./proxy-origin.js";
3
4
  export interface ProxyAuthRequestOptions {
4
5
  cookieDomain?: string;
6
+ apiKey?: Redacted.Redacted<string>;
5
7
  }
6
8
  export declare const proxyAuthRequest: (request: Request, baseUrl: string | URL, options?: ProxyAuthRequestOptions) => Promise<Response>;
7
- export declare const proxyAuthRequestEffect: (request: Request, baseUrl: string | URL, options?: ProxyAuthRequestOptions) => Effect.Effect<HttpServerResponse.HttpServerResponse, import("effect/unstable/http/HttpClientError").HttpClientError, HttpClient.HttpClient>;
8
- export declare const proxyAuthHttpEffect: (baseUrl: string | URL, options?: ProxyAuthRequestOptions) => Effect.Effect<HttpServerResponse.HttpServerResponse, import("effect/unstable/http/HttpClientError").HttpClientError | import("effect/unstable/http/HttpServerError").RequestError, HttpClient.HttpClient | HttpServerRequest.HttpServerRequest>;
9
+ export declare const proxyAuthRequestEffect: (request: Request, baseUrl: string | URL, options?: ProxyAuthRequestOptions) => Effect.Effect<HttpServerResponse.HttpServerResponse, AuthProxyError, HttpClient.HttpClient>;
10
+ export declare const proxyAuthHttpEffect: (baseUrl: string | URL, options?: ProxyAuthRequestOptions) => Effect.Effect<HttpServerResponse.HttpServerResponse, AuthProxyError | import("effect/unstable/http/HttpServerError").RequestError, HttpClient.HttpClient | HttpServerRequest.HttpServerRequest>;
@@ -1,5 +1,6 @@
1
1
  import { Effect } from "effect";
2
2
  import { Cookies, FetchHttpClient, Headers as HttpHeaders, HttpClient, HttpClientRequest, HttpServerRequest, HttpServerResponse, } from "effect/unstable/http";
3
+ import { AuthProxyError, authProxyKeyHeader, proxyOriginHeaders, } from "./proxy-origin.js";
3
4
  const blockedRequestHeaders = [
4
5
  "accept-encoding",
5
6
  "b3",
@@ -66,31 +67,29 @@ const proxyResponse = async (request, response, options) => {
66
67
  const forwardedUrl = (request) => {
67
68
  return new URL(request.url);
68
69
  };
69
- const preservesOAuthOrigin = (pathname) => pathname === "/api/auth/sign-in/social" ||
70
- pathname.startsWith("/api/auth/callback/");
71
70
  const proxyHeaders = (request) => {
72
71
  const headers = new Headers(request.headers);
73
72
  const url = forwardedUrl(request);
74
73
  for (const header of blockedRequestHeaders)
75
74
  headers.delete(header);
76
75
  for (const header of Array.from(headers.keys())) {
77
- if (header.startsWith("cf-") || header.startsWith("x-forwarded-")) {
76
+ if (header.startsWith("cf-") ||
77
+ header.startsWith("x-forwarded-") ||
78
+ header.startsWith("x-krakstack-")) {
78
79
  headers.delete(header);
79
80
  }
80
81
  }
81
82
  headers.set("x-forwarded-host", url.host);
82
83
  headers.set("x-forwarded-proto", url.protocol.replace(":", ""));
83
- if (preservesOAuthOrigin(url.pathname)) {
84
- headers.set("x-krakstack-forwarded-host", url.host);
85
- headers.set("x-krakstack-forwarded-proto", url.protocol.replace(":", ""));
86
- }
87
84
  return headers;
88
85
  };
89
- const proxyRequest = (request, baseUrl) => {
86
+ const proxyRequest = (request, baseUrl, originHeaders) => {
90
87
  const target = new URL(request.url);
91
88
  const authUrl = new URL(baseUrl);
89
+ const headers = proxyHeaders(request);
90
+ originHeaders.forEach((value, name) => headers.set(name, value));
92
91
  const init = {
93
- headers: proxyHeaders(request),
92
+ headers,
94
93
  method: request.method,
95
94
  };
96
95
  target.protocol = authUrl.protocol;
@@ -103,13 +102,19 @@ const proxyRequest = (request, baseUrl) => {
103
102
  return new Request(target, init);
104
103
  };
105
104
  export const proxyAuthRequest = async (request, baseUrl, options) => {
106
- const response = await fetch(proxyRequest(request, baseUrl), {
105
+ const originHeaders = await Effect.runPromise(proxyOriginHeaders(request, options?.apiKey));
106
+ const response = await fetch(proxyRequest(request, baseUrl, originHeaders), {
107
107
  redirect: "manual",
108
108
  });
109
109
  return proxyResponse(request, response, options);
110
110
  };
111
111
  export const proxyAuthRequestEffect = (request, baseUrl, options) => Effect.gen(function* () {
112
- const response = yield* HttpClient.execute(HttpClientRequest.fromWeb(proxyRequest(request, baseUrl))).pipe(Effect.provideService(FetchHttpClient.RequestInit, {
112
+ const originHeaders = yield* proxyOriginHeaders(request, options?.apiKey);
113
+ const redactedNames = yield* HttpHeaders.CurrentRedactedNames;
114
+ const response = yield* HttpClient.execute(HttpClientRequest.fromWeb(proxyRequest(request, baseUrl, originHeaders))).pipe(Effect.provideService(HttpHeaders.CurrentRedactedNames, [
115
+ ...redactedNames,
116
+ authProxyKeyHeader,
117
+ ]), Effect.mapError(() => new AuthProxyError({ reason: "upstream" })), Effect.provideService(FetchHttpClient.RequestInit, {
113
118
  redirect: "manual",
114
119
  }));
115
120
  const cookieDomain = sharedCookieDomain(request, options);
@@ -1,4 +1,9 @@
1
1
  import { Schema } from "effect";
2
+ export declare const AuthProxyOrigin: Schema.Struct<{
3
+ readonly apiKey: Schema.RedactedFromValue<Schema.NonEmptyString>;
4
+ readonly host: Schema.String;
5
+ readonly protocol: Schema.Literals<readonly ["http", "https"]>;
6
+ }>;
2
7
  export declare const ServerIdsQuery: Schema.Struct<{
3
8
  readonly ids: Schema.NonEmptyString;
4
9
  }>;
@@ -1,5 +1,10 @@
1
1
  import { Schema } from "effect";
2
2
  import { Member, Organization, User } from "../schema.js";
3
+ export const AuthProxyOrigin = Schema.Struct({
4
+ apiKey: Schema.RedactedFromValue(Schema.NonEmptyString),
5
+ host: Schema.String.check(Schema.isPattern(/^(?:[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?|\[[a-f0-9:]+\])(?::[0-9]{1,5})?$/)),
6
+ protocol: Schema.Literals(["http", "https"]),
7
+ }).annotate({ identifier: "AuthProxyOrigin" });
3
8
  export const ServerIdsQuery = Schema.Struct({
4
9
  ids: Schema.NonEmptyString,
5
10
  }).annotate({
package/dist/service.d.ts CHANGED
@@ -1151,7 +1151,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
1151
1151
  }>>;
1152
1152
  readonly status: import("effect/Schema").optional<import("effect/Schema").Boolean>;
1153
1153
  }>>, import("effect/Schema").toCodecJson<typeof import("./auth/schema.js").AuthBadRequest>, never, never>, never, never>;
1154
- readonly signOut: HttpApiClient.Client.Method<import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"signOut", "POST", "/api/auth/sign-out", never, never, never, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{
1154
+ readonly signOut: HttpApiClient.Client.Method<import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"signOut", "POST", "/api/auth/sign-out", never, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{}>>, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{
1155
1155
  readonly success: import("effect/Schema").Boolean;
1156
1156
  }>>, import("effect/Schema").toCodecJson<typeof import("./auth/schema.js").AuthBadRequest>, never, never>, never, never>;
1157
1157
  readonly twoFactorDisable: HttpApiClient.Client.Method<import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"twoFactorDisable", "POST", "/api/auth/two-factor/disable", never, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{
@@ -3174,7 +3174,7 @@ declare const AuthService_base: Context.ServiceClass<AuthService, "@krak-stack/a
3174
3174
  }>>;
3175
3175
  readonly status: import("effect/Schema").optional<import("effect/Schema").Boolean>;
3176
3176
  }>>, import("effect/Schema").toCodecJson<typeof import("./auth/schema.js").AuthBadRequest>, never, never>, never, never>;
3177
- readonly signOut: HttpApiClient.Client.Method<import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"signOut", "POST", "/api/auth/sign-out", never, never, never, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{
3177
+ readonly signOut: HttpApiClient.Client.Method<import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"signOut", "POST", "/api/auth/sign-out", never, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{}>>, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{
3178
3178
  readonly success: import("effect/Schema").Boolean;
3179
3179
  }>>, import("effect/Schema").toCodecJson<typeof import("./auth/schema.js").AuthBadRequest>, never, never>, never, never>;
3180
3180
  readonly twoFactorDisable: HttpApiClient.Client.Method<import("effect/unstable/httpapi/HttpApiEndpoint").HttpApiEndpoint<"twoFactorDisable", "POST", "/api/auth/two-factor/disable", never, never, import("effect/Schema").toCodecJson<import("effect/Schema").Struct<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/auth",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "Effect HttpApi clients and schemas for KrakStack Auth.",
5
5
  "license": "MIT",
6
6
  "files": [