oauth authtoken saved

This commit is contained in:
simon 2020-09-30 14:34:14 +03:00
parent 735cfb3dce
commit cc107157fc
5 changed files with 184 additions and 9 deletions

View file

@ -479,7 +479,16 @@
<activity android:name="net.osmand.plus.activities.SettingsNavigationActivity" android:configChanges="keyboardHidden|orientation" />
<activity android:name="net.osmand.plus.monitoring.SettingsMonitoringActivity" android:configChanges="keyboardHidden|orientation" />
<activity android:name="net.osmand.plus.osmedit.SettingsOsmEditingActivity" android:configChanges="keyboardHidden|orientation" />
<activity android:name="net.osmand.plus.osmedit.SettingsOsmEditingActivity"
android:launchMode="singleInstance"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="osmand-oauth" />
</intent-filter>
</activity>
<activity android:name="net.osmand.plus.development.SettingsDevelopmentActivity" android:configChanges="keyboardHidden|orientation" />
<activity android:name="net.osmand.plus.audionotes.SettingsAudioVideoActivity" android:configChanges="keyboardHidden|orientation" />
<activity android:name="net.osmand.access.SettingsAccessibilityActivity" android:configChanges="keyboardHidden|orientation" />

View file

@ -216,9 +216,13 @@ android {
buildTypes {
debug {
buildConfigField "String", "CONSUMER_KEY", "\"Ti2qq3fo4i4Wmuox3SiWRIGq3obZisBHnxmcM05y\""
buildConfigField "String", "CONSUMER_SECRET", "\"lxulb3HYoMmd2cC4xxNe1dyfRMAY8dS0eNihJ0DM\""
signingConfig signingConfigs.development
}
release {
buildConfigField "String", "CONSUMER_KEY", "\"Ti2qq3fo4i4Wmuox3SiWRIGq3obZisBHnxmcM05y\""
buildConfigField "String", "CONSUMER_SECRET", "\"lxulb3HYoMmd2cC4xxNe1dyfRMAY8dS0eNihJ0DM\""
signingConfig signingConfigs.publishing
}
}
@ -564,6 +568,9 @@ dependencies {
exclude group: 'com.android.support'
}
implementation 'com.jaredrummler:colorpicker:1.1.0'
implementation('com.github.scribejava:scribejava-apis:7.1.1'){
exclude group: "com.fasterxml.jackson.core"
}
huaweiImplementation files('libs/huawei-android-drm_v2.5.2.300.jar')
freehuaweiImplementation files('libs/huawei-android-drm_v2.5.2.300.jar')

View file

@ -3,8 +3,10 @@ package net.osmand.plus.osmedit;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.Preference;
@ -12,22 +14,34 @@ import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import net.osmand.plus.settings.backend.OsmAndAppCustomization;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationClient;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.settings.backend.OsmAndAppCustomization;
public class SettingsOsmEditingActivity extends SettingsBaseActivity {
OsmOAuthAuthorizationClient client;
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
((OsmandApplication) getApplication()).applyTheme(this);
super.onCreate(savedInstanceState);
client = new OsmOAuthAuthorizationClient(this.getMyApplication());
getToolbar().setTitle(R.string.osm_settings);
@SuppressWarnings("deprecation")
PreferenceScreen grp = getPreferenceScreen();
@ -39,7 +53,7 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
R.string.offline_edition, R.string.offline_edition_descr);
grp.addPreference(poiEdit);
Preference pref = new Preference(this);
final Preference pref = new Preference(this);
pref.setTitle(R.string.local_openstreetmap_settings);
pref.setSummary(R.string.local_openstreetmap_settings_descr);
pref.setKey("local_openstreetmap_points");
@ -56,6 +70,20 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
}
});
grp.addPreference(pref);
final Preference prefOAuth = new Preference(this);
prefOAuth.setTitle(R.string.osb_author_dialog_password);
prefOAuth.setSummary(R.string.osb_author_dialog_password);
prefOAuth.setKey("local_openstreetmap_points");
prefOAuth.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
ViewGroup preferenceView = (ViewGroup)getListView().getChildAt(preference.getOrder());
client.startOAuth(preferenceView);
return true;
}
});
grp.addPreference(prefOAuth);
}
public class OsmLoginDataDialogPreference extends DialogPreference {
@ -116,4 +144,15 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
System.out.println("URI=" + uri);
if (uri != null && uri.toString().startsWith("osmand-oauth")) {
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
client.authorize(oauthVerifier);
}
}
}

