forked from organicmaps/organicmaps
[android] Working password and webview auth.
This commit is contained in:
parent
8d759992a0
commit
3a7faf7346
6 changed files with 148 additions and 58 deletions
|
@ -37,7 +37,6 @@ Java_com_mapswithme_maps_editor_OsmOAuth_nativeAuthWithPassword(JNIEnv * env, jc
|
|||
{
|
||||
OsmOAuth auth = OsmOAuth::ServerAuth();
|
||||
auto authResult = auth.AuthorizePassword(ToNativeString(env, login), ToNativeString(env, password));
|
||||
LOG(LINFO, ("Auth result : ", authResult));
|
||||
return authResult == OsmOAuth::AuthResult::OK ? ToStringArray(env, auth.GetToken())
|
||||
: nullptr;
|
||||
}
|
||||
|
@ -50,7 +49,6 @@ Java_com_mapswithme_maps_editor_OsmOAuth_nativeAuthWithWebviewToken(JNIEnv * env
|
|||
TKeySecret outKeySecret;
|
||||
TKeySecret inKeySecret(ToNativeString(env, secret), ToNativeString(env, token));
|
||||
auto authResult = auth.FinishAuthorization(inKeySecret, ToNativeString(env, verifier), outKeySecret);
|
||||
LOG(LINFO, ("Auth result : ", authResult));
|
||||
if (authResult != OsmOAuth::AuthResult::OK)
|
||||
return nullptr;
|
||||
auth.FinishAuthorization(inKeySecret, ToNativeString(env, token), outKeySecret);
|
||||
|
@ -65,7 +63,7 @@ Java_com_mapswithme_maps_editor_OsmOAuth_nativeGetFacebookAuthUrl(JNIEnv * env,
|
|||
}
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
Java_com_mapswithme_maps_editor_OsmOAuth_nativeAuthGoogle(JNIEnv * env, jclass clazz)
|
||||
Java_com_mapswithme_maps_editor_OsmOAuth_nativeGetGoogleAuthUrl(JNIEnv * env, jclass clazz)
|
||||
{
|
||||
OsmOAuth::TUrlKeySecret keySecret = OsmOAuth::ServerAuth().GetGoogleOAuthURL();
|
||||
return ToStringArray(env, keySecret.first, keySecret.second.first, keySecret.second.second);
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
tools:ignore="UnusedAttribute"/>
|
||||
|
||||
<ScrollView
|
||||
android:id="@+id/block_login"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/bg_cards"
|
||||
|
@ -118,37 +117,4 @@
|
|||
|
||||
</ScrollView>
|
||||
|
||||
<ScrollView
|
||||
android:id="@+id/block_terms"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:fillViewport="true"
|
||||
tools:ignore="DuplicateIds">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/block_terms"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="@dimen/margin_base"
|
||||
android:visibility="gone">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/terms"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn__confirm"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/base_block_size"
|
||||
android:background="?buttonAccent"
|
||||
android:text="@string/agree"
|
||||
android:textAllCaps="true"
|
||||
android:textAppearance="@style/MwmTextAppearance.Body3.Light"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
||||
</LinearLayout>
|
|
@ -1,20 +1,29 @@
|
|||
package com.mapswithme.maps.editor;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.UrlQuerySanitizer;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.Size;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.EditText;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmToolbarFragment;
|
||||
import com.mapswithme.maps.widget.ToolbarController;
|
||||
import com.mapswithme.util.Constants;
|
||||
import com.mapswithme.util.Graphics;
|
||||
import com.mapswithme.maps.widget.InputWebView;
|
||||
import com.mapswithme.util.ThemeUtils;
|
||||
import com.mapswithme.util.Utils;
|
||||
import com.mapswithme.util.concurrency.ThreadPool;
|
||||
import com.mapswithme.util.concurrency.UiThread;
|
||||
|
||||
|
@ -35,10 +44,6 @@ public class AuthFragment extends BaseMwmToolbarFragment implements View.OnClick
|
|||
}
|
||||
}
|
||||
|
||||
private static final String PREF_OSM_CONTRIBUTION_CONFIRMED = "OsmContributionConfirmed";
|
||||
|
||||
private View mTermsBlock;
|
||||
private View mLoginBlock;
|
||||
private EditText mEtLogin;
|
||||
private EditText mEtPassword;
|
||||
|
||||
|
@ -54,8 +59,6 @@ public class AuthFragment extends BaseMwmToolbarFragment implements View.OnClick
|
|||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
mToolbarController.setTitle("Log In");
|
||||
mLoginBlock = view.findViewById(R.id.block_login);
|
||||
mTermsBlock = view.findViewById(R.id.block_terms);
|
||||
view.findViewById(R.id.login_osm).setOnClickListener(this);
|
||||
mEtLogin = (EditText) view.findViewById(R.id.osm_username);
|
||||
mEtPassword = (EditText) view.findViewById(R.id.osm_password);
|
||||
|
@ -74,21 +77,23 @@ public class AuthFragment extends BaseMwmToolbarFragment implements View.OnClick
|
|||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
// TODO show/hide spinners
|
||||
switch (v.getId())
|
||||
{
|
||||
case R.id.login_osm:
|
||||
loginOsm();
|
||||
break;
|
||||
case R.id.login_facebook:
|
||||
loginFacebook();
|
||||
loginWebview(true);
|
||||
break;
|
||||
case R.id.login_google:
|
||||
loginGoogle();
|
||||
loginWebview(false);
|
||||
break;
|
||||
case R.id.lost_password:
|
||||
recoverPassword();
|
||||
break;
|
||||
case R.id.register:
|
||||
register();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -114,37 +119,111 @@ public class AuthFragment extends BaseMwmToolbarFragment implements View.OnClick
|
|||
if (!isAdded())
|
||||
return;
|
||||
|
||||
if (auth == null)
|
||||
{
|
||||
new AlertDialog.Builder(getActivity()).setTitle("Auth error!").show();
|
||||
return;
|
||||
}
|
||||
|
||||
OsmOAuth.setAuthorization(auth[0], auth[1]);
|
||||
processAuth(auth);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void loginFacebook()
|
||||
private void processAuth(@Size(2) String[] auth)
|
||||
{
|
||||
if (auth == null)
|
||||
{
|
||||
if (isAdded())
|
||||
{
|
||||
new AlertDialog.Builder(getActivity()).setTitle("Auth error!")
|
||||
.setPositiveButton(android.R.string.ok, null).show();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
OsmOAuth.setAuthorization(auth[0], auth[1]);
|
||||
Utils.navigateToParent(getActivity());
|
||||
}
|
||||
|
||||
private void loginGoogle()
|
||||
private void loginWebview(final boolean facebook)
|
||||
{
|
||||
final WebView webview = new InputWebView(getActivity());
|
||||
final AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(webview).create();
|
||||
|
||||
ThreadPool.getWorker().execute(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = facebook ? OsmOAuth.nativeGetFacebookAuthUrl()
|
||||
: OsmOAuth.nativeGetGoogleAuthUrl();
|
||||
|
||||
UiThread.run(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if (isAdded())
|
||||
loadWebviewAuth(dialog, webview, auth);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void loadWebviewAuth(final AlertDialog dialog, final WebView webview, @Size(3) final String[] auth)
|
||||
{
|
||||
final String authUrl = auth[0];
|
||||
webview.setWebViewClient(new WebViewClient()
|
||||
{
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url)
|
||||
{
|
||||
if (OsmOAuth.shouldReloadWebviewUrl(url))
|
||||
{
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
else if (url.contains(OsmOAuth.URL_PARAM_VERIFIER))
|
||||
{
|
||||
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
|
||||
sanitizer.setAllowUnregisteredParamaters(true);
|
||||
sanitizer.parseUrl(url);
|
||||
final String verifier = sanitizer.getValue(OsmOAuth.URL_PARAM_VERIFIER);
|
||||
finishWebviewAuth(auth[1], auth[2], verifier);
|
||||
dialog.cancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
webview.loadUrl(authUrl);
|
||||
}
|
||||
|
||||
private void finishWebviewAuth(final String token, final String secret, final String verifier)
|
||||
{
|
||||
ThreadPool.getWorker().execute(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final String[] auth = OsmOAuth.nativeAuthWithWebviewToken(token, secret, verifier);
|
||||
UiThread.run(new Runnable() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
processAuth(auth);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void recoverPassword()
|
||||
{
|
||||
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_RECOVER_PASSWORD)));
|
||||
}
|
||||
|
||||
|
||||
private void checkContributionTermsConfirmed()
|
||||
private void register()
|
||||
{
|
||||
// TODO
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.OSM_REGISTER)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ public final class OsmOAuth
|
|||
private static final String PREF_OSM_TOKEN = "OsmToken";
|
||||
private static final String PREF_OSM_SECRET = "OsmSecret";
|
||||
|
||||
public static final String URL_PARAM_VERIFIER = "oauth_verifier";
|
||||
|
||||
public static boolean isAuthorized()
|
||||
{
|
||||
return MwmApplication.prefs().contains(PREF_OSM_TOKEN) && MwmApplication.prefs().contains(PREF_OSM_SECRET);
|
||||
|
@ -51,6 +53,15 @@ public final class OsmOAuth
|
|||
.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Some redirect urls indicates that user wasn't registered before.
|
||||
* Initial auth url should be reloaded to get correct {@link #URL_PARAM_VERIFIER} then.
|
||||
*/
|
||||
public static boolean shouldReloadWebviewUrl(String url)
|
||||
{
|
||||
return url.contains("/welcome") || url.endsWith("/");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array containing auth token and secret
|
||||
*/
|
||||
|
|
33
android/src/com/mapswithme/maps/widget/InputWebView.java
Normal file
33
android/src/com/mapswithme/maps/widget/InputWebView.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package com.mapswithme.maps.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* Workaround for not appearing soft keyboard in webview.
|
||||
* Check bugreport at https://code.google.com/p/android/issues/detail?id=7189 for more details.
|
||||
*/
|
||||
public class InputWebView extends WebView
|
||||
{
|
||||
public InputWebView(Context context)
|
||||
{
|
||||
super(context);
|
||||
}
|
||||
|
||||
public InputWebView(Context context, AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public InputWebView(Context context, AttributeSet attrs, int defStyleAttr)
|
||||
{
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCheckIsTextEditor()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -31,6 +31,9 @@ public final class Constants
|
|||
public static final String COPYRIGHT = "file:///android_asset/copyright.html";
|
||||
public static final String FAQ = "file:///android_asset/faq.html";
|
||||
|
||||
public static final String OSM_REGISTER = "https://www.openstreetmap.org/user/new";
|
||||
public static final String OSM_RECOVER_PASSWORD = "https://www.openstreetmap.org/user/forgot-password";
|
||||
|
||||
private Url() {}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue