@polyguard/sdk 1.4.2 → 1.5.1

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.
Files changed (116) hide show
  1. package/LICENSE +200 -0
  2. package/README.md +328 -0
  3. package/dist/sdk.esm.js +16 -4
  4. package/dist/sdk.js +16 -4
  5. package/dist/server.esm.js +83 -0
  6. package/package.json +14 -3
  7. package/dist/sdk.global.js +0 -11256
  8. package/scripts/regenerate-client.sh +0 -59
  9. package/src/PolyguardClient.js +0 -25
  10. package/src/PolyguardWebsocketClientImpl.js +0 -183
  11. package/src/__tests__/PolyguardClient.test.js +0 -65
  12. package/src/__tests__/PolyguardWebsocketClientImpl.test.js +0 -574
  13. package/src/__tests__/helpers/fixtures.js +0 -27
  14. package/src/__tests__/helpers/mockWebSocket.js +0 -66
  15. package/src/__tests__/sidebar.test.js +0 -289
  16. package/src/browser.js +0 -9
  17. package/src/generated/.babelrc +0 -33
  18. package/src/generated/.openapi-generator/FILES +0 -63
  19. package/src/generated/.openapi-generator/VERSION +0 -1
  20. package/src/generated/.openapi-generator-ignore +0 -23
  21. package/src/generated/.travis.yml +0 -5
  22. package/src/generated/README.md +0 -211
  23. package/src/generated/docs/ApiControllersPslStirCallRequest.md +0 -18
  24. package/src/generated/docs/ApiModelsApiCallModelsCallRequest.md +0 -12
  25. package/src/generated/docs/AppId.md +0 -8
  26. package/src/generated/docs/AppleApi.md +0 -88
  27. package/src/generated/docs/AuthApi.md +0 -1464
  28. package/src/generated/docs/BlockingApi.md +0 -208
  29. package/src/generated/docs/CallsApi.md +0 -140
  30. package/src/generated/docs/Certainty.md +0 -8
  31. package/src/generated/docs/CreateLinkRequest.md +0 -12
  32. package/src/generated/docs/DefaultApi.md +0 -128
  33. package/src/generated/docs/HTTPValidationError.md +0 -9
  34. package/src/generated/docs/InviteRequestModel.md +0 -10
  35. package/src/generated/docs/JWTRequest.md +0 -11
  36. package/src/generated/docs/LinksApi.md +0 -162
  37. package/src/generated/docs/NumberVerification.md +0 -10
  38. package/src/generated/docs/SdkApi.md +0 -54
  39. package/src/generated/docs/SecureLinksApi.md +0 -614
  40. package/src/generated/docs/StartAttestationRequest.md +0 -9
  41. package/src/generated/docs/StartMeetingRequest.md +0 -9
  42. package/src/generated/docs/StirApi.md +0 -52
  43. package/src/generated/docs/TwilioApi.md +0 -138
  44. package/src/generated/docs/UsersApi.md +0 -362
  45. package/src/generated/docs/ValidationError.md +0 -11
  46. package/src/generated/docs/ValidationErrorLocInner.md +0 -8
  47. package/src/generated/docs/VeriffApi.md +0 -188
  48. package/src/generated/docs/VeriffSessionRequest.md +0 -10
  49. package/src/generated/docs/VerifyRequest.md +0 -10
  50. package/src/generated/docs/WellKnownApi.md +0 -128
  51. package/src/generated/docs/ZoomApi.md +0 -848
  52. package/src/generated/git_push.sh +0 -57
  53. package/src/generated/mocha.opts +0 -1
  54. package/src/generated/package.json +0 -46
  55. package/src/generated/src/ApiClient.js +0 -677
  56. package/src/generated/src/api/AppleApi.js +0 -109
  57. package/src/generated/src/api/AuthApi.js +0 -1512
  58. package/src/generated/src/api/BlockingApi.js +0 -217
  59. package/src/generated/src/api/CallsApi.js +0 -164
  60. package/src/generated/src/api/DefaultApi.js +0 -145
  61. package/src/generated/src/api/LinksApi.js +0 -195
  62. package/src/generated/src/api/SdkApi.js +0 -81
  63. package/src/generated/src/api/SecureLinksApi.js +0 -670
  64. package/src/generated/src/api/StirApi.js +0 -80
  65. package/src/generated/src/api/TwilioApi.js +0 -158
  66. package/src/generated/src/api/UsersApi.js +0 -375
  67. package/src/generated/src/api/VeriffApi.js +0 -209
  68. package/src/generated/src/api/WellKnownApi.js +0 -145
  69. package/src/generated/src/api/ZoomApi.js +0 -823
  70. package/src/generated/src/index.js +0 -244
  71. package/src/generated/src/model/ApiControllersPslStirCallRequest.js +0 -211
  72. package/src/generated/src/model/ApiModelsApiCallModelsCallRequest.js +0 -119
  73. package/src/generated/src/model/CreateLinkRequest.js +0 -131
  74. package/src/generated/src/model/HTTPValidationError.js +0 -94
  75. package/src/generated/src/model/InviteRequestModel.js +0 -109
  76. package/src/generated/src/model/JWTRequest.js +0 -113
  77. package/src/generated/src/model/NumberVerification.js +0 -107
  78. package/src/generated/src/model/StartAttestationRequest.js +0 -95
  79. package/src/generated/src/model/StartMeetingRequest.js +0 -95
  80. package/src/generated/src/model/ValidationError.js +0 -130
  81. package/src/generated/src/model/VeriffSessionRequest.js +0 -107
  82. package/src/generated/src/model/VerifyRequest.js +0 -109
  83. package/src/generated/test/api/AppleApi.spec.js +0 -73
  84. package/src/generated/test/api/AuthApi.spec.js +0 -353
  85. package/src/generated/test/api/BlockingApi.spec.js +0 -103
  86. package/src/generated/test/api/CallsApi.spec.js +0 -83
  87. package/src/generated/test/api/DefaultApi.spec.js +0 -73
  88. package/src/generated/test/api/LinksApi.spec.js +0 -83
  89. package/src/generated/test/api/SdkApi.spec.js +0 -63
  90. package/src/generated/test/api/SecureLinksApi.spec.js +0 -143
  91. package/src/generated/test/api/StirApi.spec.js +0 -63
  92. package/src/generated/test/api/TwilioApi.spec.js +0 -83
  93. package/src/generated/test/api/UsersApi.spec.js +0 -133
  94. package/src/generated/test/api/VeriffApi.spec.js +0 -93
  95. package/src/generated/test/api/WellKnownApi.spec.js +0 -83
  96. package/src/generated/test/api/ZoomApi.spec.js +0 -213
  97. package/src/generated/test/model/ApiControllersPslStirCallRequest.spec.js +0 -119
  98. package/src/generated/test/model/ApiModelsApiCallModelsCallRequest.spec.js +0 -83
  99. package/src/generated/test/model/AppId.spec.js +0 -59
  100. package/src/generated/test/model/Certainty.spec.js +0 -59
  101. package/src/generated/test/model/CreateLinkRequest.spec.js +0 -83
  102. package/src/generated/test/model/HTTPValidationError.spec.js +0 -65
  103. package/src/generated/test/model/InviteRequestModel.spec.js +0 -71
  104. package/src/generated/test/model/JWTRequest.spec.js +0 -77
  105. package/src/generated/test/model/NumberVerification.spec.js +0 -71
  106. package/src/generated/test/model/StartAttestationRequest.spec.js +0 -65
  107. package/src/generated/test/model/StartMeetingRequest.spec.js +0 -65
  108. package/src/generated/test/model/ValidationError.spec.js +0 -77
  109. package/src/generated/test/model/ValidationErrorLocInner.spec.js +0 -59
  110. package/src/generated/test/model/VeriffSessionRequest.spec.js +0 -71
  111. package/src/generated/test/model/VerifyRequest.spec.js +0 -71
  112. package/src/index.js +0 -16
  113. package/src/messageHandler.js +0 -121
  114. package/src/ticketService.js +0 -24
  115. package/src/ui.js +0 -88
  116. package/vitest.config.js +0 -10
