Just recently, I had a similar question and stumbled upon this insightful post: https://medium.com/@atifhussain.nu21/ssl-pinning-in-ionic-cordova-based-applications-ce993adcc07
It appears to offer a practical solution by utilizing the cordova plugin/library 'cordova-plugin-sslcertificatechecker'.
Here is an example code snippet for reference:
import { Injectable, Injector } from '@angular/core';
import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest
} from '@angular/common/http';
import { Network } from '@ionic-native/network';
import { Observable } from 'rxjs/Rx';
import { CONFIG_CONSTANTS } from '../../common/constant/app-config';
import { MESSAGE_CONSTANTS } from '../../common/constant/message';
declare var window: any;
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
helperFunction: HelperFunctions;
constructor(private network: Network, private injector: Injector) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.helperFunction = this.injector.get(HelperFunctions);
let setHeader = {
'ContentType': 'application/json',
'Accept': 'application/json',
'DEVICETYPE': 'MOBILE',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
'dataType': 'json'
};
let Host = CONFIG_CONSTANTS.BASE_URL;
if (this.network.type === 'none') {
this.helperFunction.showAlert(MESSAGE_CONSTANTS.CONNECTION_NOT_WORKING);
return next.handle(request);
}
return this.checkSecurity(`${Host}${request.url}`, request).flatMap((modifiedReq) => {
let newReq = null;
if (modifiedReq['message'] === 'CONNECTION_SECURE') {
newReq = request.clone({
url: `${Host}${request.url}`,
setHeaders: setHeader
});
}
return next.handle(newReq);
});
}
checkSecurity(URL, request) {
return new Observable((observer) => {
window.plugins.sslCertificateChecker.check(
(message) => {
return observer.next({req: request, message: message});
},
(message) => {
return observer.error({req: request, message: message});
},
URL,
CONFIG_CONSTANTS.FINGERPRINT);
});
}
}
I have yet to test this in my own project. Hopefully, this information proves useful. Feel free to share your experience! :)