Search in sources :

Example 1 with KeyChain

use of android.security.KeyChain in project talk-android by nextcloud.

the class WebViewLoginController method onViewBound.

@Override
protected void onViewBound(@NonNull View view) {
    super.onViewBound(view);
    NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
    if (getActivity() != null) {
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    if (getActionBar() != null) {
        getActionBar().hide();
    }
    assembledPrefix = getResources().getString(R.string.nc_talk_login_scheme) + PROTOCOL_SUFFIX + "login/";
    webView.getSettings().setAllowFileAccess(false);
    webView.getSettings().setAllowFileAccessFromFileURLs(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setUserAgentString(getWebLoginUserAgent());
    webView.getSettings().setSaveFormData(false);
    webView.getSettings().setSavePassword(false);
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webView.clearCache(true);
    webView.clearFormData();
    webView.clearHistory();
    CookieSyncManager.createInstance(getActivity());
    android.webkit.CookieManager.getInstance().removeAllCookies(null);
    Map<String, String> headers = new HashMap<>();
    headers.put("OCS-APIRequest", "true");
    webView.setWebViewClient(new WebViewClient() {

        private boolean basePageLoaded;

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith(assembledPrefix)) {
                parseAndLoginFromWebView(url);
                return true;
            }
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            loginStep++;
            if (!basePageLoaded) {
                if (progressBar != null) {
                    progressBar.setVisibility(View.GONE);
                }
                if (webView != null) {
                    webView.setVisibility(View.VISIBLE);
                }
                basePageLoaded = true;
            }
            if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
                if (loginStep == 1) {
                    webView.loadUrl("javascript: {document.getElementsByClassName('login')[0].click(); };");
                } else if (!automatedLoginAttempted) {
                    automatedLoginAttempted = true;
                    webView.loadUrl("javascript: {" + "document.getElementById('user').value = '" + username + "';" + "document.getElementById('password').value = '" + password + "';" + "document.getElementById('submit').click(); };");
                }
            }
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
            String host = null;
            try {
                URL url = new URL(webView.getUrl());
                host = url.getHost();
            } catch (MalformedURLException e) {
                Log.d(TAG, "Failed to create url");
            }
            KeyChain.choosePrivateKeyAlias(getActivity(), alias -> {
                try {
                    if (alias != null) {
                        PrivateKey privateKey = KeyChain.getPrivateKey(getActivity(), alias);
                        X509Certificate[] certificates = KeyChain.getCertificateChain(getActivity(), alias);
                        request.proceed(privateKey, certificates);
                    } else {
                        request.cancel();
                    }
                } catch (KeyChainException e) {
                    Log.e(TAG, "Failed to get keys via keychain exception");
                    request.cancel();
                } catch (InterruptedException e) {
                    Log.e(TAG, "Failed to get keys due to interruption");
                    request.cancel();
                }
            }, new String[] { "RSA" }, null, host, -1, null);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            try {
                SslCertificate sslCertificate = error.getCertificate();
                Field f = sslCertificate.getClass().getDeclaredField("mX509Certificate");
                f.setAccessible(true);
                X509Certificate cert = (X509Certificate) f.get(sslCertificate);
                if (cert == null) {
                    handler.cancel();
                } else {
                    try {
                        magicTrustManager.checkServerTrusted(new X509Certificate[] { cert }, "generic");
                        handler.proceed();
                    } catch (CertificateException exception) {
                        eventBus.post(new CertificateEvent(cert, magicTrustManager, handler));
                    }
                }
            } catch (Exception exception) {
                handler.cancel();
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    });
    webView.loadUrl(baseUrl + "/index.php/login/flow", headers);
}
Also used : X509Certificate(java.security.cert.X509Certificate) AutoInjector(autodagger.AutoInjector) Bundle(android.os.Bundle) KeyChain(android.security.KeyChain) UserUtils(com.nextcloud.talk.utils.database.user.UserUtils) URLDecoder(java.net.URLDecoder) ProgressBar(android.widget.ProgressBar) URL(java.net.URL) BindView(butterknife.BindView) SslErrorHandler(android.webkit.SslErrorHandler) Locale(java.util.Locale) Map(java.util.Map) ActivityInfo(android.content.pm.ActivityInfo) WebViewClient(android.webkit.WebViewClient) View(android.view.View) CookieSyncManager(android.webkit.CookieSyncManager) WebView(android.webkit.WebView) Persistable(io.requery.Persistable) Log(android.util.Log) ReactiveEntityStore(io.requery.reactivex.ReactiveEntityStore) ViewGroup(android.view.ViewGroup) BundleKeys(com.nextcloud.talk.utils.bundle.BundleKeys) Disposable(io.reactivex.disposables.Disposable) ClientCertRequest(android.webkit.ClientCertRequest) LoginData(com.nextcloud.talk.models.LoginData) PrivateKey(java.security.PrivateKey) UserEntity(com.nextcloud.talk.models.database.UserEntity) HashMap(java.util.HashMap) NonNull(android.support.annotation.NonNull) CertificateEvent(com.nextcloud.talk.events.CertificateEvent) MagicTrustManager(com.nextcloud.talk.utils.ssl.MagicTrustManager) Inject(javax.inject.Inject) WebSettings(android.webkit.WebSettings) BaseController(com.nextcloud.talk.controllers.base.BaseController) EventBus(org.greenrobot.eventbus.EventBus) Build(android.os.Build) SslError(android.net.http.SslError) R(com.nextcloud.talk.R) MalformedURLException(java.net.MalformedURLException) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) CertificateException(java.security.cert.CertificateException) Field(java.lang.reflect.Field) HorizontalChangeHandler(com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler) KeyChainException(android.security.KeyChainException) SslCertificate(android.net.http.SslCertificate) ApplicationWideMessageHolder(com.nextcloud.talk.utils.ApplicationWideMessageHolder) NextcloudTalkApplication(com.nextcloud.talk.application.NextcloudTalkApplication) RouterTransaction(com.bluelinelabs.conductor.RouterTransaction) SslErrorHandler(android.webkit.SslErrorHandler) MalformedURLException(java.net.MalformedURLException) PrivateKey(java.security.PrivateKey) HashMap(java.util.HashMap) ClientCertRequest(android.webkit.ClientCertRequest) SslError(android.net.http.SslError) CertificateException(java.security.cert.CertificateException) URL(java.net.URL) X509Certificate(java.security.cert.X509Certificate) MalformedURLException(java.net.MalformedURLException) CertificateException(java.security.cert.CertificateException) KeyChainException(android.security.KeyChainException) Field(java.lang.reflect.Field) KeyChainException(android.security.KeyChainException) SslCertificate(android.net.http.SslCertificate) CertificateEvent(com.nextcloud.talk.events.CertificateEvent) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Aggregations

ActivityInfo (android.content.pm.ActivityInfo)1 SslCertificate (android.net.http.SslCertificate)1 SslError (android.net.http.SslError)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 KeyChain (android.security.KeyChain)1 KeyChainException (android.security.KeyChainException)1 NonNull (android.support.annotation.NonNull)1 TextUtils (android.text.TextUtils)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ClientCertRequest (android.webkit.ClientCertRequest)1 CookieSyncManager (android.webkit.CookieSyncManager)1 SslErrorHandler (android.webkit.SslErrorHandler)1 WebSettings (android.webkit.WebSettings)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 ProgressBar (android.widget.ProgressBar)1