siarashield_workspace 0.0.47 → 0.0.51

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
@@ -1,242 +1,87 @@
1
- # `siarashield_workspace`
1
+ # `siarashield_workspace` (CyberSiara SiaraShield Angular plugin)
2
2
 
3
- Angular integration for CyberSiara **SiaraShield** captcha.
3
+ Angular wrapper for the CyberSiara SiaraShield captcha embed.
4
4
 
5
- ## Angular compatibility
6
-
7
- - Minimum supported Angular: `16`
8
- - Supported range: `16.x` to `21.x`
9
- - Peer dependencies:
10
- - `@angular/core >=16.0.0 <22.0.0`
11
- - `@angular/common >=16.0.0 <22.0.0`
12
-
13
- ## Quick start (recommended, easiest path)
14
-
15
- Use this flow first. Most users need only these steps.
16
-
17
- 1. Install package:
5
+ ## Install
18
6
 
19
7
  ```bash
20
8
  npm i siarashield_workspace
21
9
  ```
22
10
 
23
- 2. Ensure Angular environment files exist.
24
- - If your project does not have `src/environments`, create:
25
- - `src/environments/environment.ts`
26
- - `src/environments/environment.prod.ts`
27
-
28
- 3. Put only your **public key** in Angular environment:
29
-
30
- ```ts
31
- export const environment = {
32
- siaraShieldPublicKey: 'YOUR-PUBLIC-KEY',
33
- };
34
- ```
35
-
36
- 4. Render component + submit button in template:
11
+ ## Quick setup (recommended)
37
12
 
38
- ```html
39
- <siara-shield
40
- [publicKey]="environment.siaraShieldPublicKey"
41
- [cspNonce]="cspNonce"
42
- (token)="onCaptchaToken($event)"
43
- ></siara-shield>
44
-
45
- <button type="submit" class="CaptchaSubmit" (click)="onSubmit()">Submit</button>
46
- ```
13
+ Run `ng add` to automatically apply the recommended CSP + Angular `CSP_NONCE` wiring into your app:
14
+ - updates `src/index.html` (adds `meta[name="csp-nonce"]` + CSP policy)
15
+ - updates `src/app/app.config.ts` (standalone) or `src/main.ts` (fallback) to provide Angular `CSP_NONCE`
47
16
 
48
- 5. Check captcha before API submit:
49
-
50
- ```ts
51
- import { Component, ViewChild } from '@angular/core';
52
- import { environment } from '../environments/environment';
53
- import { SiaraShieldComponent } from 'siarashield_workspace';
54
-
55
- @Component({
56
- selector: 'app-form',
57
- standalone: true,
58
- imports: [SiaraShieldComponent],
59
- templateUrl: './form.component.html',
60
- })
61
- export class FormComponent {
62
- protected readonly environment = environment;
63
- protected readonly cspNonce = (window as any).__cspNonce as string | undefined;
64
-
65
- private isSubmitting = false;
66
- @ViewChild(SiaraShieldComponent) private readonly captcha?: SiaraShieldComponent;
67
-
68
- onCaptchaToken(token: string) {
69
- console.log('SiaraShield token:', token);
70
- }
71
-
72
- async onSubmit() {
73
- if (this.isSubmitting) return;
74
- this.isSubmitting = true;
75
- try {
76
- const ok = await this.captcha?.checkCaptchaAsync();
77
- if (!ok) return;
78
-
79
- // Call your backend API only after captcha success.
80
- alert('Form submitted successfully');
81
- } finally {
82
- this.isSubmitting = false;
83
- }
84
- }
85
- }
17
+ ```bash
18
+ ng add siarashield_workspace
86
19
  ```
87
20
 
88
- 6. Keep your submit API logic only in the success branch (`ok === true`).
89
-
90
- ## Key handling (required)
21
+ If your `angular.json` does not have a `defaultProject`, pass the project name:
91
22
 
92
- - **Frontend (Angular): public key only**
93
- - **Backend (.env or secret store): private key only**
94
-
95
- Backend example:
96
-
97
- ```dotenv
98
- SIARASHIELD_PRIVATE_KEY=YOUR-PRIVATE-KEY
23
+ ```bash
24
+ ng add siarashield_workspace --project <yourProjectName>
99
25
  ```