View file

@ -0,0 +1,114 @@
// License: GPL. For details, see LICENSE file.
package net.osmand.plus.osmedit.oauth;
import android.net.TrafficStats;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.builder.api.DefaultApi10a;
import com.github.scribejava.core.model.*;
import com.github.scribejava.core.oauth.OAuth10aService;
import net.osmand.plus.BuildConfig;
import net.osmand.plus.OsmandApplication;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
/**
* An OAuth 1.0 authorization client.
*
* @since 2746
*/
public class OsmOAuthAuthorizationClient {
OAuth10aService service = new ServiceBuilder(BuildConfig.CONSUMER_KEY)
.apiSecret(BuildConfig.CONSUMER_SECRET)
.debug()
.callback("osmand-oauth://example.com/oauth")
.build(new OsmApi());
OAuth1RequestToken requestToken;
OAuth1AccessToken accessToken;
OsmandApplication application;
static class OsmApi extends DefaultApi10a {
@Override
public String getRequestTokenEndpoint() { return "https://www.openstreetmap.org/oauth/request_token"; }
@Override
public String getAccessTokenEndpoint() {
return "https://www.openstreetmap.org/oauth/access_token";
}
@Override
protected String getAuthorizationBaseUrl() {
return "https://www.openstreetmap.org/oauth/authorize";
}
}
public OsmOAuthAuthorizationClient(OsmandApplication application) {
final int THREAD_ID = 10101;
TrafficStats.setThreadStatsTag(THREAD_ID);
this.application = application;
}
public void performGetRequest(String url,OAuthAsyncRequestCallback<Response> callback) {
if (accessToken == null) {
throw new IllegalStateException("Access token is null");
}
OAuthRequest req = new OAuthRequest(Verb.GET, url);
service.signRequest(accessToken, req);
service.execute(req, callback);
}
public void startOAuth(ViewGroup rootLayout) {
try {
requestToken = service.getRequestToken();
loadWebView(rootLayout, service.getAuthorizationUrl(requestToken));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void authorize(String oauthVerifier) {
try {
accessToken = service.getAccessToken(requestToken, oauthVerifier);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
saveToken();
}
public boolean isValidToken(){
return !(accessToken == null);
}
public void restoreToken() {
String token = application.getSettings().USER_ACCESS_TOKEN.get();
String tokenSecret = application.getSettings().USER_ACCESS_TOKEN_SECRET.get();
accessToken = new OAuth1AccessToken(token, tokenSecret);
}
private void saveToken() {
application.getSettings().USER_ACCESS_TOKEN.set(accessToken.getToken());
application.getSettings().USER_ACCESS_TOKEN_SECRET.set(accessToken.getTokenSecret());
}
private void loadWebView(ViewGroup root, String url) {
WebView webView = new WebView(root.getContext());
webView.requestFocus(View.FOCUS_DOWN);
webView.loadUrl(url);
root.addView(webView);
}
}

View file

@ -2023,6 +2023,12 @@ public class OsmandSettings {
public final OsmandPreference<String> USER_PASSWORD =
new StringPreference("user_password", "").makeGlobal();
public final OsmandPreference<String> USER_ACCESS_TOKEN =
new StringPreference("user_access_token", "").makeGlobal();
public final OsmandPreference<String> USER_ACCESS_TOKEN_SECRET =
new StringPreference("user_access_token_secret", "").makeGlobal();
// this value boolean is synchronized with settings_pref.xml preference offline POI/Bugs edition
public final OsmandPreference<Boolean> OFFLINE_EDITION = new BooleanPreference("offline_osm_editing", true).makeGlobal();