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

@ -7,7 +7,7 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" /> <uses-permission android:name="android.permission.STORAGE" />
@ -52,7 +52,7 @@
<application android:allowBackup="true" android:backupAgent="net.osmand.plus.OsmandBackupAgent" <application android:allowBackup="true" android:backupAgent="net.osmand.plus.OsmandBackupAgent"
android:icon="@mipmap/icon" android:label="@string/app_name" android:icon="@mipmap/icon" android:label="@string/app_name"
android:name="net.osmand.plus.OsmandApplication" android:configChanges="locale" android:name="net.osmand.plus.OsmandApplication" android:configChanges="locale"
android:theme="@style/OsmandDarkTheme" android:restoreAnyVersion="true" android:largeHeap="true" android:theme="@style/OsmandDarkTheme" android:restoreAnyVersion="true" android:largeHeap="true"
android:supportsRtl="true" android:usesCleartextTraffic="true"> android:supportsRtl="true" android:usesCleartextTraffic="true">
<meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIqF3tNGT66etVBn_vgzpfAY1wmIzKV1Ss6Ku-2A" /> <meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIqF3tNGT66etVBn_vgzpfAY1wmIzKV1Ss6Ku-2A" />
@ -65,7 +65,7 @@
<meta-data android:name="com.sec.minimode.icon.landscape.normal" android:resource="@mipmap/icon" android:value="" /> <meta-data android:name="com.sec.minimode.icon.landscape.normal" android:resource="@mipmap/icon" android:value="" />
<activity android:name="net.osmand.plus.activities.HelpActivity" /> <activity android:name="net.osmand.plus.activities.HelpActivity" />
<activity android:name="net.osmand.plus.activities.ExitActivity" /> <activity android:name="net.osmand.plus.activities.ExitActivity" />
<provider <provider
android:name="androidx.core.content.FileProvider" android:name="androidx.core.content.FileProvider"
android:authorities="net.osmand.plus.fileprovider" android:authorities="net.osmand.plus.fileprovider"
@ -479,7 +479,16 @@
<activity android:name="net.osmand.plus.activities.SettingsNavigationActivity" android:configChanges="keyboardHidden|orientation" /> <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.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.development.SettingsDevelopmentActivity" android:configChanges="keyboardHidden|orientation" />
<activity android:name="net.osmand.plus.audionotes.SettingsAudioVideoActivity" 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" /> <activity android:name="net.osmand.access.SettingsAccessibilityActivity" android:configChanges="keyboardHidden|orientation" />
@ -1025,7 +1034,7 @@
<!-- keep android:process on a separate line !! --> <!-- keep android:process on a separate line !! -->
<service <service
android:process="net.osmand.plus" android:process="net.osmand.plus"
android:label="@string/process_navigation_service" android:label="@string/process_navigation_service"
android:name="net.osmand.plus.NavigationService" android:name="net.osmand.plus.NavigationService"
android:foregroundServiceType="location" android:foregroundServiceType="location"

View file

@ -216,9 +216,13 @@ android {
buildTypes { buildTypes {
debug { debug {
buildConfigField "String", "CONSUMER_KEY", "\"Ti2qq3fo4i4Wmuox3SiWRIGq3obZisBHnxmcM05y\""
buildConfigField "String", "CONSUMER_SECRET", "\"lxulb3HYoMmd2cC4xxNe1dyfRMAY8dS0eNihJ0DM\""
signingConfig signingConfigs.development signingConfig signingConfigs.development
} }
release { release {
buildConfigField "String", "CONSUMER_KEY", "\"Ti2qq3fo4i4Wmuox3SiWRIGq3obZisBHnxmcM05y\""
buildConfigField "String", "CONSUMER_SECRET", "\"lxulb3HYoMmd2cC4xxNe1dyfRMAY8dS0eNihJ0DM\""
signingConfig signingConfigs.publishing signingConfig signingConfigs.publishing
} }
} }
@ -564,6 +568,9 @@ dependencies {
exclude group: 'com.android.support' exclude group: 'com.android.support'
} }
implementation 'com.jaredrummler:colorpicker:1.1.0' 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') huaweiImplementation files('libs/huawei-android-drm_v2.5.2.300.jar')
freehuaweiImplementation 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.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.StrictMode;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.DialogPreference; import android.preference.DialogPreference;
import android.preference.Preference; import android.preference.Preference;
@ -12,22 +14,34 @@ import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; 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.OsmandPlugin;
import net.osmand.plus.osmedit.oauth.OsmOAuthAuthorizationClient;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.settings.backend.OsmAndAppCustomization;
public class SettingsOsmEditingActivity extends SettingsBaseActivity { public class SettingsOsmEditingActivity extends SettingsBaseActivity {
OsmOAuthAuthorizationClient client;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
((OsmandApplication) getApplication()).applyTheme(this); ((OsmandApplication) getApplication()).applyTheme(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
client = new OsmOAuthAuthorizationClient(this.getMyApplication());
getToolbar().setTitle(R.string.osm_settings); getToolbar().setTitle(R.string.osm_settings);
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
PreferenceScreen grp = getPreferenceScreen(); PreferenceScreen grp = getPreferenceScreen();
@ -39,7 +53,7 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
R.string.offline_edition, R.string.offline_edition_descr); R.string.offline_edition, R.string.offline_edition_descr);
grp.addPreference(poiEdit); grp.addPreference(poiEdit);
Preference pref = new Preference(this); final Preference pref = new Preference(this);
pref.setTitle(R.string.local_openstreetmap_settings); pref.setTitle(R.string.local_openstreetmap_settings);
pref.setSummary(R.string.local_openstreetmap_settings_descr); pref.setSummary(R.string.local_openstreetmap_settings_descr);
pref.setKey("local_openstreetmap_points"); pref.setKey("local_openstreetmap_points");
@ -56,6 +70,20 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
} }
}); });
grp.addPreference(pref); 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 { public class OsmLoginDataDialogPreference extends DialogPreference {
@ -116,4 +144,15 @@ public class SettingsOsmEditingActivity extends SettingsBaseActivity {
Toast.makeText(context, text, Toast.LENGTH_LONG).show(); 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 = public final OsmandPreference<String> USER_PASSWORD =
new StringPreference("user_password", "").makeGlobal(); 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 // 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(); public final OsmandPreference<Boolean> OFFLINE_EDITION = new BooleanPreference("offline_osm_editing", true).makeGlobal();