@request-shield/angular 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +27 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 karammm
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @request-shield/angular
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Angular `HttpClient` interceptor for [request-shield](https://github.com/karammm/request-shield): every request to your API is signed with a
|
|
4
|
+
non-exportable WebCrypto key, so copying it into cURL/Postman doesn't work.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install @request-shield/angular
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
// app.config.ts
|
|
12
|
+
import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
13
|
+
import { provideRequestShield, requestShieldInterceptor } from '@request-shield/angular';
|
|
14
|
+
|
|
15
|
+
export const appConfig: ApplicationConfig = {
|
|
16
|
+
providers: [
|
|
17
|
+
provideRequestShield({
|
|
18
|
+
apiBase: environment.apiUrl,
|
|
19
|
+
getAttestationToken: () => getTurnstileToken(),
|
|
20
|
+
}),
|
|
21
|
+
provideHttpClient(withInterceptors([requestShieldInterceptor])),
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Your services keep using `HttpClient` unchanged. Requires Angular 16+.
|
|
27
|
+
Your server must verify the signatures: `go get github.com/karammm/request-shield/server/go`.
|
|
28
|
+
[Full documentation](https://github.com/karammm/request-shield#readme).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type HttpInterceptorFn } from '@angular/common/http';
|
|
2
|
+
import { InjectionToken, type EnvironmentProviders } from '@angular/core';
|
|
3
|
+
import { ShieldClient, type ShieldConfig } from '@request-shield/core';
|
|
4
|
+
export * from '@request-shield/core';
|
|
5
|
+
export declare const REQUEST_SHIELD: InjectionToken<ShieldClient>;
|
|
6
|
+
/**
|
|
7
|
+
* app.config.ts:
|
|
8
|
+
* providers: [
|
|
9
|
+
* provideRequestShield({ apiBase: environment.apiUrl, getAttestationToken: () => turnstile.token() }),
|
|
10
|
+
* provideHttpClient(withInterceptors([requestShieldInterceptor])),
|
|
11
|
+
* ]
|
|
12
|
+
*/
|
|
13
|
+
export declare function provideRequestShield(config: ShieldConfig | ShieldClient): EnvironmentProviders;
|
|
14
|
+
export declare const requestShieldInterceptor: HttpInterceptorFn;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACjF,OAAO,EAAE,cAAc,EAAoC,KAAK,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAE5G,OAAO,EAIL,YAAY,EACZ,KAAK,YAAY,EAElB,MAAM,sBAAsB,CAAC;AAE9B,cAAc,sBAAsB,CAAC;AAErC,eAAO,MAAM,cAAc,8BAAqD,CAAC;AAEjF;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,GAAG,YAAY,GAAG,oBAAoB,CAG9F;AAED,eAAO,MAAM,wBAAwB,EAAE,iBAiCtC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { HttpErrorResponse } from '@angular/common/http';
|
|
2
|
+
import { InjectionToken, inject, makeEnvironmentProviders } from '@angular/core';
|
|
3
|
+
import { catchError, defer, switchMap, throwError } from 'rxjs';
|
|
4
|
+
import { isRetryableShieldError, resolveUrl, SHIELD_ERROR_HEADER, ShieldClient, } from '@request-shield/core';
|
|
5
|
+
export * from '@request-shield/core';
|
|
6
|
+
export const REQUEST_SHIELD = new InjectionToken('REQUEST_SHIELD');
|
|
7
|
+
/**
|
|
8
|
+
* app.config.ts:
|
|
9
|
+
* providers: [
|
|
10
|
+
* provideRequestShield({ apiBase: environment.apiUrl, getAttestationToken: () => turnstile.token() }),
|
|
11
|
+
* provideHttpClient(withInterceptors([requestShieldInterceptor])),
|
|
12
|
+
* ]
|
|
13
|
+
*/
|
|
14
|
+
export function provideRequestShield(config) {
|
|
15
|
+
const client = config instanceof ShieldClient ? config : new ShieldClient(config);
|
|
16
|
+
return makeEnvironmentProviders([{ provide: REQUEST_SHIELD, useValue: client }]);
|
|
17
|
+
}
|
|
18
|
+
export const requestShieldInterceptor = (req, next) => {
|
|
19
|
+
const client = inject(REQUEST_SHIELD);
|
|
20
|
+
const url = resolveUrl(req.urlWithParams);
|
|
21
|
+
if (!client.shouldSign(url))
|
|
22
|
+
return next(req);
|
|
23
|
+
// Serialize exactly as Angular would, then send that serialized form so the
|
|
24
|
+
// signed bytes and the wire bytes are identical. Keep Angular's content-type.
|
|
25
|
+
const serialized = req.serializeBody();
|
|
26
|
+
const contentType = req.headers.get('Content-Type') ?? req.detectContentTypeHeader();
|
|
27
|
+
const send = (isRetry) => defer(() => client.signRequest(req.method, url, serialized)).pipe(switchMap((signed) => {
|
|
28
|
+
const setHeaders = { ...signed.headers };
|
|
29
|
+
const ct = contentType ?? signed.contentType;
|
|
30
|
+
if (ct && signed.body !== undefined)
|
|
31
|
+
setHeaders['Content-Type'] = ct;
|
|
32
|
+
return next(req.clone({ setHeaders, body: signed.body ?? null })).pipe(catchError((err) => {
|
|
33
|
+
if (!isRetry &&
|
|
34
|
+
err instanceof HttpErrorResponse &&
|
|
35
|
+
err.status === 401 &&
|
|
36
|
+
isRetryableShieldError(err.headers.get(SHIELD_ERROR_HEADER))) {
|
|
37
|
+
return defer(() => client.invalidate(signed.sessionId)).pipe(switchMap(() => send(true)));
|
|
38
|
+
}
|
|
39
|
+
return throwError(() => err);
|
|
40
|
+
}));
|
|
41
|
+
}));
|
|
42
|
+
return send(false);
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAA0B,MAAM,sBAAsB,CAAC;AACjF,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,wBAAwB,EAA6B,MAAM,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Client interceptor for request-shield.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"angular",
|
|
7
|
+
"http-interceptor",
|
|
8
|
+
"api-security",
|
|
9
|
+
"request-signing"
|
|
10
|
+
],
|
|
11
|
+
"author": "karammm",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://github.com/karammm/request-shield#readme",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/karammm/request-shield.git",
|
|
17
|
+
"directory": "packages/angular"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/karammm/request-shield/issues"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.json"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@request-shield/core": "^0.1.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@angular/common": ">=16",
|
|
45
|
+
"@angular/core": ">=16",
|
|
46
|
+
"rxjs": ">=7"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|