@corvushold/guard-sdk 0.14.0 → 0.16.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 CHANGED
@@ -20,6 +20,36 @@ This SDK uses a spec‑first approach. Core auth flows implemented:
20
20
  - Token introspection
21
21
  - Magic link: send + verify
22
22
 
23
+ ## OAuth2 Authorization Code (PKCE)
24
+
25
+ Use the SDK to build the authorization URL from the configured `baseUrl` (Guard API host),
26
+ instead of manually composing `/oauth/authorize` against the current page origin.
27
+
28
+ ```ts
29
+ import { GuardClient } from '@corvushold/guard-sdk';
30
+
31
+ const client = new GuardClient({
32
+ baseUrl: 'http://localhost:8082', // Guard API URL
33
+ });
34
+
35
+ const authorizeUrl = client.buildOAuth2AuthorizeUrl({
36
+ client_id: 'gc_VXfCQgfwZChIpUFVCLAgEecI_OKSMszh',
37
+ redirect_uri: 'http://localhost:3000/callback',
38
+ code_challenge: 'PKCE_CODE_CHALLENGE',
39
+ code_challenge_method: 'S256',
40
+ scope: ['openid', 'profile', 'email'],
41
+ state: crypto.randomUUID(),
42
+ });
43
+
44
+ window.location.href = authorizeUrl;
45
+ ```
46
+
47
+ Notes:
48
+
49
+ - `response_type` defaults to `code`.
50
+ - `scope` accepts either a string (`"openid profile email"`) or a string array.
51
+ - When `code_challenge` is set and `code_challenge_method` is omitted, the SDK defaults to `S256`.
52
+
23
53
  ## WorkOS Organization Portal Link
24
54
 
25
55
  Generate a WorkOS Admin Portal link for an organization (server enforces admin-only RBAC):
package/dist/index.cjs CHANGED
@@ -279,7 +279,7 @@ var HttpClient = class {
279
279
 
280
280
  // package.json
281
281
  var package_default = {
282
- version: "0.14.0"};
282
+ version: "0.16.0"};
283
283
 
284
284
  // src/client.ts
285
285
  function isTenantSelectionRequired(data) {
@@ -1050,6 +1050,51 @@ var GuardClient = class {
1050
1050
  // ==============================
1051
1051
  // OAuth2 Discovery (RFC 8414)
1052
1052
  // ==============================
1053
+ /**
1054
+ * Build an OAuth2 Authorization Code URL using this Guard client's baseUrl.
1055
+ *
1056
+ * This prevents accidental redirects to the current app origin when initiating
1057
+ * OAuth2 flows from SPAs.
1058
+ *
1059
+ * @example
1060
+ * ```ts
1061
+ * const url = client.buildOAuth2AuthorizeUrl({
1062
+ * client_id: 'gc_123',
1063
+ * redirect_uri: 'https://app.example.com/callback',
1064
+ * code_challenge: 'abc...',
1065
+ * code_challenge_method: 'S256',
1066
+ * scope: ['openid', 'profile', 'email'],
1067
+ * state: 'csrf-state',
1068
+ * });
1069
+ * window.location.href = url;
1070
+ * ```
1071
+ */
1072
+ buildOAuth2AuthorizeUrl(params) {
1073
+ const clientID = params.client_id?.trim();
1074
+ if (!clientID) throw new Error("client_id is required");
1075
+ const redirectURI = params.redirect_uri?.trim();
1076
+ if (!redirectURI) throw new Error("redirect_uri is required");
1077
+ const responseType = params.response_type ?? "code";
1078
+ if (responseType !== "code") {
1079
+ throw new Error('response_type must be "code"');
1080
+ }
1081
+ const scope = Array.isArray(params.scope) ? params.scope.filter(Boolean).join(" ").trim() : params.scope?.trim();
1082
+ const u = new URL("/oauth/authorize", this.baseUrl);
1083
+ u.searchParams.set("response_type", responseType);
1084
+ u.searchParams.set("client_id", clientID);
1085
+ u.searchParams.set("redirect_uri", redirectURI);
1086
+ if (scope) u.searchParams.set("scope", scope);
1087
+ if (params.state) u.searchParams.set("state", params.state);
1088
+ if (params.nonce) u.searchParams.set("nonce", params.nonce);
1089
+ if (params.code_challenge) u.searchParams.set("code_challenge", params.code_challenge);
1090
+ if (params.code_challenge) {
1091
+ u.searchParams.set("code_challenge_method", params.code_challenge_method ?? "S256");
1092
+ }
1093
+ if (params.prompt) u.searchParams.set("prompt", params.prompt);
1094
+ if (params.login_hint) u.searchParams.set("login_hint", params.login_hint);
1095
+ if (params.max_age !== void 0) u.searchParams.set("max_age", String(params.max_age));
1096
+ return u.toString();
1097
+ }
1053
1098
  /**
1054
1099
  * Fetch OAuth 2.0 Authorization Server Metadata (RFC 8414)
1055
1100
  * Returns server capabilities including supported auth modes, endpoints, and grant types.