@krak-stack/auth 0.1.38 → 0.1.40
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/dist/api.d.ts +22 -0
- package/dist/better-auth/api.d.ts +2 -0
- package/dist/better-auth/index.d.ts +1 -0
- package/dist/better-auth/schema.d.ts +1 -0
- package/dist/better-auth/schema.js +1 -0
- package/dist/components/admin-organization-form.d.ts +6 -0
- package/dist/components/admin-organization-form.js +136 -0
- package/dist/components/admin-organizations.d.ts +7 -0
- package/dist/components/admin-organizations.js +118 -0
- package/dist/components/admin-users.d.ts +8 -0
- package/dist/components/admin-users.js +265 -0
- package/dist/components/auth-api-client.d.ts +22 -0
- package/dist/components/auth-client.d.ts +3 -1
- package/dist/components/auth-forms.js +10 -24
- package/dist/components/auth-provider.d.ts +16 -15
- package/dist/components/auth-provider.js +51 -4
- package/dist/components/index.d.ts +3 -0
- package/dist/components/index.js +3 -0
- package/dist/components/user-button.d.ts +11 -1
- package/dist/components/user-button.js +73 -15
- package/dist/extra/api.d.ts +2 -0
- package/dist/extra/index.d.ts +1 -0
- package/dist/extra/schema.d.ts +1 -0
- package/dist/extra/schema.js +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/schema.d.ts +2 -0
- package/dist/schema.js +3 -0
- package/dist/server/api.d.ts +40 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +1 -0
- package/dist/server/middleware.d.ts +23 -0
- package/dist/server/middleware.js +40 -0
- package/dist/server/schema.d.ts +6 -0
- package/dist/server/schema.js +8 -0
- package/dist/service.d.ts +516 -437
- package/dist/service.js +96 -12
- package/package.json +1 -1
package/dist/service.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Cache, Context, Duration, Effect, Exit, Layer, Redacted, } from "effect";
|
|
2
2
|
import { FetchHttpClient, HttpClient, HttpClientRequest, } from "effect/unstable/http";
|
|
3
3
|
import { HttpApiClient } from "effect/unstable/httpapi";
|
|
4
|
+
import { HttpApiError } from "effect/unstable/httpapi";
|
|
4
5
|
import { AuthApi } from "./api";
|
|
5
6
|
import { AuthClientConfig } from "./config";
|
|
7
|
+
import { ExtraVerifiedApiKey } from "./extra/schema";
|
|
6
8
|
const forwardedAuthHeaderNames = [
|
|
7
9
|
"accept-language",
|
|
8
10
|
"cookie",
|
|
@@ -28,10 +30,21 @@ const authHttpClientLayer = (headers) => Layer.effect(HttpClient.HttpClient, Eff
|
|
|
28
30
|
return HttpClient.mapRequest(http, (request) => HttpClientRequest.setHeaders(request, forwardedAuthHeaders(headers)));
|
|
29
31
|
})).pipe(Layer.provide(FetchHttpClient.layer));
|
|
30
32
|
const getSessionCacheKey = (headers) => JSON.stringify(forwardedAuthHeaders(headers));
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const headerValue = (headers, name) => {
|
|
34
|
+
const lowerName = name.toLowerCase();
|
|
35
|
+
const entry = Object.entries(headers).find(([key]) => key.toLowerCase() === lowerName);
|
|
36
|
+
return entry?.[1];
|
|
37
|
+
};
|
|
38
|
+
const unauthorized = () => new HttpApiError.Unauthorized({});
|
|
39
|
+
const withRequiredOrganization = (authSession) => {
|
|
40
|
+
const activeOrganizationId = authSession.session.activeOrganizationId;
|
|
41
|
+
return activeOrganizationId
|
|
42
|
+
? Effect.succeed({
|
|
43
|
+
...authSession,
|
|
44
|
+
session: { ...authSession.session, activeOrganizationId },
|
|
45
|
+
})
|
|
46
|
+
: Effect.fail(unauthorized());
|
|
47
|
+
};
|
|
35
48
|
const isBetterAuthSessionRequest = (request) => request.url.endsWith("/api/auth/get-session");
|
|
36
49
|
class SessionLookup extends Context.Service()("@krak-stack/auth/SessionLookup") {
|
|
37
50
|
}
|
|
@@ -44,7 +57,9 @@ const sessionCache = Effect.runSync(Cache.makeWith(() => Effect.gen(function* ()
|
|
|
44
57
|
requireServicesAt: "lookup",
|
|
45
58
|
}));
|
|
46
59
|
export class AuthService extends Context.Service()("@krak-stack/auth/AuthService", {
|
|
47
|
-
make: (
|
|
60
|
+
make: (options = {}) => Effect.gen(function* () {
|
|
61
|
+
const headers = options.headers ?? {};
|
|
62
|
+
const apiKeyConfigId = options.apiKeyConfigId ?? "user";
|
|
48
63
|
const config = yield* AuthClientConfig;
|
|
49
64
|
const http = yield* HttpClient.HttpClient;
|
|
50
65
|
const httpClient = HttpClient.mapRequest(http, (request) => isBetterAuthSessionRequest(request)
|
|
@@ -55,19 +70,88 @@ export class AuthService extends Context.Service()("@krak-stack/auth/AuthService
|
|
|
55
70
|
httpClient,
|
|
56
71
|
});
|
|
57
72
|
const sessionCacheKey = getSessionCacheKey(headers);
|
|
73
|
+
const apiKey = headerValue(headers, "x-api-key")?.trim();
|
|
74
|
+
const getCookieSession = () => Cache.get(sessionCache, sessionCacheKey).pipe(Effect.provideService(SessionLookup, {
|
|
75
|
+
getSession: api.auth.getSession,
|
|
76
|
+
}), Effect.mapError(unauthorized), Effect.map((authSession) => authSession
|
|
77
|
+
? {
|
|
78
|
+
...authSession,
|
|
79
|
+
isSuperAdminImpersonation: false,
|
|
80
|
+
authMethod: { type: "cookie" },
|
|
81
|
+
}
|
|
82
|
+
: null));
|
|
83
|
+
const userForApiKey = (verifiedKey) => {
|
|
84
|
+
if (verifiedKey.configId === "organization") {
|
|
85
|
+
return Effect.succeed(undefined);
|
|
86
|
+
}
|
|
87
|
+
return api.users
|
|
88
|
+
.getUser({ params: { id: verifiedKey.referenceId } })
|
|
89
|
+
.pipe(Effect.mapError(unauthorized));
|
|
90
|
+
};
|
|
91
|
+
const activeOrganizationIdForApiKey = (verifiedKey, user) => {
|
|
92
|
+
if (verifiedKey.configId === "organization") {
|
|
93
|
+
return Effect.succeed({ id: verifiedKey.referenceId });
|
|
94
|
+
}
|
|
95
|
+
if (!user)
|
|
96
|
+
return Effect.succeed({ id: null });
|
|
97
|
+
return api.users
|
|
98
|
+
.getUserActiveOrganization({ params: { userId: user.id } })
|
|
99
|
+
.pipe(Effect.catch(() => Effect.succeed({ id: null })));
|
|
100
|
+
};
|
|
101
|
+
const getApiKeySession = (key) => Effect.gen(function* () {
|
|
102
|
+
const verified = yield* api.authExtra
|
|
103
|
+
.verifyApiKey({
|
|
104
|
+
payload: { key, configId: apiKeyConfigId },
|
|
105
|
+
})
|
|
106
|
+
.pipe(Effect.mapError(unauthorized));
|
|
107
|
+
if (!verified.valid || !verified.key) {
|
|
108
|
+
return yield* Effect.fail(unauthorized());
|
|
109
|
+
}
|
|
110
|
+
const user = yield* userForApiKey(verified.key);
|
|
111
|
+
const activeOrganization = yield* activeOrganizationIdForApiKey(verified.key, user);
|
|
112
|
+
const now = new Date();
|
|
113
|
+
return {
|
|
114
|
+
session: {
|
|
115
|
+
id: `api-key:${verified.key.id}`,
|
|
116
|
+
token: verified.key.id,
|
|
117
|
+
userId: user?.id ?? `api-key:${verified.key.id}`,
|
|
118
|
+
activeOrganizationId: activeOrganization.id,
|
|
119
|
+
createdAt: now,
|
|
120
|
+
updatedAt: now,
|
|
121
|
+
expiresAt: verified.key.expiresAt ?? new Date(now.getTime() + 3600000),
|
|
122
|
+
},
|
|
123
|
+
user,
|
|
124
|
+
isSuperAdminImpersonation: false,
|
|
125
|
+
authMethod: {
|
|
126
|
+
type: "apiKey",
|
|
127
|
+
apiKey: verified.key,
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
const getSession = () => apiKey ? getApiKeySession(apiKey) : getCookieSession();
|
|
132
|
+
const requireSession = () => getSession().pipe(Effect.flatMap((authSession) => authSession
|
|
133
|
+
? Effect.succeed(authSession)
|
|
134
|
+
: Effect.fail(unauthorized())));
|
|
135
|
+
const requireUser = () => requireSession().pipe(Effect.flatMap((authSession) => authSession.user
|
|
136
|
+
? Effect.succeed({ ...authSession, user: authSession.user })
|
|
137
|
+
: Effect.fail(unauthorized())));
|
|
138
|
+
const requireOrganization = () => requireSession().pipe(Effect.flatMap(withRequiredOrganization));
|
|
139
|
+
const requireUserOrganization = () => requireUser().pipe(Effect.flatMap(withRequiredOrganization));
|
|
58
140
|
return {
|
|
59
141
|
...api,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
},
|
|
142
|
+
getSession,
|
|
143
|
+
requireSession,
|
|
144
|
+
requireUser,
|
|
145
|
+
requireOrganization,
|
|
146
|
+
requireUserOrganization,
|
|
66
147
|
};
|
|
67
148
|
}),
|
|
68
149
|
}) {
|
|
69
150
|
static layer = (options = {}) => {
|
|
70
151
|
const headers = options.headers ?? {};
|
|
71
|
-
return Layer.effect(this, this.make(headers)).pipe(Layer.provide(AuthClientConfig.layer(
|
|
152
|
+
return Layer.effect(this, this.make({ ...options, headers })).pipe(Layer.provide(AuthClientConfig.layer({
|
|
153
|
+
...(options.baseUrl ? { baseUrl: options.baseUrl } : {}),
|
|
154
|
+
...(options.apiKey ? { apiKey: options.apiKey } : {}),
|
|
155
|
+
})), Layer.provide(authHttpClientLayer(headers)));
|
|
72
156
|
};
|
|
73
157
|
}
|