package/LICENSE ADDED
@@ -0,0 +1,200 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may accept support,
167
+ warranty, indemnity, or other liability obligations and/or rights
168
+ consistent with this License. However, in accepting such obligations,
169
+ You may act only on Your own behalf and on Your sole responsibility,
170
+ not on behalf of any other Contributor, and only if You agree to
171
+ indemnify, defend, and hold each Contributor harmless for any
172
+ liability incurred by, or claims asserted against, such Contributor
173
+ by reason of your accepting any such warranty or additional liability.
174
+
175
+ END OF TERMS AND CONDITIONS
176
+
177
+ APPENDIX: How to apply the Apache License to your work.
178
+
179
+ To apply the Apache License to your work, attach the following
180
+ boilerplate notice, with the fields enclosed by brackets "[]"
181
+ replaced with your own identifying information. (Don't include
182
+ the brackets!) The text should be enclosed in the appropriate
183
+ comment syntax for the file format. We also recommend that a
184
+ file or class name and description of purpose be included on the
185
+ same "printed page" as the copyright notice for easier
186
+ identification within third-party archives.
187
+
188
+ Copyright 2026 Polyguard, Inc.
189
+
190
+ Licensed under the Apache License, Version 2.0 (the "License");
191
+ you may not use this file except in compliance with the License.
192
+ You may obtain a copy of the License at
193
+
194
+ http://www.apache.org/licenses/LICENSE-2.0
195
+
196
+ Unless required by applicable law or agreed to in writing, software
197
+ distributed under the License is distributed on an "AS IS" BASIS,
198
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
199
+ implied. See the License for the specific language governing
200
+ permissions and limitations under the License.
package/README.md ADDED
@@ -0,0 +1,328 @@
1
+ # @polyguard/sdk
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@polyguard/sdk.svg)](https://www.npmjs.com/package/@polyguard/sdk)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@polyguard/sdk.svg)](https://www.npmjs.com/package/@polyguard/sdk)
5
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
6
+
7
+ The official JavaScript SDK for [Polyguard](https://polyguard.ai) — drop-in identity verification for web applications. Users scan a QR code with the Polyguard mobile app and your site receives a signed JWT containing the verified claims.
8
+
9
+ ## Features
10
+
11
+ - QR-code based identity verification
12
+ - Real-time WebSocket session with automatic reconnection
13
+ - Two UI modes: full-screen modal or embedded into your own DOM
14
+ - Mobile-aware: deep-links into the Polyguard app on the same device
15
+ - Configurable required proofs (e.g. `Full Name`, age, document type)
16
+ - Signed JWT response — verifiable with your Polyguard public key
17
+ - Convenience `require()` helper for one-call proof matching
18
+ - Server-side proxy helper for safe API-key usage (Next.js, Express, Workers, Bun, Deno)
19
+ - Ships as both an ESM module (npm) and a browser IIFE (CDN)
20
+
21
+ ## Requirements
22
+
23
+ - Browser runtime with `WebSocket`, `fetch`, and ES2020 features
24
+ - Node.js 18+ for any server-side proxy code
25
+ - A Polyguard account with an `appId` and API key — sign up at [console.polyguard.ai](https://console.polyguard.ai)
26
+
27
+ ## Installation
28
+
29
+ ### npm
30
+
31
+ ```bash
32
+ npm install @polyguard/sdk
33
+ ```
34
+
35
+ ### CDN
36
+
37
+ ```html
38
+ <script src="https://cdn.polyguard.ai/sdk/latest/sdk.js"></script>
39
+ ```
40
+
41
+ Pin to a specific version in production:
42
+
43
+ ```html
44
+ <script src="https://cdn.polyguard.ai/sdk/1.5.0/sdk.js"></script>
45
+ ```
46
+
47
+ ## Quick start
48
+
49
+ ### ESM (bundlers, frameworks)
50
+
51
+ ```js
52
+ import { PolyguardClient } from '@polyguard/sdk';
53
+
54
+ const client = new PolyguardClient({
55
+ appId: 'your-app-id',
56
+ apiServer: 'api.polyguard.ai', // optional
57
+ proxyUrl: '/api/polyguard', // your server-side proxy — see below
58
+ });
59
+
60
+ try {
61
+ const jwt = await client.verify();
62
+ // jwt is a signed token containing the verified identity claims
63
+ } catch (err) {
64
+ // The user closed the modal or cancelled.
65
+ }
66
+ ```
67
+
68
+ ### Browser (CDN)
69
+
70
+ ```html
71
+ <script src="https://cdn.polyguard.ai/sdk/latest/sdk.js"></script>
72
+ <script>
73
+ const client = new window.Polyguard.Client({
74
+ appId: 'your-app-id',
75
+ apiServer: 'api.polyguard.ai', // optional
76
+ proxyUrl: '/api/polyguard',
77
+ });
78
+
79
+ client.verify().then((jwt) => {
80
+ // jwt is a signed token containing the verified identity claims
81
+ });
82
+ </script>
83
+ ```
84
+
85
+ ## Security: never ship an API key to the browser
86
+
87
+ API keys grant role-based access to your Polyguard tenant. Passing `apiKey` to `PolyguardClient` in a browser context **throws** by design:
88
+
89
+ ```
90
+ Error: apiKey is forbidden in browser code; use proxyUrl with a server-side proxy
91
+ (see @polyguard/sdk/server). Set iKnowThisIsInsecure: true to bypass.
92
+ ```
93
+
94
+ The supported pattern is:
95
+
96
+ 1. Run a tiny proxy on your own server that injects the API key.
97
+ 2. Point the browser SDK at that proxy with `proxyUrl`.
98
+
99
+ The SDK provides `createProxyHandler` from the `@polyguard/sdk/server` entry point. It is a Fetch-API request handler that runs on Node 18+, Cloudflare Workers, Bun, Deno, Vercel Edge, Netlify Edge, and any framework that accepts a `(Request) => Response` function.
100
+
101
+ ### Server proxy with Next.js (App Router)
102
+
103
+ ```ts
104
+ // app/api/polyguard/[...path]/route.ts
105
+ import { createProxyHandler } from '@polyguard/sdk/server';
106
+
107
+ const handler = createProxyHandler({
108
+ apiKey: process.env.POLYGUARD_API_KEY,
109
+ apiServer: 'api.polyguard.ai',
110
+ pathPrefix: '/api/polyguard',
111
+ });
112
+
113
+ export const GET = handler;
114
+ export const POST = handler;
115
+ export const PUT = handler;
116
+ ```
117
+
118
+ ### Server proxy with Cloudflare Workers / Bun / Deno
119
+
120
+ ```js
121
+ import { createProxyHandler } from '@polyguard/sdk/server';
122
+
123
+ const polyguardProxy = createProxyHandler({
124
+ apiKey: globalThis.POLYGUARD_API_KEY, // bind in wrangler.toml / env
125
+ apiServer: 'api.polyguard.ai',
126
+ });
127
+
128
+ export default { fetch: polyguardProxy };
129
+ ```
130
+
131
+ ### Server proxy with Express
132
+
133
+ ```js
134
+ import express from 'express';
135
+ import { createProxyHandler } from '@polyguard/sdk/server';
136
+
137
+ const app = express();
138
+ const polyguardProxy = createProxyHandler({
139
+ apiKey: process.env.POLYGUARD_API_KEY,
140
+ apiServer: 'api.polyguard.ai',
141
+ });
142
+
143
+ // Wrap the Fetch-style handler for Express
144
+ app.all('/api/polyguard/*', async (req, res) => {
145
+ const url = `http://${req.headers.host}${req.originalUrl}`;
146
+ const request = new Request(url, {
147
+ method: req.method,
148
+ headers: req.headers,
149
+ body: ['GET', 'HEAD'].includes(req.method) ? undefined : req,
150
+ duplex: 'half',
151
+ });
152
+ const response = await polyguardProxy(request);
153
+ res.status(response.status);
154
+ response.headers.forEach((v, k) => res.setHeader(k, v));
155
+ if (response.body) {
156
+ const reader = response.body.getReader();
157
+ while (true) {
158
+ const { value, done } = await reader.read();
159
+ if (done) break;
160
+ res.write(value);
161
+ }
162
+ }
163
+ res.end();
164
+ });
165
+ ```
166
+
167
+ ### Proxy options
168
+
169
+ | Option | Type | Default | Description |
170
+ |---|---|---|---|
171
+ | `apiKey` | string | **required** | Your Polyguard API key — must come from a secret store / env var. |
172
+ | `apiServer` | string | `api.polyguard.ai` | Hostname of the Polyguard API. |
173
+ | `pathPrefix` | string | `/api/polyguard` | Prefix stripped from incoming requests before forwarding. |
174
+ | `allowedPaths` | string[] | `['POST /v2/ticket/', 'PUT /link/', 'GET /v2/preview/']` | Whitelist of `METHOD path-prefix` patterns. Requests that do not match return `404`. |
175
+
176
+ The default whitelist is the minimum surface the browser SDK calls. **Do not broaden `allowedPaths` without reason** — the proxy attaches your API key to every forwarded request, so each entry is an open door to that part of the Polyguard API.
177
+
178
+ ## Usage
179
+
180
+ ### Modal verification
181
+
182
+ The default `verify()` call opens an overlay modal with the QR code, a spinner, and a cancel button. It returns the JWT once the user completes verification on their phone.
183
+
184
+ ```js
185
+ const jwt = await client.verify();
186
+ ```
187
+
188
+ ### Embedded verification
189
+
190
+ Pass the `id` of an element you already render. The SDK injects the QR code there instead of opening a modal:
191
+
192
+ ```html
193
+ <div id="pg-qr"></div>
194
+ ```
195
+
196
+ ```js
197
+ const jwt = await client.verify('pg-qr');
198
+ ```
199
+
200
+ Embedded mode does **not** render a close button — you control the surrounding UI.
201
+
202
+ ### Proof matching with `require()`
203
+
204
+ `require()` runs `verify()` and then checks the decoded JWT payload for specific expected values. It is a convenience wrapper for the common "is this the user who claims to be X?" pattern:
205
+
206
+ ```js
207
+ const isJane = await client.require({ 'Full Name': 'Jane Doe' });
208
+ if (isJane) {
209
+ // proceed
210
+ }
211
+ ```
212
+
213
+ `require()` resolves to `false` if the user cancels, the server is unreachable, or any expected claim does not match. It never throws.
214
+
215
+ > **Always verify the JWT signature on your backend** before trusting any claim. You can also receive the complete JWT in your backend from a Polyguard webhook. `require()` decodes the payload client-side for UX gating only, and can be used to begin polling the backend for the webhook response.
216
+
217
+ ### Raw response
218
+
219
+ For target/embedded mode, pass `rawJwt: true` (second argument to `verify`) to get the full JWT string instead of the decoded object:
220
+
221
+ ```js
222
+ const response = await client.verify('pg-qr', true);
223
+ ```
224
+
225
+ ### Error handling
226
+
227
+ The behavior differs by UI mode — this is intentional:
228
+
229
+ | Scenario | Modal mode | Embedded / target mode |
230
+ |---|---|---|
231
+ | User clicks the close button | rejects with `Error('User cancelled')` | n/a (no close button) |
232
+ | Verification succeeds | resolves with the decoded JWT object (or raw JWT string if `rawJwt: true`) | resolves with the decoded JWT object (or raw JWT string if `rawJwt: true`) |
233
+
234
+ Always handle both a rejected promise and a `FAIL` result.
235
+
236
+ ## Configuration
237
+
238
+ ```js
239
+ new PolyguardClient(options)
240
+ ```
241
+
242
+ | Option | Type | Default | Description |
243
+ |---|---|---|---|
244
+ | `appId` | string | — | Your Polyguard application ID. |
245
+ | `apiServer` | string | — | Hostname of the Polyguard API (e.g. `api.polyguard.ai`). |
246
+ | `proxyUrl` | string | — | URL of your server-side proxy. **Recommended** for all browser deployments. |
247
+ | `baseUrl` | string | — | Direct API base URL. Mutually exclusive with `proxyUrl`. |
248
+ | `apiKey` | string | — | API key. **Throws in browser code.** Use a proxy instead. |
249
+ | `iKnowThisIsInsecure` | boolean | `false` | Escape hatch that disables the browser `apiKey` check. Do not use in production. |
250
+ | `requiredProofs` | string[] | `['Full Name']` | Claims the user must satisfy during verification. |
251
+ | `scanType` | string | `'single'` | Verification mode — `single` is the standard one-shot flow. `multi` should be used for trust checks with PG-Presence enabled. |
252
+ | `link_uuid` | string | — | UUID for a pre-created (semi-dynamic / "Adhoc") verification link. Omit for fully dynamic flows. |
253
+ | `integrationType` | string | `'websocket'` | Reserved for future transports. Leave at the default. |
254
+
255
+ ## API reference
256
+
257
+ ### `new PolyguardClient(options)`
258
+
259
+ Creates a client and instantiates all underlying API services. See **Configuration** above.
260
+
261
+ ### `client.verify(target?, rawJwt?) → Promise<string | object>`
262
+
263
+ Starts the verification flow.
264
+
265
+ - `target` (string, optional) — DOM element id to render the QR into. Omit for modal mode.
266
+ - `rawJwt` (boolean, optional) — when `true`, resolve with the full response object instead of the JWT string.
267
+
268
+ ### `client.require(expectedProofs, target?) → Promise<boolean>`
269
+
270
+ Runs `verify()` and returns `true` only if every key in `expectedProofs` matches the decoded JWT payload exactly. Returns `false` on cancellation, server errors, or any mismatch. Never throws.
271
+
272
+ ### Browser global
273
+
274
+ When loaded via `<script>`, the SDK is exposed as:
275
+
276
+ - `window.Polyguard.Client` — the `PolyguardClient` constructor
277
+ - `window.Polyguard.ApiClient` — the underlying API client class
278
+ - `window.Polyguard.<ServiceName>Api` — every generated API service (e.g. `TicketApi`)
279
+
280
+ ## Verifying JWTs on your backend
281
+
282
+ The SDK's job ends when it hands you a JWT. To trust the contents you must verify the signature using Polyguard's public key. A minimal Node example:
283
+
284
+ ```js
285
+ import jwt from 'jsonwebtoken';
286
+
287
+ const POLYGUARD_PUBKEY = process.env.POLYGUARD_PUBKEY;
288
+
289
+ const payload = jwt.verify(tokenFromBrowser, POLYGUARD_PUBKEY, {
290
+ algorithms: ['RS256'],
291
+ });
292
+
293
+ // payload['Full Name'], payload.iat, payload.exp, ...
294
+ ```
295
+
296
+ See the [Polyguard documentation](https://polyguard.ai/docs) for the canonical key distribution and claim list.
297
+
298
+ ## Module entry points
299
+
300
+ | Import | Use it for |
301
+ |---|---|
302
+ | `@polyguard/sdk` | Browser code — `PolyguardClient`, generated API services. |
303
+ | `@polyguard/sdk/server` | Server code — `createProxyHandler`. Has no browser-only dependencies. |
304
+
305
+ The CDN bundle (`https://cdn.polyguard.ai/sdk/<version>/sdk.js`) is browser-only and exposes the `window.Polyguard` global.
306
+
307
+ ## Versioning and distribution
308
+
309
+ `@polyguard/sdk` follows semantic versioning. Each release ships in two places:
310
+
311
+ - **npm** — `@polyguard/sdk` (ESM)
312
+ - **CDN** — `https://cdn.polyguard.ai/sdk/<version>/sdk.js` (IIFE) and `https://cdn.polyguard.ai/sdk/latest/sdk.js` (latest)
313
+
314
+ The `latest/` path tracks the most recent published tag. For production deployments, pin to a specific version.
315
+
316
+ ## Browser support
317
+
318
+ Modern evergreen browsers (Chrome, Edge, Firefox, Safari). The SDK relies on `WebSocket`, `fetch`, `Promise`, and ES2020 syntax. Internet Explorer is not supported.
319
+
320
+ ## Support
321
+
322
+ - Issues and feature requests: <https://github.com/polyguard-ai/web-sdk/issues>
323
+ - Product documentation: <[https://dev.polyguard.ai/documentation](https://dev.polyguard.ai/documentation)>
324
+ - Email: <support@polyguard.ai>
325
+
326
+ ## License
327
+
328
+ Apache License 2.0 © Polyguard, Inc. See [LICENSE](./LICENSE) for the full text.
package/dist/sdk.esm.js CHANGED
@@ -10920,8 +10920,9 @@ function renderQRCode(qrDiv, qrUrl) {
10920
10920
  }
10921
10921
 
10922
10922
  // src/ticketService.js
10923
- async function fetchTicket({ apiServer, appId, link_uuid, requiredProofs, scanType }) {
10924
- const ticketUrl = link_uuid ? `https://${apiServer}/v2/ticket/${appId}/${link_uuid}` : `https://${apiServer}/v2/ticket/${appId}`;
10923
+ async function fetchTicket({ apiServer, proxyUrl, appId, link_uuid, requiredProofs, scanType }) {
10924
+ const base = proxyUrl ? proxyUrl.replace(/\/$/, "") : `https://${apiServer}`;
10925
+ const ticketUrl = link_uuid ? `${base}/v2/ticket/${appId}/${link_uuid}` : `${base}/v2/ticket/${appId}`;
10925
10926
  const ticketRes = await fetch(ticketUrl, {
10926
10927
  method: "POST",
10927
10928
  headers: { "Content-Type": "application/json" },
@@ -11033,15 +11034,24 @@ function handleWebSocketMessage(event, ctx) {
11033
11034
  var PolyguardWebsocketClientImpl = class {
11034
11035
  constructor(params = {}) {
11035
11036
  this.apiClient = new ApiClient_default();
11036
- const { apiKey, baseUrl, appId, apiServer, requiredProofs = ["Full Name"], scanType = "single", redirectUrl, callbackUrl, cookieName, link_uuid, sidebarUrl } = params;
11037
+ const { apiKey, baseUrl, appId, apiServer, proxyUrl, iKnowThisIsInsecure, requiredProofs = ["Full Name"], scanType = "single", redirectUrl, callbackUrl, cookieName, link_uuid, sidebarUrl } = params;
11037
11038
  this.sidebarUrl = sidebarUrl;
11038
- if (baseUrl) {
11039
+ if (apiKey && typeof window !== "undefined" && iKnowThisIsInsecure !== true) {
11040
+ throw new Error(
11041
+ "apiKey is forbidden in browser code; use proxyUrl with a server-side proxy (see @polyguard/sdk/server). Set iKnowThisIsInsecure: true to bypass."
11042
+ );
11043
+ }
11044
+ if (proxyUrl) {
11045
+ this.apiClient.basePath = proxyUrl.replace(/\/$/, "");
11046
+ } else if (baseUrl) {
11039
11047
  this.apiClient.basePath = baseUrl;
11040
11048
  }
11041
11049
  if (apiKey) {
11042
11050
  if (this.apiClient.authentications["bearerAuth"]) {
11043
11051
  this.apiClient.authentications["bearerAuth"].apiKey = apiKey;
11044
11052
  }
11053
+ this.apiClient.defaultHeaders = this.apiClient.defaultHeaders || {};
11054
+ this.apiClient.defaultHeaders["x-pg-api-key"] = apiKey;
11045
11055
  }
11046
11056
  for (const key in src_exports) {
11047
11057
  if (key.endsWith("Api")) {
@@ -11052,6 +11062,7 @@ var PolyguardWebsocketClientImpl = class {
11052
11062
  this.integrationType = "websocket";
11053
11063
  this.appId = appId;
11054
11064
  this.apiServer = apiServer;
11065
+ this.proxyUrl = proxyUrl;
11055
11066
  this.requiredProofs = requiredProofs;
11056
11067
  this.scanType = scanType;
11057
11068
  this.redirectUrl = redirectUrl;
@@ -11132,6 +11143,7 @@ var PolyguardWebsocketClientImpl = class {
11132
11143
  const wsProtocol = window.location.protocol === "https:" ? "wss" : "ws";
11133
11144
  const newTicket = await fetchTicket({
11134
11145
  apiServer: this.apiServer,
11146
+ proxyUrl: this.proxyUrl,
11135
11147
  appId: this.appId,
11136
11148
  link_uuid: this.link_uuid,
11137
11149
  requiredProofs: this.requiredProofs,
package/dist/sdk.js CHANGED
@@ -10953,8 +10953,9 @@ var Polyguard = (() => {
10953
10953
  }
10954
10954
 
10955
10955
  // src/ticketService.js
10956
- async function fetchTicket({ apiServer, appId, link_uuid, requiredProofs, scanType }) {
10957
- const ticketUrl = link_uuid ? `https://${apiServer}/v2/ticket/${appId}/${link_uuid}` : `https://${apiServer}/v2/ticket/${appId}`;
10956
+ async function fetchTicket({ apiServer, proxyUrl, appId, link_uuid, requiredProofs, scanType }) {
10957
+ const base = proxyUrl ? proxyUrl.replace(/\/$/, "") : `https://${apiServer}`;
10958
+ const ticketUrl = link_uuid ? `${base}/v2/ticket/${appId}/${link_uuid}` : `${base}/v2/ticket/${appId}`;
10958
10959
  const ticketRes = await fetch(ticketUrl, {
10959
10960
  method: "POST",
10960
10961
  headers: { "Content-Type": "application/json" },
@@ -11066,15 +11067,24 @@ var Polyguard = (() => {
11066
11067
  var PolyguardWebsocketClientImpl = class {
11067
11068
  constructor(params = {}) {
11068
11069
  this.apiClient = new ApiClient_default();
11069
- const { apiKey, baseUrl, appId, apiServer, requiredProofs = ["Full Name"], scanType = "single", redirectUrl, callbackUrl, cookieName, link_uuid, sidebarUrl } = params;
11070
+ const { apiKey, baseUrl, appId, apiServer, proxyUrl, iKnowThisIsInsecure, requiredProofs = ["Full Name"], scanType = "single", redirectUrl, callbackUrl, cookieName, link_uuid, sidebarUrl } = params;
11070
11071
  this.sidebarUrl = sidebarUrl;
11071
- if (baseUrl) {
11072
+ if (apiKey && typeof window !== "undefined" && iKnowThisIsInsecure !== true) {
11073
+ throw new Error(
11074
+ "apiKey is forbidden in browser code; use proxyUrl with a server-side proxy (see @polyguard/sdk/server). Set iKnowThisIsInsecure: true to bypass."
11075
+ );
11076
+ }
11077
+ if (proxyUrl) {
11078
+ this.apiClient.basePath = proxyUrl.replace(/\/$/, "");
11079
+ } else if (baseUrl) {
11072
11080
  this.apiClient.basePath = baseUrl;
11073
11081
  }
11074
11082
  if (apiKey) {
11075
11083
  if (this.apiClient.authentications["bearerAuth"]) {
11076
11084
  this.apiClient.authentications["bearerAuth"].apiKey = apiKey;
11077
11085
  }
11086
+ this.apiClient.defaultHeaders = this.apiClient.defaultHeaders || {};
11087
+ this.apiClient.defaultHeaders["x-pg-api-key"] = apiKey;
11078
11088
  }
11079
11089
  for (const key in src_exports) {
11080
11090
  if (key.endsWith("Api")) {
@@ -11085,6 +11095,7 @@ var Polyguard = (() => {
11085
11095
  this.integrationType = "websocket";
11086
11096
  this.appId = appId;
11087
11097
  this.apiServer = apiServer;
11098
+ this.proxyUrl = proxyUrl;
11088
11099
  this.requiredProofs = requiredProofs;
11089
11100
  this.scanType = scanType;
11090
11101
  this.redirectUrl = redirectUrl;
@@ -11165,6 +11176,7 @@ var Polyguard = (() => {
11165
11176
  const wsProtocol = window.location.protocol === "https:" ? "wss" : "ws";
11166
11177
  const newTicket = await fetchTicket({
11167
11178
  apiServer: this.apiServer,
11179
+ proxyUrl: this.proxyUrl,
11168
11180
  appId: this.appId,
11169
11181
  link_uuid: this.link_uuid,
11170
11182
  requiredProofs: this.requiredProofs,