I'm trying to implement oauth authorization into my Android(KitKat based) app.
I've prepared own oauth2 server,based on php (Symfony3 Framework + FOS Oauth Server Bundle). Now I need to make authorization in android app. I was wondering on internet and I didn't found any solution which can help me to do authorization. There are a lot of docs, which describes OAUTH using google or other social services, but, as I told, I have my own oauth server.
I was trying to make something in mobile app, and i meet a few problems. Oauth flow requires to open web-view element in app and accept usage of my web account by my app. This flow works in my server, but i have no idea how to do it in app. I tried to open web view, i was passing auth flow and was getting code, but it is displayed inside webview. I found method - which catch the moment of web-view load, and in this case - i can catch some params from web-view URL, but my oauth flow in web is under firewall. If i'm not authorized in web - flow will redirect me to login page, and later - after success login - it will offer me accept or deny usage of my account data. So, i can't use onPageFinished or something else.
Other case, i can obtain access_token by passing login,password, client_id,secret and other params. I was thinking to make 2 services in app, first will check locally - if my token is not expired, and if it is - it will run second service - to refresh my token - to make http request to my oauth server(web application). But I meet another problem here.
I'm using Volley library to make http calls. AS I know, volley runs asynchronous requests to web. I was trying to move my request into separate class/service. But i had problems with nullable context. Ok. I decided to make requests in activity class (not good case of usage, but ok), and there - i meet another problems. I have defined button, and onClick listener for it. I want to authenticate user after he'll path login and password into EditText fields and in onClick for sign in button - i'm checking SharedPreferences for client_id, and if it's empty - i want to get new oauth client_id from web, i'm runing new volley request in onCLickListener. The problem is that - i can't obtain response correctly. My code example.
``` signIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String login = LoginView.getText().toString();
String password = PasswordView.getText().toString();
auth_dialog = new Dialog(LoginActivity.this);
auth_dialog.setContentView(R.layout.auth_dialog);
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
preferences.edit().putString(getString(R.string.oauth_client_id), null).apply();
String clientId = preferences.getString(getString(R.string.oauth_client_id), null);
preferences.edit().putString(getString(R.string.user_login), login).apply();
preferences.edit().putString(getString(R.string.user_password), password).apply();
if(clientId == null){
RequestQueue queue = Volley.newRequestQueue(context);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String url = context.getString(R.string.url) + context.getString(R.string.url_token) + "?";
url += "client_id=" + preferences.getString(context.getString(R.string.oauth_client_id), null) + "&";
url += "client_secret=" + preferences.getString(context.getString(R.string.oauth_client_secret), null) + "&";
url += "grant_type=" + context.getString(R.string.grant_type_password) + "&";
url += "username=" + preferences.getString(context.getString(R.string.user_login), null) + "&";
url += "password=" + preferences.getString(context.getString(R.string.user_password), null);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response){
clientResponse = response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "Request for getting Token failed", Toast.LENGTH_SHORT).show();
}
});
queue.add(stringRequest);
}
Log.d("VIOO_APP", clientResponse);// THIS IS THE PLACE WHERE ERROR HAPPENS
token = getToken();
Toast.makeText(getApplicationContext(), token.getAccessToken(), Toast.LENGTH_LONG).show();
}
});```
I want to say that all variables are defined - it's jsut the part of code from button onClick Listener. clientResponse
variable defined in activity class as Global variable.
Ofcorse, i can put my logic into response statement - when volley got response - do my stuff, but i think - later, my code will be unreadable and it's not good approach in building app structure. It's a total bullshit to make request in response from another request, and so on...
I had good cases, when my request was working in another class. I saw examples in internet - how some guys tries to make app service - to make requests through volley, but probably, this info is not actual now. Every internet case - which i found - won't work or provide useless info.
I see this challenge like unwinnable. All stuff I try - fails and won't work.
I hope, i explained my problem fully.
Thank you.