@iqauth/sdk 2.2.0 → 2.3.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 +24 -0
- package/dist/browser-session.d.mts +1 -2
- package/dist/browser-session.d.ts +1 -2
- package/dist/browser-session.js +89 -68
- package/dist/browser-session.mjs +2 -1
- package/dist/browser.d.mts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +13 -2
- package/dist/browser.mjs +2 -2
- package/dist/{chunk-D72UL5HL.mjs → chunk-EKTNEZIH.mjs} +4 -4
- package/dist/{chunk-M4J6BPK7.mjs → chunk-KGEPDXHU.mjs} +10 -1
- package/dist/{chunk-QZB745C2.mjs → chunk-RACIPVLD.mjs} +13 -2
- package/dist/chunk-UNYDG2L4.mjs +209 -0
- package/dist/{chunk-MDUHPQMM.mjs → chunk-W3F4JYGP.mjs} +8 -180
- package/dist/{chunk-QEJB7WEQ.mjs → chunk-WQWBJSSS.mjs} +1 -1
- package/dist/cli/index.mjs +1 -1
- package/dist/{client-DXbHb2ul.d.ts → client-DTX4hNdS.d.ts} +16 -21
- package/dist/{client-Dv4v92Mj.d.mts → client-vdh2a9fJ.d.mts} +16 -21
- package/dist/{doctor-XCI77BQS.mjs → doctor-A5E7LSFW.mjs} +1 -1
- package/dist/{express-BZmF1llh.d.mts → express-A0-dWEMy.d.mts} +1 -1
- package/dist/{express-B4o3P8vK.d.ts → express-Bo_pJKHN.d.ts} +1 -1
- package/dist/express.d.mts +75 -5
- package/dist/express.d.ts +75 -5
- package/dist/express.js +300 -70
- package/dist/express.mjs +208 -7
- package/dist/fastify.js +101 -70
- package/dist/fastify.mjs +8 -6
- package/dist/hono.js +100 -70
- package/dist/hono.mjs +7 -6
- package/dist/index.d.mts +2 -3
- package/dist/index.d.ts +2 -3
- package/dist/index.js +90 -69
- package/dist/index.mjs +15 -13
- package/dist/mobile.d.mts +1 -2
- package/dist/mobile.d.ts +1 -2
- package/dist/mobile.js +89 -68
- package/dist/mobile.mjs +2 -1
- package/dist/next.d.mts +9 -0
- package/dist/next.d.ts +9 -0
- package/dist/next.js +99 -1616
- package/dist/next.mjs +9 -9
- package/dist/react.d.mts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +13 -2
- package/dist/react.mjs +2 -2
- package/dist/server/handlers.d.mts +2 -0
- package/dist/server/handlers.d.ts +2 -0
- package/dist/server/handlers.js +10 -1
- package/dist/server/handlers.mjs +2 -2
- package/dist/server.d.mts +2 -3
- package/dist/server.d.ts +2 -3
- package/dist/server.js +99 -69
- package/dist/server.mjs +7 -6
- package/dist/service.d.mts +1 -2
- package/dist/service.d.ts +1 -2
- package/dist/service.js +89 -68
- package/dist/service.mjs +2 -1
- package/dist/{signIn-D_kP3v-c.d.mts → signIn-Cd0P4y9d.d.mts} +8 -0
- package/dist/{signIn-BVDTIA_t.d.ts → signIn-DKakyzeu.d.ts} +8 -0
- package/package.json +3 -2
package/dist/mobile.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { I as IQAuthClient } from './client-
|
|
1
|
+
import { I as IQAuthClient } from './client-vdh2a9fJ.mjs';
|
|
2
2
|
import { b as IQAuthTokenClientConfig } from './types-Cxl3bQHt.mjs';
|
|
3
3
|
export { E as ErrorCodes, I as IQAuthError } from './errors-CDdl24MP.mjs';
|
|
4
|
-
import 'jsonwebtoken';
|
|
5
4
|
|
|
6
5
|
declare class MobileIQAuthClient extends IQAuthClient {
|
|
7
6
|
constructor(config: IQAuthTokenClientConfig);
|
package/dist/mobile.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { I as IQAuthClient } from './client-
|
|
1
|
+
import { I as IQAuthClient } from './client-DTX4hNdS.js';
|
|
2
2
|
import { b as IQAuthTokenClientConfig } from './types-Cxl3bQHt.js';
|
|
3
3
|
export { E as ErrorCodes, I as IQAuthError } from './errors-CDdl24MP.js';
|
|
4
|
-
import 'jsonwebtoken';
|
|
5
4
|
|
|
6
5
|
declare class MobileIQAuthClient extends IQAuthClient {
|
|
7
6
|
constructor(config: IQAuthTokenClientConfig);
|
package/dist/mobile.js
CHANGED
|
@@ -446,8 +446,7 @@ function parseMfaResponse(data, browserSessionMode) {
|
|
|
446
446
|
}
|
|
447
447
|
|
|
448
448
|
// src/modules/tokens.ts
|
|
449
|
-
var
|
|
450
|
-
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
|
449
|
+
var import_jose = require("jose");
|
|
451
450
|
var JWKS_CACHE_TTL_MS = 60 * 60 * 1e3;
|
|
452
451
|
var DEFAULT_TOKEN_ISSUER = [
|
|
453
452
|
"https://auth.dispositioniq.com",
|
|
@@ -460,6 +459,24 @@ var DEFAULT_TOKEN_AUDIENCE = [
|
|
|
460
459
|
"iqvalidate"
|
|
461
460
|
];
|
|
462
461
|
var DEFAULT_CLOCK_TOLERANCE_SECONDS = 30;
|
|
462
|
+
function decodeProtectedHeader(token) {
|
|
463
|
+
const parts = token.split(".");
|
|
464
|
+
if (parts.length < 2) return null;
|
|
465
|
+
try {
|
|
466
|
+
const padded = parts[0] + "=".repeat((4 - parts[0].length % 4) % 4);
|
|
467
|
+
const b64 = padded.replace(/-/g, "+").replace(/_/g, "/");
|
|
468
|
+
let json;
|
|
469
|
+
if (typeof atob === "function") {
|
|
470
|
+
json = atob(b64);
|
|
471
|
+
} else {
|
|
472
|
+
const { Buffer: Buffer2 } = require("buffer");
|
|
473
|
+
json = Buffer2.from(b64, "base64").toString("utf8");
|
|
474
|
+
}
|
|
475
|
+
return JSON.parse(json);
|
|
476
|
+
} catch {
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
463
480
|
var TokensModule = class {
|
|
464
481
|
constructor(baseUrl, options = {}) {
|
|
465
482
|
this.jwksCache = null;
|
|
@@ -470,49 +487,49 @@ var TokensModule = class {
|
|
|
470
487
|
this.defaultClockTolerance = options.clockTolerance ?? DEFAULT_CLOCK_TOLERANCE_SECONDS;
|
|
471
488
|
}
|
|
472
489
|
/**
|
|
473
|
-
* Verify a JWT access token using RS256 via JWKS from
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
* clock tolerance default to client config but can be overridden per call.
|
|
490
|
+
* Verify a JWT access token using RS256/ES256 via JWKS from
|
|
491
|
+
* `/.well-known/jwks.json`. Backed by `jose` (Web Crypto) so it runs on
|
|
492
|
+
* Node, browser, and edge runtimes alike — no `node:crypto` dependency.
|
|
493
|
+
* Caches JWKS for 1 hour and refetches once on unknown `kid`.
|
|
478
494
|
*/
|
|
479
495
|
async verify(token, options = {}) {
|
|
480
|
-
const
|
|
481
|
-
if (!
|
|
496
|
+
const header = decodeProtectedHeader(token);
|
|
497
|
+
if (!header) {
|
|
482
498
|
throw new IQAuthError("TOKEN_INVALID", "Unable to decode token");
|
|
483
499
|
}
|
|
484
|
-
const kid =
|
|
500
|
+
const kid = header.kid;
|
|
485
501
|
if (!kid) {
|
|
486
502
|
throw new IQAuthError("TOKEN_INVALID", "Token missing kid header");
|
|
487
503
|
}
|
|
488
|
-
let
|
|
489
|
-
if (!
|
|
490
|
-
|
|
491
|
-
|
|
504
|
+
let cache = await this.ensureCache();
|
|
505
|
+
if (!cache.byKid.has(kid)) {
|
|
506
|
+
this.jwksCache = null;
|
|
507
|
+
cache = await this.ensureCache();
|
|
492
508
|
}
|
|
493
|
-
if (!
|
|
509
|
+
if (!cache.byKid.has(kid)) {
|
|
494
510
|
throw new IQAuthError("TOKEN_INVALID", `Unknown key ID: ${kid}`);
|
|
495
511
|
}
|
|
496
512
|
const issuer = options.issuer ?? this.defaultIssuer;
|
|
497
513
|
const audience = options.audience ?? this.defaultAudience;
|
|
498
514
|
const clockTolerance = options.clockTolerance ?? this.defaultClockTolerance;
|
|
499
|
-
const algorithms = options.algorithms ?? ["RS256"];
|
|
515
|
+
const algorithms = options.algorithms ?? ["RS256", "ES256"];
|
|
516
|
+
const verifyOptions = {
|
|
517
|
+
algorithms,
|
|
518
|
+
clockTolerance,
|
|
519
|
+
issuer,
|
|
520
|
+
audience
|
|
521
|
+
};
|
|
500
522
|
try {
|
|
501
|
-
const
|
|
502
|
-
|
|
503
|
-
clockTolerance,
|
|
504
|
-
// The jsonwebtoken types insist on tuple types for arrays; runtime
|
|
505
|
-
// accepts plain string[] so we cast to satisfy the compiler.
|
|
506
|
-
issuer,
|
|
507
|
-
audience
|
|
508
|
-
};
|
|
509
|
-
const verified = import_jsonwebtoken.default.verify(token, publicKey, verifyOptions);
|
|
510
|
-
return verified;
|
|
523
|
+
const { payload } = await (0, import_jose.jwtVerify)(token, cache.verifier, verifyOptions);
|
|
524
|
+
return payload;
|
|
511
525
|
} catch (err) {
|
|
526
|
+
if (err instanceof import_jose.errors.JWTExpired) {
|
|
527
|
+
throw new IQAuthError("TOKEN_EXPIRED", "Token has expired");
|
|
528
|
+
}
|
|
529
|
+
if (err instanceof import_jose.errors.JOSEError) {
|
|
530
|
+
throw new IQAuthError("TOKEN_INVALID", err.message);
|
|
531
|
+
}
|
|
512
532
|
if (err instanceof Error) {
|
|
513
|
-
if (err.name === "TokenExpiredError") {
|
|
514
|
-
throw new IQAuthError("TOKEN_EXPIRED", "Token has expired");
|
|
515
|
-
}
|
|
516
533
|
throw new IQAuthError("TOKEN_INVALID", err.message);
|
|
517
534
|
}
|
|
518
535
|
throw new IQAuthError("TOKEN_INVALID", "Token verification failed");
|
|
@@ -520,29 +537,40 @@ var TokensModule = class {
|
|
|
520
537
|
}
|
|
521
538
|
/**
|
|
522
539
|
* Decode a JWT without verification. Returns null if malformed.
|
|
523
|
-
*
|
|
524
|
-
* @remarks Local decode only — no network call
|
|
525
540
|
*/
|
|
526
541
|
decode(token) {
|
|
527
|
-
|
|
528
|
-
|
|
542
|
+
try {
|
|
543
|
+
const parts = token.split(".");
|
|
544
|
+
if (parts.length < 2) return null;
|
|
545
|
+
const payload = parts[1];
|
|
546
|
+
const padded = payload + "=".repeat((4 - payload.length % 4) % 4);
|
|
547
|
+
const b64 = padded.replace(/-/g, "+").replace(/_/g, "/");
|
|
548
|
+
let json;
|
|
549
|
+
if (typeof atob === "function") {
|
|
550
|
+
json = atob(b64);
|
|
551
|
+
} else {
|
|
552
|
+
const { Buffer: Buffer2 } = require("buffer");
|
|
553
|
+
json = Buffer2.from(b64, "base64").toString("utf8");
|
|
554
|
+
}
|
|
555
|
+
try {
|
|
556
|
+
json = decodeURIComponent(escape(json));
|
|
557
|
+
} catch {
|
|
558
|
+
}
|
|
559
|
+
const claims = JSON.parse(json);
|
|
560
|
+
if (!claims || typeof claims !== "object") return null;
|
|
561
|
+
return claims;
|
|
562
|
+
} catch {
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
529
565
|
}
|
|
530
|
-
/**
|
|
531
|
-
* Check if a token is expired based on the `exp` claim.
|
|
532
|
-
*
|
|
533
|
-
* @remarks Local check only — no network call
|
|
534
|
-
*/
|
|
566
|
+
/** Check if a token is expired based on the `exp` claim. */
|
|
535
567
|
isExpired(token) {
|
|
536
568
|
const claims = this.decode(token);
|
|
537
569
|
if (!claims?.exp) return true;
|
|
538
570
|
const now = Math.floor(Date.now() / 1e3);
|
|
539
571
|
return claims.exp <= now;
|
|
540
572
|
}
|
|
541
|
-
/**
|
|
542
|
-
* Get the claims from a token without verification.
|
|
543
|
-
*
|
|
544
|
-
* @remarks Local decode only — no network call
|
|
545
|
-
*/
|
|
573
|
+
/** Get the claims from a token without verification. */
|
|
546
574
|
getClaims(token) {
|
|
547
575
|
const claims = this.decode(token);
|
|
548
576
|
if (!claims) {
|
|
@@ -550,11 +578,15 @@ var TokensModule = class {
|
|
|
550
578
|
}
|
|
551
579
|
return claims;
|
|
552
580
|
}
|
|
553
|
-
async
|
|
554
|
-
if (
|
|
555
|
-
|
|
581
|
+
async ensureCache() {
|
|
582
|
+
if (this.jwksCache && Date.now() - this.jwksCache.fetchedAt <= JWKS_CACHE_TTL_MS) {
|
|
583
|
+
return this.jwksCache;
|
|
584
|
+
}
|
|
585
|
+
await this.refreshJwks();
|
|
586
|
+
if (!this.jwksCache) {
|
|
587
|
+
throw new IQAuthError("INTERNAL_ERROR", "JWKS cache unavailable after refresh");
|
|
556
588
|
}
|
|
557
|
-
return this.jwksCache
|
|
589
|
+
return this.jwksCache;
|
|
558
590
|
}
|
|
559
591
|
async refreshJwks() {
|
|
560
592
|
if (this.inFlightRefresh) {
|
|
@@ -581,35 +613,24 @@ var TokensModule = class {
|
|
|
581
613
|
"Malformed JWKS response: expected { keys: [...] }"
|
|
582
614
|
);
|
|
583
615
|
}
|
|
584
|
-
const
|
|
616
|
+
const byKid = /* @__PURE__ */ new Set();
|
|
585
617
|
for (const key of jwks.keys) {
|
|
586
|
-
if (!key || typeof key.kid !== "string" || typeof key.n !== "string" || typeof key.e !== "string") {
|
|
618
|
+
if (!key || typeof key.kid !== "string" || typeof key.n !== "string" && typeof key.x !== "string" || key.kty === "RSA" && (typeof key.n !== "string" || typeof key.e !== "string")) {
|
|
587
619
|
throw new IQAuthError(
|
|
588
620
|
"INTERNAL_ERROR",
|
|
589
621
|
"Malformed JWKS response: key missing required fields"
|
|
590
622
|
);
|
|
591
623
|
}
|
|
592
|
-
|
|
593
|
-
keys.set(key.kid, pem);
|
|
624
|
+
byKid.add(key.kid);
|
|
594
625
|
}
|
|
595
|
-
|
|
626
|
+
const verifier = (0, import_jose.createLocalJWKSet)({ keys: jwks.keys });
|
|
627
|
+
this.jwksCache = { raw: jwks.keys, byKid, verifier, fetchedAt: Date.now() };
|
|
596
628
|
} finally {
|
|
597
629
|
this.inFlightRefresh = null;
|
|
598
630
|
}
|
|
599
631
|
})();
|
|
600
632
|
return this.inFlightRefresh;
|
|
601
633
|
}
|
|
602
|
-
jwkToPem(jwk) {
|
|
603
|
-
const keyObject = import_crypto.default.createPublicKey({
|
|
604
|
-
key: {
|
|
605
|
-
kty: jwk.kty,
|
|
606
|
-
n: jwk.n,
|
|
607
|
-
e: jwk.e
|
|
608
|
-
},
|
|
609
|
-
format: "jwk"
|
|
610
|
-
});
|
|
611
|
-
return keyObject.export({ type: "spki", format: "pem" });
|
|
612
|
-
}
|
|
613
634
|
/** @internal Exposed for testing — clears JWKS cache */
|
|
614
635
|
clearCache() {
|
|
615
636
|
this.jwksCache = null;
|
|
@@ -817,7 +838,7 @@ var PermissionsModule = class {
|
|
|
817
838
|
};
|
|
818
839
|
|
|
819
840
|
// src/modules/oidc.ts
|
|
820
|
-
var
|
|
841
|
+
var import_crypto = __toESM(require("crypto"));
|
|
821
842
|
var InMemoryOidcStateStore = class {
|
|
822
843
|
constructor() {
|
|
823
844
|
this.map = /* @__PURE__ */ new Map();
|
|
@@ -898,12 +919,12 @@ var OidcModule = class {
|
|
|
898
919
|
* ready to redirect the user to.
|
|
899
920
|
*/
|
|
900
921
|
async createAuthRequest(params) {
|
|
901
|
-
const codeVerifier = base64UrlEncode(
|
|
922
|
+
const codeVerifier = base64UrlEncode(import_crypto.default.randomBytes(32));
|
|
902
923
|
const codeChallenge = base64UrlEncode(
|
|
903
|
-
|
|
924
|
+
import_crypto.default.createHash("sha256").update(codeVerifier).digest()
|
|
904
925
|
);
|
|
905
|
-
const state = base64UrlEncode(
|
|
906
|
-
const nonce = base64UrlEncode(
|
|
926
|
+
const state = base64UrlEncode(import_crypto.default.randomBytes(16));
|
|
927
|
+
const nonce = base64UrlEncode(import_crypto.default.randomBytes(16));
|
|
907
928
|
await this.stateStore.set(state, {
|
|
908
929
|
codeVerifier,
|
|
909
930
|
state,
|
package/dist/mobile.mjs
CHANGED
package/dist/next.d.mts
CHANGED
|
@@ -25,6 +25,15 @@ declare function handler(options: IQAuthNextOptions): (req: Request) => Promise<
|
|
|
25
25
|
/**
|
|
26
26
|
* Edge-compatible Next.js middleware factory. Returns a function suitable
|
|
27
27
|
* for `export const middleware = createMiddleware({ ... })`.
|
|
28
|
+
*
|
|
29
|
+
* Behavior: reads a Bearer token from the `Authorization` header or the
|
|
30
|
+
* access cookie (`iqauth_at` by default), verifies its signature against
|
|
31
|
+
* the issuer's JWKS using `jose` (Web Crypto, edge-safe), and on failure
|
|
32
|
+
* returns a 401 JSON response. The middleware does NOT transparently
|
|
33
|
+
* refresh expired access tokens — refresh happens in the browser SDK
|
|
34
|
+
* (single-flight) or by calling `POST /api/iqauth/refresh`. Pair this
|
|
35
|
+
* middleware with the helper routes mounted by `attachHelpers` for the
|
|
36
|
+
* full session lifecycle.
|
|
28
37
|
*/
|
|
29
38
|
declare function createMiddleware(options: IQAuthNextOptions): (req: Request) => Promise<Response | undefined>;
|
|
30
39
|
/**
|
package/dist/next.d.ts
CHANGED
|
@@ -25,6 +25,15 @@ declare function handler(options: IQAuthNextOptions): (req: Request) => Promise<
|
|
|
25
25
|
/**
|
|
26
26
|
* Edge-compatible Next.js middleware factory. Returns a function suitable
|
|
27
27
|
* for `export const middleware = createMiddleware({ ... })`.
|
|
28
|
+
*
|
|
29
|
+
* Behavior: reads a Bearer token from the `Authorization` header or the
|
|
30
|
+
* access cookie (`iqauth_at` by default), verifies its signature against
|
|
31
|
+
* the issuer's JWKS using `jose` (Web Crypto, edge-safe), and on failure
|
|
32
|
+
* returns a 401 JSON response. The middleware does NOT transparently
|
|
33
|
+
* refresh expired access tokens — refresh happens in the browser SDK
|
|
34
|
+
* (single-flight) or by calling `POST /api/iqauth/refresh`. Pair this
|
|
35
|
+
* middleware with the helper routes mounted by `attachHelpers` for the
|
|
36
|
+
* full session lifecycle.
|
|
28
37
|
*/
|
|
29
38
|
declare function createMiddleware(options: IQAuthNextOptions): (req: Request) => Promise<Response | undefined>;
|
|
30
39
|
/**
|