Following code can be used to get SAML token Id from SAML
//Calling method to display Token id from the SAML Reponse
view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementsByName('SAMLResponse')[0].value);");
don't forget to use below inner class
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
@JavascriptInterface
public void showHTML(String html) {
System.out.println("html>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:"+html);
new AlertDialog.Builder(ctx).setTitle("Token Id").setMessage(html)
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
}
}
Please refer to the below files for complete code
1- MainActivity
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity {
private long pageStartTime = 0;
private WebView wv;
private ImageButton newActivityBtn;
private EditText et;
private TextView pageLoadTime;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.wv);
newActivityBtn = (ImageButton) findViewById(R.id.new_activity);
pageLoadTime = (TextView) findViewById(R.id.page_load_time);
et = (EditText) findViewById(R.id.et);
// setup edit text
et.setSelected(false);
if (getIntent().getStringExtra("url") != null) {
et.setText(getIntent().getStringExtra("url"));
} else {
et.setText("");
}
//wv.setWebChromeClient(new WebChromeClient());
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAppCacheEnabled(false);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
et.setText(url);
pageStartTime = System.currentTimeMillis();
pageLoadTime.setText("0ms");
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (pageStartTime == 0) {
} else {
//Calling method to display Token id from the SAML Reponse
view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementsByName('SAMLResponse')[0].value);");
long loadTime = (System.currentTimeMillis() - pageStartTime);
pageLoadTime.setText(String.format("%sms", loadTime));
System.out.println(String.format("page load time: %sms", loadTime));
view.clearCache(true);
view.clearView();
}
}
});
handleLoadUrl();
// setup events
newActivityBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
newActivityBtn.setColorFilter(getResources().getColor(android.R.color.holo_blue_dark));
return false;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
newActivityBtn.setColorFilter(null);
return false;
}
return false;
}
});
newActivityBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("url", et.getText().toString());
startActivity(intent);
}
});
et.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
handleLoadUrl();
return true;
} else {
return false;
}
}
});
}
private void handleLoadUrl() {
String url = et.getText().toString();
if (url.startsWith("http://")) {
} else if (url.startsWith("https://")) {
} else {
url = String.format("http://%s", url);
}
wv.loadUrl(url);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
@JavascriptInterface
public void showHTML(String html) {
System.out.println("html>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:"+html);
new AlertDialog.Builder(ctx).setTitle("Token Id").setMessage(html)
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
}
}
}
For Android Menifest file and content_main.xml Please refer to below site
https://shdhumale.wordpress.com/2016/03/30/how-to-get-saml-token-from-webview-in-android/
No comments:
Post a Comment