Skip to content

Commit 05191a8

Browse files
author
Joonas Kannisto
committed
Add option to allow SSL errors
1 parent 464a626 commit 05191a8

File tree

5 files changed

+131
-73
lines changed

5 files changed

+131
-73
lines changed

src/http.android.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ export function clearCookies() {
496496
com.klippa.NativeScriptHTTP.Async.Http.ClearCookies();
497497
}
498498

499+
export function allowSslErrors(allow: boolean) {
500+
com.klippa.NativeScriptHTTP.Async.Http.AllowSslErrors(allow);
501+
}
502+
499503
export function setUserAgent(userAgent?: string) {
500504
customUserAgent = userAgent;
501505
}

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ export declare function setConcurrencyLimits(maxRequests: number, maxRequestsPer
112112
*/
113113
export declare function clearCookies(): void;
114114

115+
/**
116+
* Allow SSL errors and self-signed certificates
117+
* @param allow true/false
118+
*/
119+
export declare function allowSslErrors(allow: boolean): void;
120+
115121
/**
116122
* Set a global user agent.
117123
* @param userAgent The new user agent. Set to null to use the default again.

src/package.json

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
11
{
2-
"name": "@klippa/nativescript-http",
3-
"version": "3.0.4",
4-
"description": "The best way to do HTTP requests in NativeScript, a drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data support and certificate pinning",
5-
"main": "http",
6-
"typings": "index.d.ts",
7-
"nativescript": {
8-
"platforms": {
9-
"android": "6.5.0",
10-
"ios": "6.5.0"
11-
}
12-
},
13-
"repository": {
14-
"type": "git",
15-
"url": "https://github.com/klippa-app/nativescript-http.git"
16-
},
17-
"scripts": {
18-
"tsc": "npm i && ts-patch install && tsc",
19-
"build": "npm i && ts-patch install && npm run ngcc && npm run ngc",
20-
"build.old": "npm i && ts-patch install && npm run ngcc && npm run ngc",
21-
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\" --exclude \"**/typings/**\"",
22-
"ci.tslint": "npm i && ts-patch install && tslint '**/*.ts' --config '../tslint.json' --exclude '**/node_modules/**' --exclude '**/platforms/**' --exclude '**/typings/**'",
23-
"demo.ios": "npm i && ts-patch install && cd ../demo && ns run ios",
24-
"demo.android": "npm i && ts-patch install && cd ../demo && ns run android",
25-
"demo.reset": "cd ../demo && npx rimraf -- hooks node_modules platforms package-lock.json",
26-
"demo-angular.ios": "npm i && ts-patch install && cd ../demo-angular && ns run ios",
27-
"demo-angular.android": "npm i && ts-patch install && cd ../demo-angular && ns run android",
28-
"demo-angular.reset": "cd ../demo-angular && npx rimraf -- hooks node_modules platforms package-lock.json",
29-
"plugin.prepare": "npm run build && cd ../demo && ns plugin remove @klippa/nativescript-http && ns plugin add ../src && cd ../demo-angular && ns plugin remove @klippa/nativescript-http && ns plugin add ../src",
30-
"clean": "npm run demo.reset && npm run demo-angular.reset && npx rimraf -- node_modules package-lock.json && npm i",
31-
"ngcc": "node --max-old-space-size=8192 ./node_modules/.bin/ngcc -p es2015",
32-
"ngc": "node --max-old-space-size=8192 ./node_modules/.bin/ngc"
33-
},
34-
"keywords": [
35-
"NativeScript",
36-
"JavaScript",
37-
"Android",
38-
"iOS"
39-
],
40-
"author": {
41-
"name": "Jeroen Bobbeldijk",
42-
"email": "jeroen@klippa.com"
43-
},
44-
"bugs": {
45-
"url": "https://github.com/klippa-app/nativescript-http/issues"
46-
},
47-
"license": "MIT",
48-
"homepage": "https://github.com/klippa-app/nativescript-http",
49-
"devDependencies": {
50-
"@angular/common": "~14.0.0",
51-
"@angular/compiler": "~14.0.0",
52-
"@angular/compiler-cli": "~14.0.0",
53-
"@angular/core": "~14.0.0",
54-
"@nativescript/angular": "~14.0.0",
55-
"@nativescript/core": "~8.2.0",
56-
"@nativescript/types": "~8.2.0",
57-
"@nativescript/webpack": "~5.0.6",
58-
"@angular/router": "~14.0.0",
59-
"@angular/animations": "~14.0.0",
60-
"@angular/forms": "~14.0.0",
61-
"@angular/platform-browser": "~14.0.0",
62-
"prompt": "^1.0.0",
63-
"rimraf": "^2.6.3",
64-
"rxjs": "~7.5.0",
65-
"semver": "^5.6.0",
66-
"ts-patch": "^1.3.0",
67-
"tslint": "6.1.3",
68-
"typescript": "~4.7.0",
69-
"zone.js": "~0.11.5"
70-
},
71-
"dependencies": {
72-
"ts-node": "9.1.1"
73-
},
74-
"bootstrapper": "nativescript-plugin-seed"
2+
"name": "@klippa/nativescript-http",
3+
"version": "3.0.5",
4+
"description": "The best way to do HTTP requests in NativeScript, a drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data support and certificate pinning",
5+
"main": "http",
6+
"typings": "index.d.ts",
7+
"nativescript": {
8+
"platforms": {
9+
"android": "6.5.0",
10+
"ios": "6.5.0"
11+
}
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/klippa-app/nativescript-http.git"
16+
},
17+
"scripts": {
18+
"tsc": "npm i && ts-patch install && tsc",
19+
"build": "npm i && ts-patch install && npm run ngcc && npm run ngc",
20+
"build.old": "npm i && ts-patch install && npm run ngcc && npm run ngc",
21+
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\" --exclude \"**/typings/**\"",
22+
"ci.tslint": "npm i && ts-patch install && tslint '**/*.ts' --config '../tslint.json' --exclude '**/node_modules/**' --exclude '**/platforms/**' --exclude '**/typings/**'",
23+
"demo.ios": "npm i && ts-patch install && cd ../demo && ns run ios",
24+
"demo.android": "npm i && ts-patch install && cd ../demo && ns run android",
25+
"demo.reset": "cd ../demo && npx rimraf -- hooks node_modules platforms package-lock.json",
26+
"demo-angular.ios": "npm i && ts-patch install && cd ../demo-angular && ns run ios",
27+
"demo-angular.android": "npm i && ts-patch install && cd ../demo-angular && ns run android",
28+
"demo-angular.reset": "cd ../demo-angular && npx rimraf -- hooks node_modules platforms package-lock.json",
29+
"plugin.prepare": "npm run build && cd ../demo && ns plugin remove @klippa/nativescript-http && ns plugin add ../src && cd ../demo-angular && ns plugin remove @klippa/nativescript-http && ns plugin add ../src",
30+
"clean": "npm run demo.reset && npm run demo-angular.reset && npx rimraf -- node_modules package-lock.json && npm i",
31+
"ngcc": "node --max-old-space-size=8192 ./node_modules/.bin/ngcc -p es2015",
32+
"ngc": "node --max-old-space-size=8192 ./node_modules/.bin/ngc"
33+
},
34+
"keywords": [
35+
"NativeScript",
36+
"JavaScript",
37+
"Android",
38+
"iOS"
39+
],
40+
"author": {
41+
"name": "Jeroen Bobbeldijk",
42+
"email": "jeroen@klippa.com"
43+
},
44+
"bugs": {
45+
"url": "https://github.com/klippa-app/nativescript-http/issues"
46+
},
47+
"license": "MIT",
48+
"homepage": "https://github.com/klippa-app/nativescript-http",
49+
"devDependencies": {
50+
"@angular/common": "~14.0.0",
51+
"@angular/compiler": "~14.0.0",
52+
"@angular/compiler-cli": "~14.0.0",
53+
"@angular/core": "~14.0.0",
54+
"@nativescript/angular": "~14.0.0",
55+
"@nativescript/core": "~8.2.0",
56+
"@nativescript/types": "~8.2.0",
57+
"@nativescript/webpack": "~5.0.6",
58+
"@angular/router": "~14.0.0",
59+
"@angular/animations": "~14.0.0",
60+
"@angular/forms": "~14.0.0",
61+
"@angular/platform-browser": "~14.0.0",
62+
"prompt": "^1.0.0",
63+
"rimraf": "^2.6.3",
64+
"rxjs": "~7.5.0",
65+
"semver": "^5.6.0",
66+
"ts-patch": "^1.3.0",
67+
"tslint": "6.1.3",
68+
"typescript": "~4.7.0",
69+
"zone.js": "~0.11.5"
70+
},
71+
"dependencies": {
72+
"ts-node": "9.1.1"
73+
},
74+
"bootstrapper": "nativescript-plugin-seed"
7575
}

src/platforms/android/java/com/klippa/NativeScriptHTTP/Async.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,55 @@ public static class Http {
8989
private static MemoryCookieJar cookieJar;
9090
private static CertificatePinner.Builder certificatePinnerBuilder;
9191
private static ImageParseMethod imageParseMethod = ImageParseMethod.CONTENTTYPE;
92+
private static boolean allowSslErrors = false;
9293

9394
public static void InitClient() {
9495
if (cookieJar == null) {
9596
cookieJar = new MemoryCookieJar();
9697
}
9798

9899
if (client == null) {
100+
if (allowSslErrors) {
101+
// Allow all ssl errors
102+
try {
103+
javax.net.ssl.TrustManager TRUST_ALL_CERTS = new javax.net.ssl.X509TrustManager() {
104+
@Override
105+
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
106+
}
107+
108+
@Override
109+
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
110+
}
111+
112+
@Override
113+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
114+
return new java.security.cert.X509Certificate[] {};
115+
}
116+
};
117+
118+
javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("SSL");
119+
sslContext.init(null, new javax.net.ssl.TrustManager[] { TRUST_ALL_CERTS }, new java.security.SecureRandom());
120+
client = new OkHttpClient.Builder()
121+
.writeTimeout(60, TimeUnit.SECONDS)
122+
.readTimeout(60, TimeUnit.SECONDS)
123+
.connectTimeout(60, TimeUnit.SECONDS)
124+
.sslSocketFactory(sslContext.getSocketFactory(), (javax.net.ssl.X509TrustManager) TRUST_ALL_CERTS)
125+
.hostnameVerifier(new javax.net.ssl.HostnameVerifier() {
126+
@Override
127+
public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
128+
return true;
129+
}
130+
})
131+
.cookieJar(cookieJar)
132+
.build();
133+
} catch (java.security.KeyManagementException e) {
134+
e.printStackTrace();
135+
} catch (java.security.NoSuchAlgorithmException e) {
136+
e.printStackTrace();
137+
}
138+
return;
139+
}
140+
99141
client = new OkHttpClient.Builder()
100142
.writeTimeout(60, TimeUnit.SECONDS)
101143
.readTimeout(60, TimeUnit.SECONDS)
@@ -188,6 +230,11 @@ public static void ClearCookies() {
188230
}
189231
}
190232

233+
public static void AllowSslErrors(boolean allow) {
234+
allowSslErrors = allow;
235+
InitClient();
236+
}
237+
191238
public static void SetImageParseMethod(ImageParseMethod newImageParseMethod) {
192239
imageParseMethod = newImageParseMethod;
193240
}

src/typings/android.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ declare module com {
2525
public static class: java.lang.Class<com.klippa.NativeScriptHTTP.Async.Http>;
2626
public static SetConcurrencyLimits(param0: number, param1: number): void;
2727
public static ClearCookies(): void;
28+
public static AllowSslErrors(param0: boolean): void;
2829
public static MakeRequest(param0: com.klippa.NativeScriptHTTP.Async.Http.RequestOptions, param1: com.klippa.NativeScriptHTTP.Async.CompleteCallback, param2: any): void;
2930
public constructor();
3031
public static InitClient(): void;

0 commit comments

Comments
 (0)