Skip to content

Commit

Permalink
Renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
Joonas Kannisto committed Oct 28, 2024
1 parent 481ccef commit 7b8af78
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 +483,18 @@ certificatePinningAdd("mydomain.com", ["DCU5TkA8n3L8+QM7dyTjfRlxWibigF+1cxMzRhlJ
certificatePinningClear();
```

### Allow SSL errors and self-signed certificates
### Disable SSL validation

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

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

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

## Roadmap
Expand Down
4 changes: 2 additions & 2 deletions src/http.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ export function clearCookies() {
com.klippa.NativeScriptHTTP.Async.Http.ClearCookies();
}

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

export function setUserAgent(userAgent?: string) {
Expand Down
7 changes: 3 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ 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
* Disable SSL validation
* @param disable true/false
*/
export declare function allowSslErrors(allow: boolean): void;
export declare function disableSSLValidation(disable: boolean): void;

/**
* Set a global user agent.
Expand Down
34 changes: 14 additions & 20 deletions src/platforms/android/java/com/klippa/NativeScriptHTTP/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,22 @@ public static class Http {
private static MemoryCookieJar cookieJar;
private static CertificatePinner.Builder certificatePinnerBuilder;
private static ImageParseMethod imageParseMethod = ImageParseMethod.CONTENTTYPE;
private static boolean allowSslErrors = false;
private static boolean disableSslValidation = false;

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

if (client == null) {
if (allowSslErrors) {
// Allow all ssl errors
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.cookieJar(cookieJar);

if (disableSslValidation) {
// Disable ssl validations
try {
javax.net.ssl.TrustManager TRUST_ALL_CERTS = new javax.net.ssl.X509TrustManager() {
@Override
Expand All @@ -117,33 +123,21 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {

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)
builder.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)
.connectTimeout(60, TimeUnit.SECONDS)
.cookieJar(cookieJar)
.build();
client = builder.build();
}
}

Expand Down Expand Up @@ -230,9 +224,9 @@ public static void ClearCookies() {
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/typings/android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +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 DisableSSLValidation(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

0 comments on commit 7b8af78

Please sign in to comment.