@krak-stack/auth 0.2.22 → 0.2.24
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.
- package/CHANGELOG.md +20 -0
- package/README.md +54 -0
- package/dist/api.d.ts +1364 -1364
- package/dist/components/admin-organizations.js +8 -3
- package/dist/components/admin-users.js +15 -5
- package/dist/components/auth-forms.js +144 -61
- package/dist/components/email-otp-state.d.ts +11 -0
- package/dist/components/email-otp-state.js +25 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/components/organization-switcher.js +6 -4
- package/dist/components/password-field.d.ts +15 -0
- package/dist/components/password-field.js +12 -0
- package/dist/components/password-reset-flow.d.ts +25 -0
- package/dist/components/password-reset-flow.js +51 -0
- package/dist/components/password-reset.d.ts +34 -0
- package/dist/components/password-reset.js +41 -0
- package/dist/components/user-button.d.ts +18 -0
- package/dist/components/user-button.js +147 -69
- package/dist/server/actor.js +1 -13
- package/dist/server/attribution.d.ts +3 -0
- package/dist/server/attribution.js +26 -0
- package/dist/server/index.d.ts +1 -1
- package/dist/server/middleware.d.ts +7 -1
- package/dist/server/middleware.js +60 -28
- package/package.json +3 -3
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Cause, Effect, Layer, Redacted } from "effect";
|
|
2
|
-
import { HttpServerRequest } from "effect/http";
|
|
2
|
+
import { HttpRouter, HttpServerRequest } from "effect/http";
|
|
3
3
|
import { HttpApiError, HttpApiMiddleware, HttpApiSecurity, } from "effect/http-api";
|
|
4
|
-
import { AuthService } from "../service.js";
|
|
4
|
+
import { AuthService, } from "../service.js";
|
|
5
|
+
import { AuthClientConfig } from "../config.js";
|
|
6
|
+
import { withAuthAnnotations } from "./attribution.js";
|
|
5
7
|
export const ApiKeySecurity = HttpApiSecurity.apiKey({
|
|
6
8
|
key: "x-api-key",
|
|
7
9
|
in: "header",
|
|
@@ -17,43 +19,73 @@ export class AuthMiddleware extends HttpApiMiddleware.Service()("@krak-stack/aut
|
|
|
17
19
|
},
|
|
18
20
|
}) {
|
|
19
21
|
static layer = (options = {}) => makeAuthenticationLive(options);
|
|
22
|
+
/** The same optional authentication and attribution for plain HttpRouter routes. */
|
|
23
|
+
static routerLayer = (options = {}) => HttpRouter.middleware(Effect.gen(function* () {
|
|
24
|
+
const config = options.authLayer
|
|
25
|
+
? undefined
|
|
26
|
+
: yield* AuthClientConfig.make(options);
|
|
27
|
+
return (httpEffect) => Effect.gen(function* () {
|
|
28
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
29
|
+
if (options.paths &&
|
|
30
|
+
!options.paths.includes(pathnameFromUrl(request.url))) {
|
|
31
|
+
return yield* httpEffect;
|
|
32
|
+
}
|
|
33
|
+
const { auth, session } = yield* authenticateRequest({ ...options, ...config }, options.authLayer);
|
|
34
|
+
return yield* withAuthAnnotations(httpEffect.pipe(Effect.provideService(AuthService, auth)), sessionAnnotations(session));
|
|
35
|
+
});
|
|
36
|
+
})).layer;
|
|
20
37
|
}
|
|
21
38
|
const isAuthServiceLayer = (value) => Layer.isLayer(value);
|
|
22
39
|
export const makeAuthenticationLive = (options = {}) => {
|
|
23
40
|
const legacyLayer = isAuthServiceLayer(options) ? options : undefined;
|
|
24
41
|
const authOptions = isAuthServiceLayer(options) ? {} : options;
|
|
25
42
|
const authLayer = legacyLayer ?? authOptions.authLayer;
|
|
26
|
-
const allowedOrganizationImpersonationRoutes = authOptions.allowedOrganizationImpersonationRoutes ?? [];
|
|
27
43
|
return Layer.effect(AuthMiddleware, Effect.gen(function* () {
|
|
28
44
|
return {
|
|
29
|
-
apiKey: (httpEffect, { credential }) => Effect.
|
|
30
|
-
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
31
|
-
const apiKey = Redacted.value(credential).trim();
|
|
32
|
-
const headers = apiKey
|
|
33
|
-
? { ...request.headers, "x-api-key": apiKey }
|
|
34
|
-
: request.headers;
|
|
35
|
-
const auth = yield* AuthService.pipe(Effect.provide(authLayer ??
|
|
36
|
-
AuthService.layer({
|
|
37
|
-
...authOptions,
|
|
38
|
-
headers,
|
|
39
|
-
})), Effect.tapCause((cause) => Effect.logError("Authentication rejected: auth client initialization failed", Cause.pretty(cause))), Effect.mapError(() => new HttpApiError.ServiceUnavailable({})));
|
|
40
|
-
if (!isAllowedRoute(allowedOrganizationImpersonationRoutes, request)) {
|
|
41
|
-
const session = yield* auth
|
|
42
|
-
.getSession()
|
|
43
|
-
.pipe(Effect.catchTag("Unauthorized", () => Effect.succeed(null)));
|
|
44
|
-
if (session?.session.impersonatedByOrganizationId) {
|
|
45
|
-
return yield* new HttpApiError.Forbidden({});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return auth;
|
|
49
|
-
}).pipe(Effect.mapError((error) => error instanceof HttpApiError.Unauthorized ||
|
|
50
|
-
error instanceof HttpApiError.Forbidden ||
|
|
51
|
-
error instanceof HttpApiError.ServiceUnavailable
|
|
52
|
-
? error
|
|
53
|
-
: new HttpApiError.ServiceUnavailable({})), Effect.flatMap((auth) => httpEffect.pipe(Effect.provideService(AuthService, auth)))),
|
|
45
|
+
apiKey: (httpEffect, { credential }) => authenticateRequest(authOptions, authLayer, Redacted.value(credential).trim()).pipe(Effect.flatMap(({ auth, session }) => withAuthAnnotations(httpEffect.pipe(Effect.provideService(AuthService, auth)), sessionAnnotations(session)))),
|
|
54
46
|
};
|
|
55
47
|
}));
|
|
56
48
|
};
|
|
49
|
+
const authenticateRequest = Effect.fnUntraced(function* (options, authLayer, apiKey) {
|
|
50
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
51
|
+
const auth = yield* AuthService.pipe(Effect.provide(authLayer ??
|
|
52
|
+
AuthService.layer({
|
|
53
|
+
...options,
|
|
54
|
+
headers: apiKey
|
|
55
|
+
? { ...request.headers, "x-api-key": apiKey }
|
|
56
|
+
: request.headers,
|
|
57
|
+
})), Effect.tapCause((cause) => Effect.logError("Authentication rejected: auth client initialization failed", Cause.pretty(cause))), Effect.mapError(() => new HttpApiError.ServiceUnavailable({})));
|
|
58
|
+
const session = yield* auth
|
|
59
|
+
.getSession()
|
|
60
|
+
.pipe(Effect.catchTag("Unauthorized", () => Effect.succeed(null)));
|
|
61
|
+
if (session?.session.impersonatedByOrganizationId &&
|
|
62
|
+
!isAllowedRoute(options.allowedOrganizationImpersonationRoutes ?? [], request)) {
|
|
63
|
+
return yield* new HttpApiError.Forbidden({});
|
|
64
|
+
}
|
|
65
|
+
return { auth, session };
|
|
66
|
+
});
|
|
67
|
+
const sessionAnnotations = (session) => {
|
|
68
|
+
const attributes = {};
|
|
69
|
+
if (!session)
|
|
70
|
+
return attributes;
|
|
71
|
+
if (session.user)
|
|
72
|
+
attributes["user.id"] = session.user.id;
|
|
73
|
+
if (session.session.activeOrganizationId) {
|
|
74
|
+
attributes["organization.id"] = session.session.activeOrganizationId;
|
|
75
|
+
}
|
|
76
|
+
if (session.authMethod.type === "cookie" && session.user) {
|
|
77
|
+
attributes["app.actor.type"] = "user";
|
|
78
|
+
attributes["app.actor.id"] = session.user.id;
|
|
79
|
+
}
|
|
80
|
+
else if (session.authMethod.type === "apiKey") {
|
|
81
|
+
const key = session.authMethod.apiKey;
|
|
82
|
+
attributes["app.actor.type"] = "api_key";
|
|
83
|
+
attributes["app.actor.id"] = key.id;
|
|
84
|
+
attributes["app.actor.owner.type"] = key.configId;
|
|
85
|
+
attributes["app.actor.owner.id"] = key.referenceId;
|
|
86
|
+
}
|
|
87
|
+
return attributes;
|
|
88
|
+
};
|
|
57
89
|
const pathnameFromUrl = (url) => new URL(url, "http://localhost").pathname;
|
|
58
90
|
const isAllowedRoute = (routes, request) => routes.some(({ method, path }) => method.toUpperCase() === request.method.toUpperCase() &&
|
|
59
91
|
routePathMatches(path, pathnameFromUrl(request.url)));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krak-stack/auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.24",
|
|
4
4
|
"description": "Effect HttpApi clients and schemas for KrakStack Auth.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -76,10 +76,10 @@
|
|
|
76
76
|
"react-qr-code": "^2.2.0"
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
|
-
"@krak-stack/registry": "0.1.
|
|
79
|
+
"@krak-stack/registry": "0.1.50"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
82
|
-
"@krak-stack/registry": "^0.1.
|
|
82
|
+
"@krak-stack/registry": "^0.1.50",
|
|
83
83
|
"@tanstack/react-router": "^1.170.15",
|
|
84
84
|
"effect": "^4.0.0",
|
|
85
85
|
"input-otp": "^1.5.0",
|