@orangecheck/me-client 0.37.0 → 0.38.1
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/README.md +1 -1
- package/dist/server.d.mts +10 -1
- package/dist/server.d.ts +10 -1
- package/dist/server.js +48 -18
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +49 -20
- package/dist/server.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -249,7 +249,7 @@ app.get('/api/me', (c) => {
|
|
|
249
249
|
|
|
250
250
|
### Sessions ended everywhere
|
|
251
251
|
|
|
252
|
-
Set `OC_PROJECT_KEY` and `OC_SIGNING_SECRET` (from ochk.app/me/projects) and `getOcSession`, `withOcAuth`, `ocAuthExpress` and `ocAuthHono` refuse a session the person ended everywhere: sign out everywhere, a passkey removed, the account deleted. A sign-in token carries `rh`, its revocation handle for your origin; they look it up in me.ochk.io's list of ended handles for that origin, read once a minute per origin with a request signed by your secret, so a refusal lands within about two minutes. The handles name nobody, and another site's handles for the same person never match yours. If the list can't be read, or is more than ten minutes old by its own clock, reads go on and writes are refused until it can. A token without `rh`, and every token while the two variables are unset, is checked by its signature alone and stays valid until it expires. Needs 0.35.0 or later.
|
|
252
|
+
Set `OC_PROJECT_KEY` and `OC_SIGNING_SECRET` (from ochk.app/me/projects) and `getOcSession`, `withOcAuth`, `ocAuthExpress` and `ocAuthHono` refuse a session the person ended everywhere: sign out everywhere, a passkey removed, the account deleted. A sign-in token carries `rh`, its revocation handle for your origin; they look it up in me.ochk.io's list of ended handles for that origin, read once a minute per origin with a request signed by your secret, so a refusal lands within about two minutes. The handles name nobody, and another site's handles for the same person never match yours. If the list can't be read, or is more than ten minutes old by its own clock, reads go on and writes are refused until it can: the adapters answer such a write `503 { reason: 'revocations_unavailable' }` (0.38.0), not 401, so a client retries instead of signing the person out, and `checkOcSession(headers, options)` tells the same apart for your own code (`signed_in`, `signed_out` or `unavailable`). A token without `rh`, and every token while the two variables are unset, is checked by its signature alone and stays valid until it expires. Needs 0.35.0 or later.
|
|
253
253
|
|
|
254
254
|
## Browser popup signin
|
|
255
255
|
|
package/dist/server.d.mts
CHANGED
|
@@ -10,6 +10,15 @@ interface OcSessionOptions extends VerifyOcOptions {
|
|
|
10
10
|
}
|
|
11
11
|
declare function _resetSessionChecksForTests(): void;
|
|
12
12
|
declare function getOcSession(headers: SessionRequestHeaders | Headers, options?: OcSessionOptions): Promise<SessionPayload | null>;
|
|
13
|
+
type OcSessionCheck = {
|
|
14
|
+
status: 'signed_in';
|
|
15
|
+
session: SessionPayload;
|
|
16
|
+
} | {
|
|
17
|
+
status: 'signed_out';
|
|
18
|
+
} | {
|
|
19
|
+
status: 'unavailable';
|
|
20
|
+
};
|
|
21
|
+
declare function checkOcSession(headers: SessionRequestHeaders | Headers, options?: OcSessionOptions): Promise<OcSessionCheck>;
|
|
13
22
|
interface NextApiRequestLike {
|
|
14
23
|
method?: string;
|
|
15
24
|
headers: {
|
|
@@ -80,4 +89,4 @@ interface HonoContextLike {
|
|
|
80
89
|
type HonoNextLike = () => Promise<void>;
|
|
81
90
|
declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
|
|
82
91
|
|
|
83
|
-
export { BACK_ACCOUNT_URL, type OcHonoEnv, type OcSessionOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, getOcSession, ocAuthExpress, ocAuthHono, withOcAuth };
|
|
92
|
+
export { BACK_ACCOUNT_URL, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, getOcSession, ocAuthExpress, ocAuthHono, withOcAuth };
|
package/dist/server.d.ts
CHANGED
|
@@ -10,6 +10,15 @@ interface OcSessionOptions extends VerifyOcOptions {
|
|
|
10
10
|
}
|
|
11
11
|
declare function _resetSessionChecksForTests(): void;
|
|
12
12
|
declare function getOcSession(headers: SessionRequestHeaders | Headers, options?: OcSessionOptions): Promise<SessionPayload | null>;
|
|
13
|
+
type OcSessionCheck = {
|
|
14
|
+
status: 'signed_in';
|
|
15
|
+
session: SessionPayload;
|
|
16
|
+
} | {
|
|
17
|
+
status: 'signed_out';
|
|
18
|
+
} | {
|
|
19
|
+
status: 'unavailable';
|
|
20
|
+
};
|
|
21
|
+
declare function checkOcSession(headers: SessionRequestHeaders | Headers, options?: OcSessionOptions): Promise<OcSessionCheck>;
|
|
13
22
|
interface NextApiRequestLike {
|
|
14
23
|
method?: string;
|
|
15
24
|
headers: {
|
|
@@ -80,4 +89,4 @@ interface HonoContextLike {
|
|
|
80
89
|
type HonoNextLike = () => Promise<void>;
|
|
81
90
|
declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
|
|
82
91
|
|
|
83
|
-
export { BACK_ACCOUNT_URL, type OcHonoEnv, type OcSessionOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, getOcSession, ocAuthExpress, ocAuthHono, withOcAuth };
|
|
92
|
+
export { BACK_ACCOUNT_URL, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, getOcSession, ocAuthExpress, ocAuthHono, withOcAuth };
|
package/dist/server.js
CHANGED
|
@@ -600,15 +600,26 @@ function _resetSessionChecksForTests() {
|
|
|
600
600
|
warnedUnchecked = false;
|
|
601
601
|
}
|
|
602
602
|
async function getOcSession(headers, options = {}) {
|
|
603
|
+
const checked = await checkOcSession(headers, options);
|
|
604
|
+
return checked.status === "signed_in" ? checked.session : null;
|
|
605
|
+
}
|
|
606
|
+
async function checkOcSession(headers, options = {}) {
|
|
603
607
|
const session = await authCore.getOcSession(headers, options);
|
|
604
608
|
if (!session) {
|
|
605
609
|
if (options.audience === void 0) warnIfAudienceMissing(headers);
|
|
606
|
-
return
|
|
610
|
+
return { status: "signed_out" };
|
|
607
611
|
}
|
|
608
612
|
const check = revocationFor(options);
|
|
609
|
-
if (check
|
|
610
|
-
|
|
613
|
+
if (check) {
|
|
614
|
+
const state = await check.status(session);
|
|
615
|
+
if (state === "revoked") return { status: "signed_out" };
|
|
616
|
+
if (state === "unverified" && !authCore.SAFE_METHODS.has((options.method ?? "").toUpperCase())) {
|
|
617
|
+
return { status: "unavailable" };
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return { status: "signed_in", session };
|
|
611
621
|
}
|
|
622
|
+
var UNAVAILABLE = { status: 503, body: { ok: false, reason: "revocations_unavailable" } };
|
|
612
623
|
var warnedAudience = false;
|
|
613
624
|
function warnIfAudienceMissing(headers) {
|
|
614
625
|
if (warnedAudience) return;
|
|
@@ -635,12 +646,15 @@ function base64UrlToString(s) {
|
|
|
635
646
|
return Buffer.from(s, "base64url").toString("utf8");
|
|
636
647
|
}
|
|
637
648
|
var BACK_ACCOUNT_URL = "https://ochk.app/me/account#backing";
|
|
649
|
+
var stamp = () => ({ mono: performance.now(), wall: Date.now() });
|
|
650
|
+
var since = (s) => Math.max(performance.now() - s.mono, Date.now() - s.wall);
|
|
638
651
|
var unknownKeys = /* @__PURE__ */ new Map();
|
|
639
652
|
var UNKNOWN_PROJECT = /* @__PURE__ */ Symbol("unknown_project");
|
|
640
653
|
var POLICY_TTL_MS = 6e4;
|
|
641
654
|
var POLICY_RETRY_MS = 1e4;
|
|
642
655
|
var POLICY_MAX_STALE_MS = 6e5;
|
|
643
656
|
var policies = /* @__PURE__ */ new Map();
|
|
657
|
+
var policyReads = /* @__PURE__ */ new Map();
|
|
644
658
|
var isFloor = (n) => Number.isSafeInteger(n) && n >= 0;
|
|
645
659
|
function readRequirement(body) {
|
|
646
660
|
const b = body;
|
|
@@ -650,16 +664,27 @@ function readRequirement(body) {
|
|
|
650
664
|
if (!r || typeof r !== "object" || !isFloor(r.min_sats) || !isFloor(r.min_days)) return void 0;
|
|
651
665
|
return { minSats: r.min_sats, minDays: r.min_days };
|
|
652
666
|
}
|
|
667
|
+
function standing(slot) {
|
|
668
|
+
const hit = policies.get(slot);
|
|
669
|
+
return hit?.at && since(hit.at) < POLICY_MAX_STALE_MS ? hit.need : void 0;
|
|
670
|
+
}
|
|
653
671
|
async function projectRequirement(options) {
|
|
654
672
|
const base = (options.meOrigin ?? "https://me.ochk.io").replace(/\/$/, "");
|
|
655
673
|
const key = options.projectKey;
|
|
656
674
|
const slot = `${base} ${key}`;
|
|
675
|
+
const unknownAt = unknownKeys.get(slot);
|
|
676
|
+
if (unknownAt && since(unknownAt) < POLICY_TTL_MS) return UNKNOWN_PROJECT;
|
|
657
677
|
const hit = policies.get(slot);
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
if (
|
|
662
|
-
|
|
678
|
+
if (hit?.at && since(hit.at) < POLICY_TTL_MS) return hit.need;
|
|
679
|
+
if (hit?.failedAt && since(hit.failedAt) < POLICY_RETRY_MS) return standing(slot);
|
|
680
|
+
let read = policyReads.get(slot);
|
|
681
|
+
if (!read) {
|
|
682
|
+
read = readProjectRule(base, key, slot).finally(() => policyReads.delete(slot));
|
|
683
|
+
policyReads.set(slot, read);
|
|
684
|
+
}
|
|
685
|
+
return read;
|
|
686
|
+
}
|
|
687
|
+
async function readProjectRule(base, key, slot) {
|
|
663
688
|
try {
|
|
664
689
|
const r = await fetch(`${base}/api/integrator/${encodeURIComponent(key)}/sign-in`, {
|
|
665
690
|
signal: AbortSignal.timeout(4e3)
|
|
@@ -670,18 +695,19 @@ async function projectRequirement(options) {
|
|
|
670
695
|
`[@orangecheck/me-client] projectKey "${key}" is not a project. Copy yours from https://ochk.app/me/projects.`
|
|
671
696
|
);
|
|
672
697
|
}
|
|
673
|
-
unknownKeys.set(slot,
|
|
698
|
+
unknownKeys.set(slot, stamp());
|
|
674
699
|
return UNKNOWN_PROJECT;
|
|
675
700
|
}
|
|
676
701
|
unknownKeys.delete(slot);
|
|
677
702
|
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
|
678
703
|
const need = readRequirement(await r.json());
|
|
679
704
|
if (need === void 0) throw new Error("unexpected body");
|
|
680
|
-
policies.set(slot, { at:
|
|
705
|
+
policies.set(slot, { at: stamp(), need });
|
|
681
706
|
return need;
|
|
682
707
|
} catch {
|
|
683
|
-
|
|
684
|
-
|
|
708
|
+
const hit = policies.get(slot);
|
|
709
|
+
policies.set(slot, { at: hit?.at ?? null, need: hit?.need ?? null, failedAt: stamp() });
|
|
710
|
+
return standing(slot);
|
|
685
711
|
}
|
|
686
712
|
}
|
|
687
713
|
async function bondNeed(options) {
|
|
@@ -727,16 +753,17 @@ function withOcAuth(handler, options = {}) {
|
|
|
727
753
|
return async (req, res) => {
|
|
728
754
|
res.setHeader("Cache-Control", "no-store, private");
|
|
729
755
|
res.setHeader("Vary", "Cookie, Authorization");
|
|
730
|
-
const
|
|
756
|
+
const checked = await checkOcSession(
|
|
731
757
|
{
|
|
732
758
|
cookie: typeof req.headers.cookie === "string" ? req.headers.cookie : null,
|
|
733
759
|
authorization: typeof req.headers.authorization === "string" ? req.headers.authorization : null
|
|
734
760
|
},
|
|
735
761
|
{ ...options, method: req.method }
|
|
736
762
|
);
|
|
763
|
+
const session = checked.status === "signed_in" ? checked.session : null;
|
|
737
764
|
const withSession = req;
|
|
738
765
|
withSession.ocSession = session;
|
|
739
|
-
const refused = await refusal(session, options);
|
|
766
|
+
const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
|
|
740
767
|
if (refused) return res.status(refused.status).json(refused.body);
|
|
741
768
|
return handler(withSession, res);
|
|
742
769
|
};
|
|
@@ -746,15 +773,16 @@ function ocAuthExpress(options = {}) {
|
|
|
746
773
|
try {
|
|
747
774
|
res.setHeader("Cache-Control", "no-store, private");
|
|
748
775
|
res.setHeader("Vary", "Cookie, Authorization");
|
|
749
|
-
const
|
|
776
|
+
const checked = await checkOcSession(
|
|
750
777
|
{
|
|
751
778
|
cookie: typeof req.headers.cookie === "string" ? req.headers.cookie : null,
|
|
752
779
|
authorization: typeof req.headers.authorization === "string" ? req.headers.authorization : null
|
|
753
780
|
},
|
|
754
781
|
{ ...options, method: req.method }
|
|
755
782
|
);
|
|
783
|
+
const session = checked.status === "signed_in" ? checked.session : null;
|
|
756
784
|
req.ocSession = session;
|
|
757
|
-
const refused = await refusal(session, options);
|
|
785
|
+
const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
|
|
758
786
|
if (refused) {
|
|
759
787
|
res.status(refused.status).json(refused.body);
|
|
760
788
|
return;
|
|
@@ -769,12 +797,13 @@ function ocAuthHono(options = {}) {
|
|
|
769
797
|
return async (c, next) => {
|
|
770
798
|
c.header("Cache-Control", "no-store, private");
|
|
771
799
|
c.header("Vary", "Cookie, Authorization");
|
|
772
|
-
const
|
|
800
|
+
const checked = await checkOcSession(c.req.raw.headers, {
|
|
773
801
|
...options,
|
|
774
802
|
method: c.req.raw.method
|
|
775
803
|
});
|
|
804
|
+
const session = checked.status === "signed_in" ? checked.session : null;
|
|
776
805
|
c.set("ocSession", session);
|
|
777
|
-
const refused = await refusal(session, options);
|
|
806
|
+
const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
|
|
778
807
|
if (refused) return c.json(refused.body, refused.status);
|
|
779
808
|
await next();
|
|
780
809
|
};
|
|
@@ -794,6 +823,7 @@ Object.defineProperty(exports, "verifyOcToken", {
|
|
|
794
823
|
});
|
|
795
824
|
exports.BACK_ACCOUNT_URL = BACK_ACCOUNT_URL;
|
|
796
825
|
exports._resetSessionChecksForTests = _resetSessionChecksForTests;
|
|
826
|
+
exports.checkOcSession = checkOcSession;
|
|
797
827
|
exports.getOcSession = getOcSession;
|
|
798
828
|
exports.identity = identity;
|
|
799
829
|
exports.ocAuthExpress = ocAuthExpress;
|