@orangecheck/me-client 0.48.0 → 0.50.0
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 +30 -0
- package/dist/button-auto.js.map +1 -1
- package/dist/button-auto.mjs.map +1 -1
- package/dist/server.d.mts +44 -2
- package/dist/server.d.ts +44 -2
- package/dist/server.js +126 -3
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +126 -4
- package/dist/server.mjs.map +1 -1
- package/package.json +1 -1
package/dist/server.mjs
CHANGED
|
@@ -769,9 +769,9 @@ function spendOnce(jti, exp, store) {
|
|
|
769
769
|
});
|
|
770
770
|
}
|
|
771
771
|
var REVOKE_OPTIONS = /* @__PURE__ */ new Set(["issuer", "timeoutMs"]);
|
|
772
|
-
function issuerOrigin(issuer) {
|
|
773
|
-
if (typeof issuer !== "string") fail2(
|
|
774
|
-
const refuse = () => fail2(
|
|
772
|
+
function issuerOrigin(issuer, where = "revokeToken") {
|
|
773
|
+
if (typeof issuer !== "string") fail2(`${where}: issuer must be a URL`);
|
|
774
|
+
const refuse = () => fail2(`${where}: issuer must be one https origin (http only on this machine), such as https://ochk.io, got ${issuer}`);
|
|
775
775
|
if (/[?#@\\]/.test(issuer)) refuse();
|
|
776
776
|
let url;
|
|
777
777
|
try {
|
|
@@ -836,6 +836,128 @@ async function post2(url, tokens, timeoutMs) {
|
|
|
836
836
|
}
|
|
837
837
|
}
|
|
838
838
|
|
|
839
|
+
// src/native.ts
|
|
840
|
+
var DEFAULT_ISSUER3 = "https://ochk.io";
|
|
841
|
+
var DEFAULT_TIMEOUT_MS3 = 5e3;
|
|
842
|
+
var MAX_TIMEOUT_MS3 = 3e4;
|
|
843
|
+
var MAX_FIELD_LENGTH = 512;
|
|
844
|
+
var OPTIONS = /* @__PURE__ */ new Set([
|
|
845
|
+
"projectKey",
|
|
846
|
+
"issuer",
|
|
847
|
+
"meOrigin",
|
|
848
|
+
"revocation",
|
|
849
|
+
"requireBond",
|
|
850
|
+
"timeoutMs"
|
|
851
|
+
]);
|
|
852
|
+
var FIELDS = ["code", "codeVerifier", "redirectUri", "audience"];
|
|
853
|
+
function fail3(message) {
|
|
854
|
+
throw new TypeError(`@orangecheck/me-client: exchangeNativeCode: ${message}`);
|
|
855
|
+
}
|
|
856
|
+
async function exchangeNativeCode(input, options = {}) {
|
|
857
|
+
if (input === null || typeof input !== "object") fail3("input must be an object");
|
|
858
|
+
if ("projectKey" in input) {
|
|
859
|
+
fail3("projectKey is your own key: pass it in options, or set OC_PROJECT_KEY, never from the request");
|
|
860
|
+
}
|
|
861
|
+
const extra = Object.keys(input).filter((k) => !FIELDS.includes(k));
|
|
862
|
+
if (extra.length > 0) fail3(`unknown input ${extra.join(", ")}. It reads only ${FIELDS.join(", ")}.`);
|
|
863
|
+
for (const f of FIELDS) {
|
|
864
|
+
const v = input[f];
|
|
865
|
+
if (typeof v !== "string" || v.length === 0 || v.length > MAX_FIELD_LENGTH) {
|
|
866
|
+
fail3(`${f} must be a non-empty string of at most ${MAX_FIELD_LENGTH} characters`);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
if (input.audience === "*") fail3("audience must be your site's origin, not '*'");
|
|
870
|
+
if (options === null || typeof options !== "object") fail3("options must be an object");
|
|
871
|
+
const unknown = Object.keys(options).filter((k) => !OPTIONS.has(k));
|
|
872
|
+
if (unknown.length > 0) fail3(`unknown option ${unknown.join(", ")}. It reads only ${[...OPTIONS].join(", ")}.`);
|
|
873
|
+
const projectKey = options.projectKey ?? (typeof process !== "undefined" ? process.env?.OC_PROJECT_KEY?.trim() : void 0);
|
|
874
|
+
if (typeof projectKey !== "string" || projectKey.length === 0 || projectKey.length > MAX_FIELD_LENGTH) {
|
|
875
|
+
fail3("needs your project key: options.projectKey, or OC_PROJECT_KEY");
|
|
876
|
+
}
|
|
877
|
+
const issuer = issuerOrigin(options.issuer ?? DEFAULT_ISSUER3, "exchangeNativeCode");
|
|
878
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS3;
|
|
879
|
+
if (typeof timeoutMs !== "number" || !(timeoutMs > 0 && timeoutMs <= MAX_TIMEOUT_MS3)) {
|
|
880
|
+
fail3(`timeoutMs must be above 0 and at most ${MAX_TIMEOUT_MS3}`);
|
|
881
|
+
}
|
|
882
|
+
const answer = await post3(`${issuer}/api/auth/native/token`, input, projectKey, timeoutMs);
|
|
883
|
+
if (answer.status !== "token") return answer;
|
|
884
|
+
const token = answer.token;
|
|
885
|
+
const verified = await checkOcToken(token, { audience: input.audience, issuer });
|
|
886
|
+
if (verified.status === "unavailable") return { status: "unavailable", reason: "key_set_unavailable" };
|
|
887
|
+
if (verified.status === "invalid") return { status: "invalid", reason: verified.reason ?? "invalid_token" };
|
|
888
|
+
const where = options.meOrigin ? { meOrigin: options.meOrigin } : {};
|
|
889
|
+
const checked = await checkOcSession(
|
|
890
|
+
{ authorization: `Bearer ${token}` },
|
|
891
|
+
{
|
|
892
|
+
audience: input.audience,
|
|
893
|
+
issuer,
|
|
894
|
+
projectKey,
|
|
895
|
+
method: "POST",
|
|
896
|
+
...where,
|
|
897
|
+
...options.revocation !== void 0 ? { revocation: options.revocation } : {}
|
|
898
|
+
}
|
|
899
|
+
);
|
|
900
|
+
if (checked.status === "unavailable") {
|
|
901
|
+
return { status: "unavailable", reason: checked.reason ?? "revocations_unavailable" };
|
|
902
|
+
}
|
|
903
|
+
if (checked.status === "signed_out") return { status: "invalid", reason: checked.reason ?? "session_ended" };
|
|
904
|
+
const rule = await checkSignInRule(checked.session, {
|
|
905
|
+
projectKey,
|
|
906
|
+
...where,
|
|
907
|
+
...options.requireBond ? { requireBond: options.requireBond } : {}
|
|
908
|
+
});
|
|
909
|
+
if (!rule.ok) {
|
|
910
|
+
const b = rule.body;
|
|
911
|
+
if (b.reason === "bond_required" && b.need && b.back_url) {
|
|
912
|
+
return { status: "invalid", reason: "bond_required", need: b.need, have: b.have ?? null, back_url: b.back_url };
|
|
913
|
+
}
|
|
914
|
+
if (b.reason === "unknown_project_key") return { status: "invalid", reason: "unknown_project_key" };
|
|
915
|
+
return { status: "unavailable", reason: "bond_policy_unavailable" };
|
|
916
|
+
}
|
|
917
|
+
return { status: "valid", token, session: checked.session };
|
|
918
|
+
}
|
|
919
|
+
async function post3(url, input, projectKey, timeoutMs) {
|
|
920
|
+
const unavailable2 = { status: "unavailable", reason: "auth_host_unavailable" };
|
|
921
|
+
const controller = new AbortController();
|
|
922
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
923
|
+
try {
|
|
924
|
+
const res = await fetch(url, {
|
|
925
|
+
method: "POST",
|
|
926
|
+
// The endpoint never redirects: the code goes to the auth host and nowhere else.
|
|
927
|
+
redirect: "error",
|
|
928
|
+
headers: { "content-type": "application/json" },
|
|
929
|
+
body: JSON.stringify({
|
|
930
|
+
grant_type: "authorization_code",
|
|
931
|
+
code: input.code,
|
|
932
|
+
code_verifier: input.codeVerifier,
|
|
933
|
+
redirect_uri: input.redirectUri,
|
|
934
|
+
project: projectKey
|
|
935
|
+
}),
|
|
936
|
+
signal: controller.signal
|
|
937
|
+
});
|
|
938
|
+
let body = null;
|
|
939
|
+
try {
|
|
940
|
+
const json = await res.json();
|
|
941
|
+
if (json !== null && typeof json === "object") body = json;
|
|
942
|
+
} catch {
|
|
943
|
+
body = null;
|
|
944
|
+
}
|
|
945
|
+
if (res.status === 200) {
|
|
946
|
+
return body?.ok === true && typeof body.token === "string" && body.token.length > 0 ? { status: "token", token: body.token } : unavailable2;
|
|
947
|
+
}
|
|
948
|
+
if (res.status === 400) {
|
|
949
|
+
return { status: "invalid", reason: body?.reason === "invalid_grant" ? "invalid_grant" : "bad_request" };
|
|
950
|
+
}
|
|
951
|
+
if (res.status === 404) return { status: "invalid", reason: "not_offered" };
|
|
952
|
+
if (res.status === 429) return { status: "unavailable", reason: "rate_limited" };
|
|
953
|
+
return unavailable2;
|
|
954
|
+
} catch {
|
|
955
|
+
return unavailable2;
|
|
956
|
+
} finally {
|
|
957
|
+
clearTimeout(timer);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
|
|
839
961
|
// src/merged.ts
|
|
840
962
|
var MERGED_HANDLES_MAX = 16;
|
|
841
963
|
function mergedHandles(session) {
|
|
@@ -1239,6 +1361,6 @@ function ocAuthHono(options = {}) {
|
|
|
1239
1361
|
};
|
|
1240
1362
|
}
|
|
1241
1363
|
|
|
1242
|
-
export { BACK_ACCOUNT_URL, CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL, MemoryJtiStore, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, getOcSession, identity, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, revokeToken, spendOnce, verifyActivityAttestation, verifyOcToken, withOcAuth };
|
|
1364
|
+
export { BACK_ACCOUNT_URL, CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL, MemoryJtiStore, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, exchangeNativeCode, getOcSession, identity, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, revokeToken, spendOnce, verifyActivityAttestation, verifyOcToken, withOcAuth };
|
|
1243
1365
|
//# sourceMappingURL=server.mjs.map
|
|
1244
1366
|
//# sourceMappingURL=server.mjs.map
|