@safez/ngx-safez
v1.0.0
Published
Enhance your Angular applications with unparalleled cybersecurity by integrating the Angular Safez Interceptor, designed to work flawlessly from Angular 8 to the latest version. This advanced interceptor, powered by safez-cryptx, employs AES-CBC symmetric
Downloads
6
Readme
Overview of ngx-safez
ngx-safez is an interceptor library for Angular's HttpClient, designed to seamlessly encrypt and decrypt HTTP requests and responses. By applying robust encryption algorithms, it ensures the security of data in transit, providing a crucial layer of defense for web applications against unauthorized access. Integrating directly with Angular, ngx-safez offers developers a straightforward solution to bolster their application's security. It automatically handles the encryption of data before it leaves the client and decrypts incoming data, maintaining the protection of sensitive information throughout the transmission process. ngx-safez stands out for its high configurability, allowing developers to tailor encryption settings to fit the precise security needs of various applications, from those demanding stringent data protection to those requiring basic encryption enhancements.
- Supports a variety of encryption standards to ensure secure data transmission.
- Provides an added layer of security to help mitigate the risks associated with data breaches and cyber threats. Incorporating ngx-safez into Angular applications is simple and enhances security protocols without adding complexity to the development workflow.
Installation
To integrate ngx-safez into your project, use the following command:
npm install @safez/ngx-safez or yarn add @safez/ngx-safez
Setup and Configuration
Configure ngx-safez with your Angular application to encrypt and decrypt HTTP requests and responses effortlessly:
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgxSafezService } from '@safez/ngx-safez';
@NgModule({
declarations: [...],
imports: [...],
providers: [
{
provide: NGX_SAFEZ_CONFIG,
useValue: {enableSafez: true, safezSaavi: true, cryptoType: 'field'}
},
{
provide: HTTP_INTERCEPTORS,
useClass: NgxSafezService,
multi: true,
},
],
})
export class AppModule {}
The optional values for NGX_SAFEZ_CONFIG are crypto type, configurable values are 'full', 'field', 'none'. When safezEnable is true, default value is full. When configured the safez, all your payload will be encrypted
Usage
With ngx-safez configured, all HTTP requests and responses managed by Angular's HttpClient will be automatically encrypted and decrypted.
Customizing Encryption per Request
You can specify the encryption type for individual requests using Angular's HttpHeaders:
import { HttpHeaders } from '@angular/common/http';
const headers = new HttpHeaders({
'x-sz-token': JSON.stringify({ cryptoType: 'full', safezSaavi: 'dummysecretkeyab' })
});
// Use these headers in your HttpClient request
Examples:
- Encrypting the entire request object:
const payload = { name: 'safez', product: 'security' };
const headers = new HttpHeaders({
'x-sz-token': JSON.stringify({ cryptoType: 'full' })
});
this.http.post('http://example.url/api/post', payload, { headers }).subscribe();
// payload will be {encryptedData: 'encrypted string'}
- Bypassing encryption for a request:
const config = { cryptoType: 'none' };
const headers = new HttpHeaders({
'x-sz-token': JSON.stringify(config)
});
this.http.post('http://example.url/api/post', payload, { headers }).subscribe();
- Encrypting specific fields within an object:
const payload = { name: 'safez', product: 'security' };
const config = { cryptoType: 'field' };
const headers = new HttpHeaders({
'x-sz-token': JSON.stringify(config)
});
this.http.post('http://example.url/api/post', payload, { headers }).subscribe();
// The payload will have 'name' and 'product' values encrypted.
Handling Errors
Dealing with encrypted error messages requires diligent error handling, particularly for scenarios involving encrypted server responses. ngx-safez equips your application to intercept, decrypt, and process these error messages securely, ensuring a coherent and secure error handling strategy.
Best Practices for Secure Error Handling
- Encryption Secret Management: Securely manage your encryption secrets, avoiding exposure in client-side code. Utilize secure storage solutions and restrict access to these secrets.
- Optimize Encryption Use: Strategically employ encryption, considering both security needs and performance implications. Utilize HttpHeaders to selectively apply encryption, optimizing your application's performance and security.
Troubleshooting Common Encryption Issues
- Encryption/Decryption Failures: Verify the consistency of encryption keys or secrets across the client and server. Discrepancies can lead to failed decryption and errors.
- Interceptor Configuration: Ensure ngx-safez is correctly set up as an HTTP interceptor and that there are no conflicts with other interceptors in your Angular application.
Frequently Asked Questions (FAQ)
- Can ngx-safez be used in any Angular application? Yes, ngx-safez is designed to integrate seamlessly with Angular's HttpClient, making it suitable for any Angular project.
- How can I exclude specific requests from encryption?
Utilize the
x-sz-token
header with acryptoType
of 'none' to bypass encryption for particular requests, allowing for flexible encryption application.