siarashield_workspace 0.0.51 → 0.0.52
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 +188 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
# `siarashield_workspace`
|
|
1
|
+
# `siarashield_workspace`
|
|
2
2
|
|
|
3
|
-
Angular
|
|
3
|
+
Angular integration for CyberSiara **SiaraShield** captcha.
|
|
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`
|
|
4
12
|
|
|
5
13
|
## Install
|
|
6
14
|
|
|
@@ -10,9 +18,7 @@ npm i siarashield_workspace
|
|
|
10
18
|
|
|
11
19
|
## Quick setup (recommended)
|
|
12
20
|
|
|
13
|
-
|
|
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`
|
|
21
|
+
This is the easiest and most reliable flow. It applies all CSP wiring automatically.
|
|
16
22
|
|
|
17
23
|
```bash
|
|
18
24
|
ng add siarashield_workspace
|
|
@@ -24,11 +30,15 @@ If your `angular.json` does not have a `defaultProject`, pass the project name:
|
|
|
24
30
|
ng add siarashield_workspace --project <yourProjectName>
|
|
25
31
|
```
|
|
26
32
|
|
|
33
|
+
What `ng add` does:
|
|
34
|
+
- Updates `src/index.html` (adds `meta[name="csp-nonce"]` + CSP policy)
|
|
35
|
+
- Updates `src/app/app.config.ts` (standalone) **or** `src/main.ts` (fallback) to provide Angular `CSP_NONCE`
|
|
36
|
+
|
|
27
37
|
## Manual setup (if you don’t want `ng add`)
|
|
28
38
|
|
|
29
39
|
### Step 1: Add CSP meta tags in `src/index.html`
|
|
30
40
|
|
|
31
|
-
Copy/paste
|
|
41
|
+
Copy/paste:
|
|
32
42
|
|
|
33
43
|
```html
|
|
34
44
|
<meta name="csp-nonce" content="REPLACE_WITH_NONCE" />
|
|
@@ -46,7 +56,9 @@ img-src 'self' data: https://embed.mycybersiara.com https://embedcdn.mycybersiar
|
|
|
46
56
|
">
|
|
47
57
|
```
|
|
48
58
|
|
|
49
|
-
### Step 2: Provide Angular `CSP_NONCE`
|
|
59
|
+
### Step 2: Provide Angular `CSP_NONCE` (recommended)
|
|
60
|
+
|
|
61
|
+
In `src/app/app.config.ts`:
|
|
50
62
|
|
|
51
63
|
```ts
|
|
52
64
|
import { ApplicationConfig, CSP_NONCE } from '@angular/core';
|
|
@@ -63,25 +75,182 @@ export const appConfig: ApplicationConfig = {
|
|
|
63
75
|
};
|
|
64
76
|
```
|
|
65
77
|
|
|
66
|
-
### Step 3:
|
|
78
|
+
### Step 3: Remove old `cspNonce` plumbing (recommended)
|
|
79
|
+
|
|
80
|
+
You normally **do not need** to pass `cspNonce` from your component.
|
|
67
81
|
|
|
68
|
-
Remove these
|
|
82
|
+
Remove these if present:
|
|
69
83
|
- `[cspNonce]="cspNonce"` in the template
|
|
70
|
-
- `protected readonly cspNonce =
|
|
84
|
+
- `protected readonly cspNonce = ...` in the component
|
|
85
|
+
- `(window as any).__cspNonce` usage
|
|
71
86
|
|
|
72
|
-
##
|
|
87
|
+
## Quick start (component integration)
|
|
73
88
|
|
|
74
|
-
|
|
89
|
+
### 1) Put public key in environment
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
export const environment = {
|
|
93
|
+
siaraShieldPublicKey: 'YOUR-PUBLIC-KEY',
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 2) Render component + submit button in template
|
|
75
98
|
|
|
76
99
|
```html
|
|
77
100
|
<siara-shield
|
|
78
|
-
[publicKey]="environment.
|
|
79
|
-
(token)="onCaptchaToken($event)"
|
|
80
|
-
|
|
101
|
+
[publicKey]="environment.siaraShieldPublicKey"
|
|
102
|
+
(token)="onCaptchaToken($event)"
|
|
103
|
+
></siara-shield>
|
|
104
|
+
|
|
105
|
+
<button type="submit" class="CaptchaSubmit" (click)="onSubmit()">Submit</button>
|
|
81
106
|
```
|
|
82
107
|
|
|
83
|
-
|
|
108
|
+
### 3) Check captcha before API submit
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { Component, ViewChild } from '@angular/core';
|
|
112
|
+
import { environment } from '../environments/environment';
|
|
113
|
+
import { SiaraShieldComponent } from 'siarashield_workspace';
|
|
114
|
+
|
|
115
|
+
@Component({
|
|
116
|
+
selector: 'app-form',
|
|
117
|
+
standalone: true,
|
|
118
|
+
imports: [SiaraShieldComponent],
|
|
119
|
+
templateUrl: './form.component.html',
|
|
120
|
+
})
|
|
121
|
+
export class FormComponent {
|
|
122
|
+
protected readonly environment = environment;
|
|
123
|
+
|
|
124
|
+
private isSubmitting = false;
|
|
125
|
+
@ViewChild(SiaraShieldComponent) private readonly captcha?: SiaraShieldComponent;
|
|
126
|
+
|
|
127
|
+
onCaptchaToken(token: string) {
|
|
128
|
+
console.log('SiaraShield token:', token);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async onSubmit() {
|
|
132
|
+
if (this.isSubmitting) return;
|
|
133
|
+
this.isSubmitting = true;
|
|
134
|
+
try {
|
|
135
|
+
const ok = await this.captcha?.checkCaptchaAsync();
|
|
136
|
+
if (!ok) return;
|
|
137
|
+
|
|
138
|
+
// Call your backend API only after captcha success.
|
|
139
|
+
alert('Form submitted successfully');
|
|
140
|
+
} finally {
|
|
141
|
+
this.isSubmitting = false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Legacy/optional: passing `cspNonce` explicitly
|
|
148
|
+
|
|
149
|
+
If you already have a server-generated nonce available in code, you can still pass it:
|
|
150
|
+
- Component input: `[cspNonce]="myNonce"`
|
|
151
|
+
- Function API: `initSiaraShield({ ..., cspNonce: myNonce })`
|
|
152
|
+
|
|
153
|
+
If omitted, the plugin auto-resolves the nonce from `meta[name="csp-nonce"]` or an existing `script[nonce]`.
|
|
154
|
+
|
|
155
|
+
## Function API (alternative)
|
|
156
|
+
|
|
157
|
+
Template:
|
|
158
|
+
|
|
159
|
+
```html
|
|
160
|
+
<div class="SiaraShield"></div>
|
|
161
|
+
<button type="submit" class="CaptchaSubmit" (click)="onSubmit()">Submit</button>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
TypeScript:
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
import { environment } from '../environments/environment';
|
|
168
|
+
import { initSiaraShield, checkSiaraShieldCaptcha } from 'siarashield_workspace';
|
|
169
|
+
|
|
170
|
+
export class FormComponent {
|
|
171
|
+
ngOnInit() {
|
|
172
|
+
void this.initializeCaptcha();
|
|
173
|
+
}
|
|
84
174
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
175
|
+
private async initializeCaptcha() {
|
|
176
|
+
await initSiaraShield({
|
|
177
|
+
publicKey: environment.siaraShieldPublicKey,
|
|
178
|
+
// cspNonce: 'optional-explicit-nonce',
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
onSubmit() {
|
|
183
|
+
const result = checkSiaraShieldCaptcha();
|
|
184
|
+
if (!result.ok) return;
|
|
185
|
+
|
|
186
|
+
console.log(result.token);
|
|
187
|
+
// API call here
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Key handling (required)
|
|
193
|
+
|
|
194
|
+
- **Frontend (Angular): public key only**
|
|
195
|
+
- **Backend (.env or secret store): private key only**
|
|
196
|
+
|
|
197
|
+
Backend example:
|
|
198
|
+
|
|
199
|
+
```dotenv
|
|
200
|
+
SIARASHIELD_PRIVATE_KEY=YOUR-PRIVATE-KEY
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Never place the private key in Angular code or browser-accessible files.
|
|
204
|
+
|
|
205
|
+
Get keys from [mycybersiara.com](https://mycybersiara.com).
|
|
206
|
+
|
|
207
|
+
## CSP guide
|
|
208
|
+
|
|
209
|
+
If your app has no strict CSP, default integration works without extra setup.
|
|
210
|
+
|
|
211
|
+
For strict CSP, use a **server-generated nonce per request**.
|
|
212
|
+
|
|
213
|
+
### Recommended policy example
|
|
214
|
+
|
|
215
|
+
```http
|
|
216
|
+
default-src 'self';
|
|
217
|
+
script-src 'self' 'nonce-<dynamic>' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
|
|
218
|
+
script-src-elem 'self' 'nonce-<dynamic>' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
|
|
219
|
+
script-src-attr 'self' 'unsafe-inline';
|
|
220
|
+
connect-src 'self' https://embed.mycybersiara.com https://embedcdn.mycybersiara.com;
|
|
221
|
+
style-src 'self' 'nonce-<dynamic>' https://embed.mycybersiara.com https://mycybersiara.com https://fonts.googleapis.com https://cdnjs.cloudflare.com;
|
|
222
|
+
style-src-elem 'self' 'nonce-<dynamic>' https://embed.mycybersiara.com https://mycybersiara.com https://fonts.googleapis.com https://cdnjs.cloudflare.com;
|
|
223
|
+
style-src-attr 'self' 'unsafe-inline';
|
|
224
|
+
font-src 'self' https://fonts.gstatic.com https://mycybersiara.com https://cdnjs.cloudflare.com data:;
|
|
225
|
+
img-src 'self' data: https://embed.mycybersiara.com https://embedcdn.mycybersiara.com https://mycybersiara.com;
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Script loading behavior
|
|
229
|
+
|
|
230
|
+
By default (`loadJQuery: true`) the package loads:
|
|
231
|
+
- `https://embedcdn.mycybersiara.com/capcha-temple/js/jquery.min.js` (fallback when jQuery not already present)
|
|
232
|
+
- `https://embedcdn.mycybersiara.com/CaptchaFormate/CaptchaResources.js`
|
|
233
|
+
- `https://embed.mycybersiara.com/CaptchaFormate/SiaraShield_Validation.js`
|
|
234
|
+
|
|
235
|
+
Most users should **not** add these script tags manually.
|
|
236
|
+
|
|
237
|
+
If your app already has jQuery, set `[loadJQuery]="false"` (or `loadJQuery: false` in function API).
|
|
238
|
+
|
|
239
|
+
## Quick troubleshooting
|
|
240
|
+
|
|
241
|
+
- Captcha not visible:
|
|
242
|
+
- Confirm component is rendered and not hidden.
|
|
243
|
+
- Confirm submit button has `class="CaptchaSubmit"`.
|
|
244
|
+
- For function API, confirm `<div class="SiaraShield"></div>` exists.
|
|
245
|
+
- `CheckCaptcha` not available:
|
|
246
|
+
- Ensure initialization completed.
|
|
247
|
+
- Ensure CSP allows required hosts and nonce.
|
|
248
|
+
- Token not available immediately:
|
|
249
|
+
- Use async check methods (`checkCaptchaAsync` / `checkSiaraShieldCaptchaAsync`).
|
|
250
|
+
|
|
251
|
+
## Build and pack (maintainers)
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
npm run build:lib
|
|
255
|
+
npm run pack:lib
|
|
256
|
+
```
|