Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,20 @@ certificatePinningAdd("mydomain.com", ["DCU5TkA8n3L8+QM7dyTjfRlxWibigF+1cxMzRhlJ
certificatePinningClear();
```

### Allow SSL errors and self-signed certificates

You can allow SSL errors and self-signed certificates if you want. This only works on android devices.

```typescript
import { allowSslErrors } from "@klippa/nativescript-http";

/**
* Allow SSL errors and self-signed certificates
* @param allow true/false
*/
allowSslErrors(true);
```

## Roadmap
* Cache control
* Allowing self signed certificates (WIP in feature/self-signed)
Expand Down
4 changes: 4 additions & 0 deletions src/http.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ export function clearCookies() {
com.klippa.NativeScriptHTTP.Async.Http.ClearCookies();
}

export function allowSslErrors(allow: boolean) {
com.klippa.NativeScriptHTTP.Async.Http.AllowSslErrors(allow);
}

export function setUserAgent(userAgent?: string) {
customUserAgent = userAgent;
}
Expand Down
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export declare function setConcurrencyLimits(maxRequests: number, maxRequestsPer
*/
export declare function clearCookies(): void;

/**
* Allow SSL errors and self-signed certificates
* ** Only Android **
* @param allow true/false
*/
export declare function allowSslErrors(allow: boolean): void;

/**
* Set a global user agent.
* @param userAgent The new user agent. Set to null to use the default again.
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@klippa/nativescript-http",
"version": "3.0.4",
"version": "3.0.5",
"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",
"main": "http",
"typings": "index.d.ts",
Expand Down
48 changes: 48 additions & 0 deletions src/platforms/android/java/com/klippa/NativeScriptHTTP/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,55 @@ public static class Http {
private static MemoryCookieJar cookieJar;
private static CertificatePinner.Builder certificatePinnerBuilder;
private static ImageParseMethod imageParseMethod = ImageParseMethod.CONTENTTYPE;
private static boolean allowSslErrors = false;

public static void InitClient() {
if (cookieJar == null) {
cookieJar = new MemoryCookieJar();
}

if (client == null) {
if (allowSslErrors) {
// Allow all ssl errors
try {
javax.net.ssl.TrustManager TRUST_ALL_CERTS = new javax.net.ssl.X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
};

javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("SSL");
sslContext.init(null, new javax.net.ssl.TrustManager[] { TRUST_ALL_CERTS }, new java.security.SecureRandom());
client = new OkHttpClient.Builder()
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.sslSocketFactory(sslContext.getSocketFactory(), (javax.net.ssl.X509TrustManager) TRUST_ALL_CERTS)
.hostnameVerifier(new javax.net.ssl.HostnameVerifier() {
@Override
public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
return true;
}
})
.cookieJar(cookieJar)
.build();
} catch (java.security.KeyManagementException e) {
e.printStackTrace();
} catch (java.security.NoSuchAlgorithmException e) {
e.printStackTrace();
}
return;
}

client = new OkHttpClient.Builder()
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
Expand Down Expand Up @@ -188,6 +230,12 @@ public static void ClearCookies() {
}
}

public static void AllowSslErrors(boolean allow) {
client = null;
allowSslErrors = allow;
InitClient();
}

public static void SetImageParseMethod(ImageParseMethod newImageParseMethod) {
imageParseMethod = newImageParseMethod;
}
Expand Down
1 change: 1 addition & 0 deletions src/typings/android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ declare module com {
public static class: java.lang.Class<com.klippa.NativeScriptHTTP.Async.Http>;
public static SetConcurrencyLimits(param0: number, param1: number): void;
public static ClearCookies(): void;
public static AllowSslErrors(param0: boolean): void;
public static MakeRequest(param0: com.klippa.NativeScriptHTTP.Async.Http.RequestOptions, param1: com.klippa.NativeScriptHTTP.Async.CompleteCallback, param2: any): void;
public constructor();
public static InitClient(): void;
Expand Down