@corvushold/guard-sdk 0.15.0 → 0.17.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.15.0"};
282
+ version: "0.17.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.
@@ -1058,6 +1103,47 @@ var GuardClient = class {
1058
1103
  async getOAuth2Metadata() {
1059
1104
  return this.request("/.well-known/oauth-authorization-server", { method: "GET" });
1060
1105
  }
1106
+ /**
1107
+ * Exchange an OAuth2 authorization code for tokens (Authorization Code + PKCE).
1108
+ */
1109
+ async exchangeOAuth2Code(input) {
1110
+ const code = input.code?.trim();
1111
+ const clientID = input.client_id?.trim();
1112
+ const redirectURI = input.redirect_uri?.trim();
1113
+ const codeVerifier = input.code_verifier?.trim();
1114
+ if (!code) throw new Error("code is required");
1115
+ if (!clientID) throw new Error("client_id is required");
1116
+ if (!redirectURI) throw new Error("redirect_uri is required");
1117
+ if (!codeVerifier) throw new Error("code_verifier is required");
1118
+ const body = new URLSearchParams();
1119
+ body.set("grant_type", "authorization_code");
1120
+ body.set("code", code);
1121
+ body.set("client_id", clientID);
1122
+ body.set("redirect_uri", redirectURI);
1123
+ body.set("code_verifier", codeVerifier);
1124
+ const res = await this.request("/oauth/token", {
1125
+ method: "POST",
1126
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
1127
+ body: body.toString()
1128
+ });
1129
+ if (res.meta.status >= 200 && res.meta.status < 300) this.persistTokensFrom(res.data);
1130
+ return res;
1131
+ }
1132
+ /**
1133
+ * Revoke an OAuth2 token (RFC7009). Server returns 200 for unknown tokens as well.
1134
+ */
1135
+ async revokeOAuth2Token(input) {
1136
+ const token = input.token?.trim();
1137
+ if (!token) throw new Error("token is required");
1138
+ const body = new URLSearchParams();
1139
+ body.set("token", token);
1140
+ if (input.token_type_hint) body.set("token_type_hint", input.token_type_hint);
1141
+ return this.request("/oauth/revoke", {
1142
+ method: "POST",
1143
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
1144
+ body: body.toString()
1145
+ });
1146
+ }
1061
1147
  /**
1062
1148
  * Static helper to discover OAuth2 metadata from any Guard API base URL.
1063
1149
  * Useful for auto-configuration before creating a GuardClient instance.