100
26
 
101
- Never place the private key in Angular code or browser-accessible files.
27
+ ## Manual setup (if you don’t want `ng add`)
102
28
 
103
- Get keys from [mycybersiara.com](https://mycybersiara.com).
29
+ ### Step 1: Add CSP meta tags in `src/index.html`
104
30
 
105
- ## Integration rules (important)
106
-
107
- - Choose one style per page:
108
- - **Recommended:** `<siara-shield ...>`
109
- - **Alternative:** function API
110
- - Do not mix both styles on the same page.
111
- - Keep submit button class: `CaptchaSubmit`.
112
- - Initialize captcha once per page load.
113
-
114
- ## Function API (alternative)
115
-
116
- Use this only when component integration is not possible.
117
-
118
- Template:
31
+ Copy/paste (and replace `REPLACE_WITH_NONCE` with your real nonce in production):
119
32
 
120
33
  ```html
121
- <div class="SiaraShield"></div>
122
- <button type="submit" class="CaptchaSubmit" (click)="onSubmit()">Submit</button>
123
- ```
124
-
125
- TypeScript:
126
-
127
- ```ts
128
- import { environment } from '../environments/environment';
129
- import { initSiaraShield, checkSiaraShieldCaptcha } from 'siarashield_workspace';
130
-
131
- export class FormComponent {
132
- ngOnInit() {
133
- void this.initializeCaptcha();
134
- }
135
-
136
- private async initializeCaptcha() {
137
- await initSiaraShield({
138
- publicKey: environment.siaraShieldPublicKey,
139
- cspNonce: (window as any).__cspNonce || undefined,
140
- });
141
- }
142
-
143
- onSubmit() {
144
- const result = checkSiaraShieldCaptcha();
145
- if (!result.ok) return;
146
-
147
- console.log(result.token);
148
- // API call here
149
- }
150
- }
151
- ```
152
-
153
- ## CSP guide
154
-
155
- If your app has no strict CSP, default integration works without extra setup.
156
-
157
- If your app uses strict nonce-based CSP:
158
-
159
- - Generate nonce on server for each request.
160
- - Use same nonce in:
161
- - `Content-Security-Policy` header
162
- - script tags loading your Angular app
163
- - `cspNonce` option/input
164
- - Do not generate nonce in browser.
165
-
166
- ### Strict policy example (recommended for security)
167
-
168
- ```http
34
+ <meta name="csp-nonce" content="REPLACE_WITH_NONCE" />
35
+ <meta http-equiv="Content-Security-Policy" content="
169
36
  default-src 'self';
170
- script-src 'self' 'nonce-<dynamic>' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
171
- script-src-elem 'self' 'nonce-<dynamic>' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
37
+ script-src 'self' 'nonce-REPLACE_WITH_NONCE' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
38
+ script-src-elem 'self' 'nonce-REPLACE_WITH_NONCE' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
39
+ script-src-attr 'self' 'unsafe-inline';
172
40
  connect-src 'self' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
173
- style-src 'self' https://embed.mycybersiara.com https://mycybersiara.com https://fonts.googleapis.com https://cdnjs.cloudflare.com;
41
+ style-src 'self' 'nonce-REPLACE_WITH_NONCE' https://embed.mycybersiara.com https://mycybersiara.com https://fonts.googleapis.com https://cdnjs.cloudflare.com;
42
+ style-src-elem 'self' 'nonce-REPLACE_WITH_NONCE' https://embed.mycybersiara.com https://mycybersiara.com https://fonts.googleapis.com https://cdnjs.cloudflare.com;
43
+ style-src-attr 'self' 'unsafe-inline';
174
44
  font-src 'self' https://fonts.gstatic.com https://mycybersiara.com https://cdnjs.cloudflare.com data:;
175
45
  img-src 'self' data: https://embed.mycybersiara.com https://embedcdn.mycybersiara.com https://mycybersiara.com;
46
+ ">
176
47
  ```
177
48
 
178
- ### Compatibility policy (easier integration, weaker security)
179
-
180
- Use only when customer policy allows `'unsafe-inline'`:
49
+ ### Step 2: Provide Angular `CSP_NONCE` in `src/app/app.config.ts`
181
50
 
182
51
  ```ts
183
- import { getSiaraShieldCspPolicy } from 'siarashield_workspace';
184
-
185
- const csp = getSiaraShieldCspPolicy({
186
- includeUnsafeInlineScript: true,
187
- includeUnsafeInlineStyle: true,
188
- });
52
+ import { ApplicationConfig, CSP_NONCE } from '@angular/core';
53
+
54
+ export const appConfig: ApplicationConfig = {
55
+ providers: [
56
+ {
57
+ provide: CSP_NONCE,
58
+ useFactory: () =>
59
+ document.querySelector('meta[name="csp-nonce"]')?.getAttribute('content') ?? null,
60
+ },
61
+ // ...your other providers...
62
+ ],
63
+ };
189
64
  ```
190
65
 
191
- ## Script loading behavior
192
-
193
- By default (`loadJQuery: true`) package loads required vendor resources automatically:
66
+ ### Step 3: Do NOT pass `cspNonce` from your component
194
67
 
195
- - `https://embedcdn.mycybersiara.com/capcha-temple/js/jquery.min.js` (fallback when jQuery not already present)
196
- - `https://embedcdn.mycybersiara.com/CaptchaFormate/CaptchaResources.js`
197
- - `https://embed.mycybersiara.com/CaptchaFormate/SiaraShield_Validation.js`
68
+ Remove these from your app code (if present):
69
+ - `[cspNonce]="cspNonce"` in the template
70
+ - `protected readonly cspNonce = document.querySelector(...)` in the component
198
71
 
199
- Most users should **not** add these script tags manually.
72
+ ## Usage
200
73
 
201
- If your app already has jQuery, set `[loadJQuery]="false"` (or `loadJQuery: false` in function API).
202
-
203
- ## Advanced validation options
204
-
205
- `checkCaptchaAsync(...)` and `checkSiaraShieldCaptchaAsync(...)` support optional tuning:
206
-
207
- - `timeoutMs`
208
- - `pollIntervalMs`
209
- - `beforeCheckDelayMs`
210
- - `retryOnFalseMs` (default `0`)
211
- - `falseResultTokenWaitMs` (default `900`)
212
-
213
- Default behavior is tuned to avoid requiring a second user click when token arrives slightly after first response.
214
-
215
- ## Quick troubleshooting
216
-
217
- - Captcha not visible:
218
- - Confirm component is rendered and not hidden.
219
- - Confirm submit button has `class="CaptchaSubmit"`.
220
- - For function API, confirm `<div class="SiaraShield"></div>` exists.
221
- - `CheckCaptcha` not available:
222
- - Ensure initialization completed.
223
- - Ensure CSP allows required hosts and nonce.
224
- - Token not available immediately:
225
- - Use async check methods (`checkCaptchaAsync` / `checkSiaraShieldCaptchaAsync`).
226
- - CSP errors:
227
- - Confirm same server-generated nonce in header, script tags, and `cspNonce`.
228
-
229
- ## Final checklist
74
+ Template:
230
75
 
231
- - Public key in frontend, private key only in backend.
232
- - Submit button contains `CaptchaSubmit`.
233
- - Captcha check runs before submit API call.
234
- - Only one integration style is used per page.
235
- - CSP nonce is wired correctly for strict CSP deployments.
76
+ ```html
77
+ <siara-shield
78
+ [publicKey]="environment.siaraShield.publicKey"
79
+ (token)="onCaptchaToken($event)">
80
+ </siara-shield>
81
+ ```
236
82
 
237
- ## Build and pack (maintainers)
83
+ ## Production note (important)
238
84
 
239
- ```bash
240
- npm run build:lib
241
- npm run pack:lib
242
- ```
85
+ In production, the nonce must be **generated fresh per page load** and injected into both:
86
+ - `meta[name="csp-nonce"]`
87
+ - CSP policy (`'nonce-<value>'`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "siarashield_workspace",
3
- "version": "0.0.47",
3
+ "version": "0.0.51",
4
4
  "description": "Angular wrapper for CyberSiara SiaraShield captcha embed.",
5
5
  "schematics": "./schematics/collection.json",
6
6
  "ng-add": {
Binary file