@bigso/auth-sdk 0.5.2 → 0.5.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.
@@ -98,6 +98,7 @@ var BigsoAuth = class extends EventEmitter {
98
98
  const nonce = generateRandomId();
99
99
  const verifier = generateVerifier();
100
100
  const requestId = this.requestId;
101
+ const codeChallenge = await sha256Base64Url(verifier);
101
102
  sessionStorage.setItem("sso_ctx", JSON.stringify({ state, nonce, verifier, requestId }));
102
103
  this.createUI();
103
104
  return new Promise((resolve, reject) => {
@@ -130,11 +131,10 @@ var BigsoAuth = class extends EventEmitter {
130
131
  this.closeUI();
131
132
  cleanup();
132
133
  this.emit("fallback");
133
- window.location.href = this.buildFallbackUrl();
134
+ window.location.href = this.buildFallbackUrl(codeChallenge, state);
134
135
  reject(new Error("Timeout"));
135
136
  }
136
137
  }, this.options.timeout);
137
- const codeChallenge = await sha256Base64Url(verifier);
138
138
  const initPayload = {
139
139
  state,
140
140
  nonce,
@@ -207,7 +207,7 @@ var BigsoAuth = class extends EventEmitter {
207
207
  cleanup();
208
208
  if (errorPayload.code === "version_mismatch") {
209
209
  this.emit("error", errorPayload);
210
- window.location.href = this.buildFallbackUrl();
210
+ window.location.href = this.buildFallbackUrl(codeChallenge, state);
211
211
  reject(new Error(`Version mismatch: expected ${errorPayload.expected_version}`));
212
212
  } else {
213
213
  this.emit("error", errorPayload);
@@ -342,12 +342,13 @@ var BigsoAuth = class extends EventEmitter {
342
342
  `;
343
343
  }
344
344
  // ─── Helpers ──────────────────────────────────────────────────────
345
- buildFallbackUrl() {
345
+ buildFallbackUrl(codeChallenge, state) {
346
346
  const url = new URL(this.options.ssoOrigin);
347
347
  url.searchParams.set("app_id", this.options.clientId);
348
348
  url.searchParams.set("redirect_uri", this.options.redirectUri || window.location.origin);
349
349
  url.searchParams.set("response_type", "code");
350
- url.searchParams.set("state", generateRandomId());
350
+ url.searchParams.set("state", state);
351
+ url.searchParams.set("code_challenge", codeChallenge);
351
352
  url.searchParams.set("code_challenge_method", "S256");
352
353
  url.searchParams.set("client_id", this.options.clientId);
353
354
  return url.toString();
@@ -1,4 +1,4 @@
1
- import { B as BigsoAuthOptions, a as BigsoAuthResult } from '../types-D5BaCbus.cjs';
1
+ import { B as BigsoAuthOptions, a as BigsoAuthResult } from '../types-K3V5MV8v.cjs';
2
2
 
3
3
  declare class EventEmitter {
4
4
  private events;
@@ -1,4 +1,4 @@
1
- import { B as BigsoAuthOptions, a as BigsoAuthResult } from '../types-D5BaCbus.js';
1
+ import { B as BigsoAuthOptions, a as BigsoAuthResult } from '../types-K3V5MV8v.js';
2
2
 
3
3
  declare class EventEmitter {
4
4
  private events;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  verifySignedPayload
3
- } from "../chunk-5ECHA2VH.js";
3
+ } from "../chunk-PB3GVAEJ.js";
4
4
 
5
5
  // src/utils/crypto.ts
6
6
  async function sha256Base64Url(input) {
@@ -66,6 +66,7 @@ var BigsoAuth = class extends EventEmitter {
66
66
  const nonce = generateRandomId();
67
67
  const verifier = generateVerifier();
68
68
  const requestId = this.requestId;
69
+ const codeChallenge = await sha256Base64Url(verifier);
69
70
  sessionStorage.setItem("sso_ctx", JSON.stringify({ state, nonce, verifier, requestId }));
70
71
  this.createUI();
71
72
  return new Promise((resolve, reject) => {
@@ -98,11 +99,10 @@ var BigsoAuth = class extends EventEmitter {
98
99
  this.closeUI();
99
100
  cleanup();
100
101
  this.emit("fallback");
101
- window.location.href = this.buildFallbackUrl();
102
+ window.location.href = this.buildFallbackUrl(codeChallenge, state);
102
103
  reject(new Error("Timeout"));
103
104
  }
104
105
  }, this.options.timeout);
105
- const codeChallenge = await sha256Base64Url(verifier);
106
106
  const initPayload = {
107
107
  state,
108
108
  nonce,
@@ -175,7 +175,7 @@ var BigsoAuth = class extends EventEmitter {
175
175
  cleanup();
176
176
  if (errorPayload.code === "version_mismatch") {
177
177
  this.emit("error", errorPayload);
178
- window.location.href = this.buildFallbackUrl();
178
+ window.location.href = this.buildFallbackUrl(codeChallenge, state);
179
179
  reject(new Error(`Version mismatch: expected ${errorPayload.expected_version}`));
180
180
  } else {
181
181
  this.emit("error", errorPayload);
@@ -310,12 +310,13 @@ var BigsoAuth = class extends EventEmitter {
310
310
  `;
311
311
  }
312
312
  // ─── Helpers ──────────────────────────────────────────────────────
313
- buildFallbackUrl() {
313
+ buildFallbackUrl(codeChallenge, state) {
314
314
  const url = new URL(this.options.ssoOrigin);
315
315
  url.searchParams.set("app_id", this.options.clientId);
316
316
  url.searchParams.set("redirect_uri", this.options.redirectUri || window.location.origin);
317
317
  url.searchParams.set("response_type", "code");
318
- url.searchParams.set("state", generateRandomId());
318
+ url.searchParams.set("state", state);
319
+ url.searchParams.set("code_challenge", codeChallenge);
319
320
  url.searchParams.set("code_challenge_method", "S256");
320
321
  url.searchParams.set("client_id", this.options.clientId);
321
322
  return url.toString();
@@ -0,0 +1,33 @@
1
+ // src/utils/jws.ts
2
+ import { jwtVerify, createRemoteJWKSet } from "jose";
3
+ async function verifySignedPayload(token, jwksUrl, expectedAudience) {
4
+ const JWKS = createRemoteJWKSet(new URL(jwksUrl));
5
+ const { payload } = await jwtVerify(token, JWKS, {
6
+ audience: expectedAudience
7
+ });
8
+ return payload;
9
+ }
10
+ async function verifyAccessToken(accessToken, jwksUrl) {
11
+ const JWKS = createRemoteJWKSet(new URL(jwksUrl));
12
+ const { payload } = await jwtVerify(accessToken, JWKS);
13
+ if (!payload.sub || !payload.jti) {
14
+ throw new Error("Invalid token structure: missing sub or jti");
15
+ }
16
+ return {
17
+ sub: payload.sub,
18
+ jti: payload.jti,
19
+ iss: payload.iss,
20
+ aud: payload.aud || "",
21
+ exp: payload.exp,
22
+ iat: payload.iat,
23
+ tenants: payload.tenants || [],
24
+ systemRole: payload.systemRole || "user",
25
+ scope: payload.scope,
26
+ deviceFingerprint: payload.deviceFingerprint
27
+ };
28
+ }
29
+
30
+ export {
31
+ verifySignedPayload,
32
+ verifyAccessToken
33
+ };
@@ -1,6 +1,6 @@
1
1
  import { Request, Response, NextFunction, Router } from 'express';
2
2
  import { BigsoSsoClient } from '../node/index.cjs';
3
- import { S as SsoJwtTenant, b as SsoTokenPayload, V as V2ExchangeResponse } from '../types-D5BaCbus.cjs';
3
+ import { S as SsoJwtTenant, b as SsoTokenPayload, V as V2ExchangeResponse } from '../types-K3V5MV8v.cjs';
4
4
 
5
5
  interface SsoAuthMiddlewareOptions {
6
6
  ssoClient: BigsoSsoClient;
@@ -1,6 +1,6 @@
1
1
  import { Request, Response, NextFunction, Router } from 'express';
2
2
  import { BigsoSsoClient } from '../node/index.js';
3
- import { S as SsoJwtTenant, b as SsoTokenPayload, V as V2ExchangeResponse } from '../types-D5BaCbus.js';
3
+ import { S as SsoJwtTenant, b as SsoTokenPayload, V as V2ExchangeResponse } from '../types-K3V5MV8v.js';
4
4
 
5
5
  interface SsoAuthMiddlewareOptions {
6
6
  ssoClient: BigsoSsoClient;
@@ -48,6 +48,7 @@ async function verifyAccessToken(accessToken, jwksUrl) {
48
48
  iat: payload.iat,
49
49
  tenants: payload.tenants || [],
50
50
  systemRole: payload.systemRole || "user",
51
+ scope: payload.scope,
51
52
  deviceFingerprint: payload.deviceFingerprint
52
53
  };
53
54
  }
@@ -1,4 +1,4 @@
1
- import { b as SsoTokenPayload, c as V2LoginResponse, V as V2ExchangeResponse, d as V2RefreshResponse } from '../types-D5BaCbus.cjs';
1
+ import { b as SsoTokenPayload, c as V2LoginResponse, V as V2ExchangeResponse, d as V2RefreshResponse } from '../types-K3V5MV8v.cjs';
2
2
 
3
3
  interface SsoClientOptions {
4
4
  ssoBackendUrl: string;
@@ -1,4 +1,4 @@
1
- import { b as SsoTokenPayload, c as V2LoginResponse, V as V2ExchangeResponse, d as V2RefreshResponse } from '../types-D5BaCbus.js';
1
+ import { b as SsoTokenPayload, c as V2LoginResponse, V as V2ExchangeResponse, d as V2RefreshResponse } from '../types-K3V5MV8v.js';
2
2
 
3
3
  interface SsoClientOptions {
4
4
  ssoBackendUrl: string;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  verifyAccessToken,
3
3
  verifySignedPayload
4
- } from "../chunk-5ECHA2VH.js";
4
+ } from "../chunk-PB3GVAEJ.js";
5
5
 
6
6
  // src/node/SsoClient.ts
7
7
  var BigsoSsoClient = class {
@@ -0,0 +1,81 @@
1
+ interface BigsoAuthOptions {
2
+ clientId: string;
3
+ ssoOrigin: string;
4
+ jwksUrl: string;
5
+ timeout?: number;
6
+ debug?: boolean;
7
+ redirectUri?: string;
8
+ tenantHint?: string;
9
+ theme?: 'light' | 'dark';
10
+ }
11
+ interface SsoUser {
12
+ userId: string;
13
+ email: string;
14
+ firstName: string;
15
+ lastName: string;
16
+ }
17
+ interface SsoTenant {
18
+ tenantId: string;
19
+ name: string;
20
+ slug: string;
21
+ role: string;
22
+ }
23
+ interface SsoJwtTenant {
24
+ id: string;
25
+ name: string;
26
+ slug: string;
27
+ role: string;
28
+ apps: string[];
29
+ }
30
+ interface SsoTokenPayload {
31
+ sub: string;
32
+ jti: string;
33
+ iss: string;
34
+ aud: string;
35
+ exp: number;
36
+ iat: number;
37
+ tenants: SsoJwtTenant[];
38
+ systemRole: string;
39
+ scope?: string[];
40
+ deviceFingerprint?: string;
41
+ }
42
+ interface V2LoginResponse {
43
+ success: boolean;
44
+ tokens: {
45
+ accessToken: string;
46
+ expiresIn: number;
47
+ };
48
+ user: SsoUser;
49
+ }
50
+ interface V2ExchangeResponse {
51
+ success: boolean;
52
+ tokens: {
53
+ accessToken: string;
54
+ refreshToken: string;
55
+ expiresIn: number;
56
+ };
57
+ user: SsoUser;
58
+ tenant: SsoTenant;
59
+ }
60
+ interface V2RefreshResponse {
61
+ success: boolean;
62
+ tokens: {
63
+ accessToken: string;
64
+ expiresIn: number;
65
+ };
66
+ }
67
+ interface BigsoAuthResult {
68
+ code: string;
69
+ state: string;
70
+ nonce: string;
71
+ codeVerifier: string;
72
+ signed_payload: string;
73
+ tenant?: SsoTenant;
74
+ jti?: string;
75
+ iss?: string;
76
+ aud?: string;
77
+ exp?: number;
78
+ iat?: number;
79
+ }
80
+
81
+ export type { BigsoAuthOptions as B, SsoJwtTenant as S, V2ExchangeResponse as V, BigsoAuthResult as a, SsoTokenPayload as b, V2LoginResponse as c, V2RefreshResponse as d };
@@ -0,0 +1,81 @@
1
+ interface BigsoAuthOptions {
2
+ clientId: string;
3
+ ssoOrigin: string;
4
+ jwksUrl: string;
5
+ timeout?: number;
6
+ debug?: boolean;
7
+ redirectUri?: string;
8
+ tenantHint?: string;
9
+ theme?: 'light' | 'dark';
10
+ }
11
+ interface SsoUser {
12
+ userId: string;
13
+ email: string;
14
+ firstName: string;
15
+ lastName: string;
16
+ }
17
+ interface SsoTenant {
18
+ tenantId: string;
19
+ name: string;
20
+ slug: string;
21
+ role: string;
22
+ }
23
+ interface SsoJwtTenant {
24
+ id: string;
25
+ name: string;
26
+ slug: string;
27
+ role: string;
28
+ apps: string[];
29
+ }
30
+ interface SsoTokenPayload {
31
+ sub: string;
32
+ jti: string;
33
+ iss: string;
34
+ aud: string;
35
+ exp: number;
36
+ iat: number;
37
+ tenants: SsoJwtTenant[];
38
+ systemRole: string;
39
+ scope?: string[];
40
+ deviceFingerprint?: string;
41
+ }
42
+ interface V2LoginResponse {
43
+ success: boolean;
44
+ tokens: {
45
+ accessToken: string;
46
+ expiresIn: number;
47
+ };
48
+ user: SsoUser;
49
+ }
50
+ interface V2ExchangeResponse {
51
+ success: boolean;
52
+ tokens: {
53
+ accessToken: string;
54
+ refreshToken: string;
55
+ expiresIn: number;
56
+ };
57
+ user: SsoUser;
58
+ tenant: SsoTenant;
59
+ }
60
+ interface V2RefreshResponse {
61
+ success: boolean;
62
+ tokens: {
63
+ accessToken: string;
64
+ expiresIn: number;
65
+ };
66
+ }
67
+ interface BigsoAuthResult {
68
+ code: string;
69
+ state: string;
70
+ nonce: string;
71
+ codeVerifier: string;
72
+ signed_payload: string;
73
+ tenant?: SsoTenant;
74
+ jti?: string;
75
+ iss?: string;
76
+ aud?: string;
77
+ exp?: number;
78
+ iat?: number;
79
+ }
80
+
81
+ export type { BigsoAuthOptions as B, SsoJwtTenant as S, V2ExchangeResponse as V, BigsoAuthResult as a, SsoTokenPayload as b, V2LoginResponse as c, V2RefreshResponse as d };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigso/auth-sdk",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "SDK de autenticación para SSO v2 - JWT Bearer + PKCE",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",
@@ -38,6 +38,7 @@
38
38
  "scripts": {
39
39
  "build": "tsup src/browser/index.ts src/node/index.ts src/express/index.ts --format esm,cjs --dts --out-dir dist",
40
40
  "dev": "tsup src/browser/index.ts src/node/index.ts src/express/index.ts --watch --out-dir dist",
41
+ "prepublishOnly": "npm run build",
41
42
  "lint": "eslint .",
42
43
  "test": "vitest",
43
44
  "release": "git tag v$npm_package_version && git push origin v$npm_package_version"