@jskit-ai/auth-provider-supabase-core 0.2.1 → 0.2.3
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/package.json
CHANGED
|
@@ -312,7 +312,7 @@ function createService(options) {
|
|
|
312
312
|
}
|
|
313
313
|
}
|
|
314
314
|
|
|
315
|
-
async function verifyAccessTokenViaSupabase(accessToken) {
|
|
315
|
+
async function verifyAccessTokenViaSupabase(accessToken, profileOptions = {}) {
|
|
316
316
|
const supabase = getSupabaseClient();
|
|
317
317
|
|
|
318
318
|
try {
|
|
@@ -329,7 +329,7 @@ function createService(options) {
|
|
|
329
329
|
return { status: "invalid" };
|
|
330
330
|
}
|
|
331
331
|
|
|
332
|
-
const profile = await syncProfileFromSupabaseUser(response.data.user, response.data.user.email);
|
|
332
|
+
const profile = await syncProfileFromSupabaseUser(response.data.user, response.data.user.email, profileOptions);
|
|
333
333
|
return {
|
|
334
334
|
status: "valid",
|
|
335
335
|
profile
|
|
@@ -624,7 +624,12 @@ function createService(options) {
|
|
|
624
624
|
};
|
|
625
625
|
}
|
|
626
626
|
|
|
627
|
-
|
|
627
|
+
// With syncProfile: false the caller asks only who is signed in, so the
|
|
628
|
+
// existing profile is read instead of synchronized.
|
|
629
|
+
async function syncProfileMirror(nextProfile, { syncProfile = true } = {}) {
|
|
630
|
+
if (!syncProfile) {
|
|
631
|
+
return findProfileByIdentity(nextProfile);
|
|
632
|
+
}
|
|
628
633
|
try {
|
|
629
634
|
const normalized = buildNormalizedIdentityProfile(nextProfile);
|
|
630
635
|
const synchronizedProfile = await userProfileSyncService.syncIdentityProfile(normalized);
|
|
@@ -642,7 +647,7 @@ function createService(options) {
|
|
|
642
647
|
}
|
|
643
648
|
}
|
|
644
649
|
|
|
645
|
-
async function syncProfileFromSupabaseUser(supabaseUser, fallbackEmail) {
|
|
650
|
+
async function syncProfileFromSupabaseUser(supabaseUser, fallbackEmail, profileOptions = {}) {
|
|
646
651
|
const supabaseUserId = normalizeProviderUserId(supabaseUser?.id);
|
|
647
652
|
const email = normalizeEmail(supabaseUser?.email || fallbackEmail);
|
|
648
653
|
|
|
@@ -655,7 +660,7 @@ function createService(options) {
|
|
|
655
660
|
authProviderUserSid: supabaseUserId,
|
|
656
661
|
email,
|
|
657
662
|
displayName: resolveDisplayName(supabaseUser, email)
|
|
658
|
-
});
|
|
663
|
+
}, profileOptions);
|
|
659
664
|
}
|
|
660
665
|
|
|
661
666
|
function buildActorFromProfile(profile) {
|
|
@@ -697,7 +702,7 @@ function createService(options) {
|
|
|
697
702
|
return async (...args) => withActor(await operation(...args));
|
|
698
703
|
}
|
|
699
704
|
|
|
700
|
-
async function syncProfileFromJwtClaims(claims) {
|
|
705
|
+
async function syncProfileFromJwtClaims(claims, { syncProfile = true } = {}) {
|
|
701
706
|
const supabaseUserId = normalizeProviderUserId(claims?.sub);
|
|
702
707
|
if (!supabaseUserId) {
|
|
703
708
|
throw new AppError(401, "Token is missing subject claim.", { code: "auth.supabase.token.subjectMissing" });
|
|
@@ -707,6 +712,9 @@ function createService(options) {
|
|
|
707
712
|
authProvider: authProviderId,
|
|
708
713
|
authProviderUserSid: supabaseUserId
|
|
709
714
|
});
|
|
715
|
+
if (!syncProfile) {
|
|
716
|
+
return existing;
|
|
717
|
+
}
|
|
710
718
|
const emailFromToken = normalizeEmail(claims?.email || "");
|
|
711
719
|
|
|
712
720
|
if (!emailFromToken) {
|
|
@@ -884,8 +892,12 @@ function createService(options) {
|
|
|
884
892
|
resolveDevAuthSecurityStatus
|
|
885
893
|
});
|
|
886
894
|
|
|
887
|
-
async function authenticateRequest(request) {
|
|
895
|
+
async function authenticateRequest(request, { syncProfile = true, refreshSession = true } = {}) {
|
|
888
896
|
const { accessToken, refreshToken } = resolveRequestSessionTokens(request);
|
|
897
|
+
// Without sync, a login whose app profile does not exist yet is not signed in.
|
|
898
|
+
const signedIn = (profile, session = null) => (profile
|
|
899
|
+
? withActor({ authenticated: true, profile, clearSession: false, session, transientFailure: false })
|
|
900
|
+
: { authenticated: false, clearSession: false, session: null, transientFailure: false });
|
|
889
901
|
|
|
890
902
|
const devAuthResult = await authenticateDevAuthRequestFromCookies(request, { accessToken, refreshToken });
|
|
891
903
|
if (devAuthResult) {
|
|
@@ -907,14 +919,7 @@ function createService(options) {
|
|
|
907
919
|
const verification = await verifyAccessToken(accessToken);
|
|
908
920
|
|
|
909
921
|
if (verification.status === "valid") {
|
|
910
|
-
|
|
911
|
-
return withActor({
|
|
912
|
-
authenticated: true,
|
|
913
|
-
profile,
|
|
914
|
-
clearSession: false,
|
|
915
|
-
session: null,
|
|
916
|
-
transientFailure: false
|
|
917
|
-
});
|
|
922
|
+
return signedIn(await syncProfileFromJwtClaims(verification.payload, { syncProfile }));
|
|
918
923
|
}
|
|
919
924
|
|
|
920
925
|
if (verification.status === "transient") {
|
|
@@ -927,15 +932,9 @@ function createService(options) {
|
|
|
927
932
|
}
|
|
928
933
|
|
|
929
934
|
if (verification.status === "invalid") {
|
|
930
|
-
const supabaseVerification = await verifyAccessTokenViaSupabase(accessToken);
|
|
935
|
+
const supabaseVerification = await verifyAccessTokenViaSupabase(accessToken, { syncProfile });
|
|
931
936
|
if (supabaseVerification.status === "valid") {
|
|
932
|
-
return
|
|
933
|
-
authenticated: true,
|
|
934
|
-
profile: supabaseVerification.profile,
|
|
935
|
-
clearSession: false,
|
|
936
|
-
session: null,
|
|
937
|
-
transientFailure: false
|
|
938
|
-
});
|
|
937
|
+
return signedIn(supabaseVerification.profile);
|
|
939
938
|
}
|
|
940
939
|
|
|
941
940
|
if (supabaseVerification.status === "transient") {
|
|
@@ -958,6 +957,18 @@ function createService(options) {
|
|
|
958
957
|
};
|
|
959
958
|
}
|
|
960
959
|
|
|
960
|
+
// A caller that cannot store a refreshed session, such as a realtime socket,
|
|
961
|
+
// must not spend the single-use refresh token; it is told to refresh first.
|
|
962
|
+
if (!refreshSession) {
|
|
963
|
+
return {
|
|
964
|
+
authenticated: false,
|
|
965
|
+
refreshRequired: true,
|
|
966
|
+
clearSession: false,
|
|
967
|
+
session: null,
|
|
968
|
+
transientFailure: false
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
|
|
961
972
|
const supabase = getSupabaseClient();
|
|
962
973
|
let refreshResponse;
|
|
963
974
|
try {
|
|
@@ -1008,15 +1019,8 @@ function createService(options) {
|
|
|
1008
1019
|
};
|
|
1009
1020
|
}
|
|
1010
1021
|
|
|
1011
|
-
const profile = await syncProfileFromSupabaseUser(refreshResponse.data.user, refreshResponse.data.user.email);
|
|
1012
|
-
|
|
1013
|
-
return withActor({
|
|
1014
|
-
authenticated: true,
|
|
1015
|
-
profile,
|
|
1016
|
-
clearSession: false,
|
|
1017
|
-
session: refreshResponse.data.session,
|
|
1018
|
-
transientFailure: false
|
|
1019
|
-
});
|
|
1022
|
+
const profile = await syncProfileFromSupabaseUser(refreshResponse.data.user, refreshResponse.data.user.email, { syncProfile });
|
|
1023
|
+
return signedIn(profile, refreshResponse.data.session);
|
|
1020
1024
|
}
|
|
1021
1025
|
|
|
1022
1026
|
function hasAccessTokenCookie(request) {
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { once } from "node:events";
|
|
2
4
|
import test from "node:test";
|
|
5
|
+
import * as jose from "jose";
|
|
3
6
|
import * as authProviderSupabase from "../src/server/lib/index.js";
|
|
4
7
|
import { createAccountFlows } from "../src/server/lib/accountFlows.js";
|
|
5
8
|
import { createPasswordSecurityFlows } from "../src/server/lib/passwordSecurityFlows.js";
|
|
@@ -197,6 +200,84 @@ test("recovery sessions use recovery cookies and do not authenticate the app", a
|
|
|
197
200
|
});
|
|
198
201
|
});
|
|
199
202
|
|
|
203
|
+
test("authentication without profile sync reads the existing profile", async (t) => {
|
|
204
|
+
const { publicKey, privateKey } = await jose.generateKeyPair("ES256");
|
|
205
|
+
const jwk = { ...(await jose.exportJWK(publicKey)), kid: "test-key", alg: "ES256" };
|
|
206
|
+
const jwks = createServer((_request, response) => {
|
|
207
|
+
response.setHeader("content-type", "application/json");
|
|
208
|
+
response.end(JSON.stringify({ keys: [jwk] }));
|
|
209
|
+
});
|
|
210
|
+
jwks.listen(0, "127.0.0.1");
|
|
211
|
+
await once(jwks, "listening");
|
|
212
|
+
t.after(() => new Promise((resolve) => jwks.close(resolve)));
|
|
213
|
+
const supabaseUrl = `http://127.0.0.1:${jwks.address().port}`;
|
|
214
|
+
const accessToken = await new jose.SignJWT({ email: "ada@example.com" })
|
|
215
|
+
.setProtectedHeader({ alg: "ES256", kid: "test-key" })
|
|
216
|
+
.setIssuer(`${supabaseUrl}/auth/v1`)
|
|
217
|
+
.setAudience("authenticated")
|
|
218
|
+
.setSubject("supabase-user-1")
|
|
219
|
+
.setIssuedAt()
|
|
220
|
+
.setExpirationTime("5m")
|
|
221
|
+
.sign(privateKey);
|
|
222
|
+
const profile = { id: "7", email: "ada@example.com", displayName: "Ada", authProvider: "supabase", authProviderUserSid: "supabase-user-1" };
|
|
223
|
+
let existingProfile = profile;
|
|
224
|
+
let syncCalls = 0;
|
|
225
|
+
const authService = createServiceFixture({
|
|
226
|
+
authProvider: { id: "supabase", supabaseUrl, supabasePublishableKey: "publishable-key" },
|
|
227
|
+
userProfileSyncService: {
|
|
228
|
+
async findByIdentity() {
|
|
229
|
+
return existingProfile;
|
|
230
|
+
},
|
|
231
|
+
async syncIdentityProfile() {
|
|
232
|
+
syncCalls += 1;
|
|
233
|
+
return profile;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
const request = { cookies: { sb_access_token: accessToken } };
|
|
238
|
+
|
|
239
|
+
const checked = await authService.authenticateRequest(request, { syncProfile: false });
|
|
240
|
+
assert.equal(checked.authenticated, true);
|
|
241
|
+
assert.equal(checked.actor.providerUserId, "supabase-user-1");
|
|
242
|
+
assert.equal(syncCalls, 0);
|
|
243
|
+
|
|
244
|
+
existingProfile = null;
|
|
245
|
+
const unprojected = await authService.authenticateRequest(request, { syncProfile: false });
|
|
246
|
+
assert.equal(unprojected.authenticated, false, "A login without an app profile is not signed in without sync");
|
|
247
|
+
assert.equal((await authService.authenticateRequest(request)).authenticated, true);
|
|
248
|
+
assert.equal(syncCalls, 1);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test("a caller that cannot store a refreshed session is told to refresh instead of spending the token", async (t) => {
|
|
252
|
+
const tokenRequests = [];
|
|
253
|
+
const supabase = createServer((request, response) => {
|
|
254
|
+
tokenRequests.push(request.url);
|
|
255
|
+
response.statusCode = 400;
|
|
256
|
+
response.setHeader("content-type", "application/json");
|
|
257
|
+
response.end(JSON.stringify({ error: "invalid_grant", error_description: "Invalid Refresh Token" }));
|
|
258
|
+
});
|
|
259
|
+
supabase.listen(0, "127.0.0.1");
|
|
260
|
+
await once(supabase, "listening");
|
|
261
|
+
t.after(() => new Promise((resolve) => supabase.close(resolve)));
|
|
262
|
+
const authService = createServiceFixture({
|
|
263
|
+
authProvider: { id: "supabase", supabaseUrl: `http://127.0.0.1:${supabase.address().port}`, supabasePublishableKey: "publishable-key" }
|
|
264
|
+
});
|
|
265
|
+
// The access cookie is gone once its token expires; the refresh cookie remains.
|
|
266
|
+
const request = { cookies: { sb_refresh_token: "R1" } };
|
|
267
|
+
|
|
268
|
+
assert.deepEqual(await authService.authenticateRequest(request, { refreshSession: false }), {
|
|
269
|
+
authenticated: false,
|
|
270
|
+
refreshRequired: true,
|
|
271
|
+
clearSession: false,
|
|
272
|
+
session: null,
|
|
273
|
+
transientFailure: false
|
|
274
|
+
});
|
|
275
|
+
assert.deepEqual(tokenRequests, []);
|
|
276
|
+
|
|
277
|
+
await authService.authenticateRequest(request);
|
|
278
|
+
assert.deepEqual(tokenRequests, ["/auth/v1/token?grant_type=refresh_token"]);
|
|
279
|
+
});
|
|
280
|
+
|
|
200
281
|
test("logout is local-only when no session cookies are present", async () => {
|
|
201
282
|
const authService = createServiceFixture();
|
|
202
283
|
|