@orangecheck/me-client 0.39.0 → 0.40.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 CHANGED
@@ -255,7 +255,7 @@ app.get('/api/me', (c) => {
255
255
 
256
256
  ### Sessions ended everywhere
257
257
 
258
- 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.
258
+ 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`). Every token while the two variables are unset is checked by its signature alone and stays valid until it expires. A token bound to your site without `rh` is refused (`no_session_handle`, 0.40.0 or later), since sign out everywhere can't reach it; every token from the sign-in popup carries one, and `requireHandle: false` accepts one minted some other way. Needs 0.35.0 or later.
259
259
 
260
260
  ## Keep your own login: OC Connect
261
261
 
package/dist/server.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { SessionPayload, VerifyOcOptions, RevocationCheck, SessionRequestHeaders } from '@orangecheck/auth-core';
2
- export { BondClaim, SessionPayload, VerifyOcOptions, bondMeets, readBondClaim, verifyOcToken } from '@orangecheck/auth-core';
2
+ export { BondClaim, SessionPayload, VerifyOcOptions, bondMeets, readBondClaim } from '@orangecheck/auth-core';
3
3
  export { A as ActivityAttestation, a as ActivityAttestationBundle, b as ActivityVerifyCheck, c as ActivityVerifyResult, V as VerifyActivityOptions, i as identity, r as refreshEnvelopeJwks, v as verifyActivityAttestation } from './identity-DfxCvxuH.mjs';
4
4
  export { CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL } from './connect-consent.mjs';
5
5
 
@@ -39,7 +39,13 @@ declare function connectProvision(options: ConnectProvisionOptions): Promise<Con
39
39
  declare function connectClaimLink(options: ConnectOptions): Promise<ConnectClaimLinkResult>;
40
40
  declare function connectDelete(options: ConnectOptions): Promise<ConnectDeleteResult>;
41
41
 
42
- interface OcSessionOptions extends VerifyOcOptions {
42
+ type OcSessionRefusal = 'no_session_handle';
43
+ interface VerifyOcTokenOptions extends VerifyOcOptions {
44
+ requireHandle?: boolean;
45
+ }
46
+ declare function verifyOcToken(token: string, options?: VerifyOcTokenOptions): Promise<SessionPayload | null>;
47
+
48
+ interface OcSessionOptions extends VerifyOcTokenOptions {
43
49
  method?: string;
44
50
  projectKey?: string;
45
51
  meOrigin?: string;
@@ -52,6 +58,7 @@ type OcSessionCheck = {
52
58
  session: SessionPayload;
53
59
  } | {
54
60
  status: 'signed_out';
61
+ reason?: OcSessionRefusal;
55
62
  } | {
56
63
  status: 'unavailable';
57
64
  };
@@ -126,4 +133,4 @@ interface HonoContextLike {
126
133
  type HonoNextLike = () => Promise<void>;
127
134
  declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
128
135
 
129
- export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, connectClaimLink, connectDelete, connectProvision, getOcSession, ocAuthExpress, ocAuthHono, withOcAuth };
136
+ export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, connectClaimLink, connectDelete, connectProvision, getOcSession, ocAuthExpress, ocAuthHono, verifyOcToken, withOcAuth };
package/dist/server.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { SessionPayload, VerifyOcOptions, RevocationCheck, SessionRequestHeaders } from '@orangecheck/auth-core';
2
- export { BondClaim, SessionPayload, VerifyOcOptions, bondMeets, readBondClaim, verifyOcToken } from '@orangecheck/auth-core';
2
+ export { BondClaim, SessionPayload, VerifyOcOptions, bondMeets, readBondClaim } from '@orangecheck/auth-core';
3
3
  export { A as ActivityAttestation, a as ActivityAttestationBundle, b as ActivityVerifyCheck, c as ActivityVerifyResult, V as VerifyActivityOptions, i as identity, r as refreshEnvelopeJwks, v as verifyActivityAttestation } from './identity-DfxCvxuH.js';
4
4
  export { CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL } from './connect-consent.js';
5
5
 
@@ -39,7 +39,13 @@ declare function connectProvision(options: ConnectProvisionOptions): Promise<Con
39
39
  declare function connectClaimLink(options: ConnectOptions): Promise<ConnectClaimLinkResult>;
40
40
  declare function connectDelete(options: ConnectOptions): Promise<ConnectDeleteResult>;
41
41
 
42
- interface OcSessionOptions extends VerifyOcOptions {
42
+ type OcSessionRefusal = 'no_session_handle';
43
+ interface VerifyOcTokenOptions extends VerifyOcOptions {
44
+ requireHandle?: boolean;
45
+ }
46
+ declare function verifyOcToken(token: string, options?: VerifyOcTokenOptions): Promise<SessionPayload | null>;
47
+
48
+ interface OcSessionOptions extends VerifyOcTokenOptions {
43
49
  method?: string;
44
50
  projectKey?: string;
45
51
  meOrigin?: string;
@@ -52,6 +58,7 @@ type OcSessionCheck = {
52
58
  session: SessionPayload;
53
59
  } | {
54
60
  status: 'signed_out';
61
+ reason?: OcSessionRefusal;
55
62
  } | {
56
63
  status: 'unavailable';
57
64
  };
@@ -126,4 +133,4 @@ interface HonoContextLike {
126
133
  type HonoNextLike = () => Promise<void>;
127
134
  declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
128
135
 
129
- export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, connectClaimLink, connectDelete, connectProvision, getOcSession, ocAuthExpress, ocAuthHono, withOcAuth };
136
+ export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, connectClaimLink, connectDelete, connectProvision, getOcSession, ocAuthExpress, ocAuthHono, verifyOcToken, withOcAuth };
package/dist/server.js CHANGED
@@ -734,6 +734,39 @@ var CONNECT_CONSENT_LABEL = "Also create my orangecheck account";
734
734
  var CONNECT_CONSENT_BLURB = "It stays with orangecheck until you claim it at ochk.app. Then you can use it on other sites, or delete it.";
735
735
 
736
736
  // src/server.ts
737
+ function lacksHandle(session, requireHandle) {
738
+ if (requireHandle === false) return false;
739
+ const { aud, rh } = session;
740
+ if (aud === void 0) return false;
741
+ return typeof rh !== "string" || rh.length === 0;
742
+ }
743
+ function checkRequireHandle(requireHandle) {
744
+ if (requireHandle !== void 0 && typeof requireHandle !== "boolean") {
745
+ throw new TypeError("@orangecheck/me-client: requireHandle must be true or false");
746
+ }
747
+ }
748
+ var warnedHandle = false;
749
+ function warnNoHandle(session) {
750
+ if (warnedHandle) return;
751
+ warnedHandle = true;
752
+ console.warn(
753
+ `[@orangecheck/me-client] a sign-in token bound to ${JSON.stringify(session.aud)} carries no revocation handle (rh), so sign out everywhere could not end it, and it was refused (no_session_handle). Tokens from the ochk.app sign-in popup carry one. Pass requireHandle: false only to accept tokens minted some other way.`
754
+ );
755
+ }
756
+ async function verifyOcToken(token, options = {}) {
757
+ if (options === null || typeof options !== "object") {
758
+ throw new TypeError("@orangecheck/me-client: options must be an object");
759
+ }
760
+ const { requireHandle, ...core } = options;
761
+ checkRequireHandle(requireHandle);
762
+ const session = await authCore.verifyOcToken(token, core);
763
+ if (!session) return null;
764
+ if (lacksHandle(session, requireHandle)) {
765
+ warnNoHandle(session);
766
+ return null;
767
+ }
768
+ return session;
769
+ }
737
770
  var sessionChecks = /* @__PURE__ */ new Map();
738
771
  var warnedUnchecked = false;
739
772
  function revocationFor(options) {
@@ -762,6 +795,7 @@ function revocationFor(options) {
762
795
  function _resetSessionChecksForTests() {
763
796
  sessionChecks.clear();
764
797
  warnedUnchecked = false;
798
+ warnedHandle = false;
765
799
  }
766
800
  async function getOcSession(headers, options = {}) {
767
801
  const checked = await checkOcSession(headers, options);
@@ -775,6 +809,10 @@ async function checkOcSession(headers, options = {}) {
775
809
  if (options.audience === void 0) warnIfAudienceMissing(headers);
776
810
  return { status: "signed_out" };
777
811
  }
812
+ if (lacksHandle(session, options.requireHandle)) {
813
+ warnNoHandle(session);
814
+ return { status: "signed_out", reason: "no_session_handle" };
815
+ }
778
816
  const check = revocationFor(options);
779
817
  if (check) {
780
818
  const state = await check.status(session);
@@ -786,6 +824,7 @@ async function checkOcSession(headers, options = {}) {
786
824
  return { status: "signed_in", session };
787
825
  }
788
826
  var UNAVAILABLE = { status: 503, body: { ok: false, reason: "revocations_unavailable" } };
827
+ var refusedFor = (checked) => checked.status === "signed_out" ? checked.reason : void 0;
789
828
  var warnedAudience = false;
790
829
  function warnIfAudienceMissing(headers) {
791
830
  if (warnedAudience) return;
@@ -820,6 +859,7 @@ var KNOWN_OPTIONS = /* @__PURE__ */ new Set([
820
859
  "projectKey",
821
860
  "meOrigin",
822
861
  "revocation",
862
+ "requireHandle",
823
863
  "required",
824
864
  "requireBond"
825
865
  ]);
@@ -834,6 +874,7 @@ function checkOptions(options) {
834
874
  `@orangecheck/me-client: unknown option ${unknown.join(", ")}. It reads only ${[...KNOWN_OPTIONS].join(", ")}.`
835
875
  );
836
876
  }
877
+ checkRequireHandle(options.requireHandle);
837
878
  const bond = options.requireBond;
838
879
  if (bond === void 0) return;
839
880
  if (bond === null || typeof bond !== "object") {
@@ -916,10 +957,9 @@ async function bondNeed(options) {
916
957
  if (options.projectKey) return projectRequirement(options);
917
958
  return null;
918
959
  }
919
- async function refusal(session, options) {
920
- if (!session && options.required) {
921
- return { status: 401, body: { ok: false, reason: "unauthenticated" } };
922
- }
960
+ async function refusal(session, options, why) {
961
+ const unauthenticated = { status: 401, body: { ok: false, reason: why ?? "unauthenticated" } };
962
+ if (!session && options.required) return unauthenticated;
923
963
  if (!session && !options.requireBond) return null;
924
964
  const need = await bondNeed(options);
925
965
  if (need === UNKNOWN_PROJECT) {
@@ -928,9 +968,7 @@ async function refusal(session, options) {
928
968
  if (need === void 0) {
929
969
  return { status: 503, body: { ok: false, reason: "bond_policy_unavailable" } };
930
970
  }
931
- if (!session) {
932
- return need ? { status: 401, body: { ok: false, reason: "unauthenticated" } } : null;
933
- }
971
+ if (!session) return need ? unauthenticated : null;
934
972
  if (need && !authCore.bondMeets(session, need)) {
935
973
  const have = authCore.readBondClaim(session);
936
974
  return {
@@ -965,7 +1003,7 @@ function withOcAuth(handler, options = {}) {
965
1003
  const session = checked.status === "signed_in" ? checked.session : null;
966
1004
  const withSession = req;
967
1005
  withSession.ocSession = session;
968
- const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
1006
+ const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options, refusedFor(checked));
969
1007
  if (refused2) return res.status(refused2.status).json(refused2.body);
970
1008
  return handler(withSession, res);
971
1009
  };
@@ -985,7 +1023,7 @@ function ocAuthExpress(options = {}) {
985
1023
  );
986
1024
  const session = checked.status === "signed_in" ? checked.session : null;
987
1025
  req.ocSession = session;
988
- const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
1026
+ const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options, refusedFor(checked));
989
1027
  if (refused2) {
990
1028
  res.status(refused2.status).json(refused2.body);
991
1029
  return;
@@ -1007,7 +1045,7 @@ function ocAuthHono(options = {}) {
1007
1045
  });
1008
1046
  const session = checked.status === "signed_in" ? checked.session : null;
1009
1047
  c.set("ocSession", session);
1010
- const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
1048
+ const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options, refusedFor(checked));
1011
1049
  if (refused2) return c.json(refused2.body, refused2.status);
1012
1050
  await next();
1013
1051
  };
@@ -1021,10 +1059,6 @@ Object.defineProperty(exports, "readBondClaim", {
1021
1059
  enumerable: true,
1022
1060
  get: function () { return authCore.readBondClaim; }
1023
1061
  });
1024
- Object.defineProperty(exports, "verifyOcToken", {
1025
- enumerable: true,
1026
- get: function () { return authCore.verifyOcToken; }
1027
- });
1028
1062
  exports.BACK_ACCOUNT_URL = BACK_ACCOUNT_URL;
1029
1063
  exports.CONNECT_CONSENT_BLURB = CONNECT_CONSENT_BLURB;
1030
1064
  exports.CONNECT_CONSENT_LABEL = CONNECT_CONSENT_LABEL;
@@ -1039,6 +1073,7 @@ exports.ocAuthExpress = ocAuthExpress;
1039
1073
  exports.ocAuthHono = ocAuthHono;
1040
1074
  exports.refreshEnvelopeJwks = refreshEnvelopeJwks;
1041
1075
  exports.verifyActivityAttestation = verifyActivityAttestation;
1076
+ exports.verifyOcToken = verifyOcToken;
1042
1077
  exports.withOcAuth = withOcAuth;
1043
1078
  //# sourceMappingURL=server.js.map
1044
1079
  //# sourceMappingURL=server.js.map