How in rewrite a java class in nativescript


#1

I am in a similar situation as this: https://gist.github.com/aembleton/889392 and want to by pass ssl checking using this java class:

  HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {            
    @Override public boolean verify(String s, SSLSession sslSession) { 
        return true;            
 } });

but do not have a clue how to do it in nativescript. What is the easiest way to achieve this?


#2

I am seeing this example: https://github.com/NativeScript/NativeScript/issues/4538 of the code implemented using typescript. This appears to be a java class:

class X509TrustManager implements javax.net.ssl.IX509TrustManager{

getAcceptedIssuers(){
    return null;
}
checkClientTrusted(arg0, args1){

}
checkServerTrusted(arg0, args1){
    
}

}

if I were to use this how would I import it?


#3

Here is how you will extend a class / interface from Java.

Unfortunately you can’t access nativescript http client’s native instance, but nativescript-https plugin is an nice example how you can implement your own version of http client


#4

I have already tried this but it does not help as it does not work with self signed certificates as seen here https://github.com/gethuman/nativescript-https/issues/10. Is there another work around?


#5

I think now I understand better, but you already have the exact code that should work from the same Github Issue.

    var trustAllCerts =new javax.net.ssl.TrustManager(new X509TrustManager());
    var myarray = [trustAllCerts]
    let sc = javax.net.ssl.SSLContext.getInstance("TLS");
    sc.init(null, myarray, new java.security.SecureRandom());
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

#6

ok, yes, but where do, I put the java class:

class X509TrustManager implements javax.net.ssl.IX509TrustManager{

getAcceptedIssuers(){
return null;
}
checkClientTrusted(arg0, args1){

}
checkServerTrusted(arg0, args1){

}
}

and how do I call it in nativescript?


#7

You can just paste this code in your app.ts or in your main module.

import { isAndroid } from "platform";

// Typescript definitions (not require if you have installed `tns-platform-declarations`)
declare var java;
declare module javax.net.ssl {
    class IX509TrustManager { }
    class SSLContext {
        static getInstance(arg0);
    }
    class TrustManager {
        constructor(arg0);
    }
    class HttpsURLConnection {
        static setDefaultSSLSocketFactory(arg0);
    }
}

if (isAndroid) {
    class X509TrustManager implements javax.net.ssl.IX509TrustManager {
        getAcceptedIssuers() {
            return null;
        }
        checkClientTrusted(arg0, args1) {
        }
        checkServerTrusted(arg0, args1) {
        }
    }
    var trustAllCerts = new javax.net.ssl.TrustManager(new X509TrustManager());
    var myarray = [trustAllCerts]
    let sc = javax.net.ssl.SSLContext.getInstance("TLS");
    sc.init(null, myarray, new java.security.SecureRandom());
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

#8

thank you so much for your quick response. i will give it a try.