forked from organicmaps/organicmaps
[Android] Build OAuth2 URL using native code instead of Java
Small changes according to PR comments Signed-off-by: Sergiy Kozyr <s.trump@gmail.com>
This commit is contained in:
parent
c6a0ad53c3
commit
7cc56dd318
6 changed files with 28 additions and 40 deletions
|
@ -34,18 +34,11 @@ bool LoadOsmUserPreferences(std::string const & oauthToken, UserPreferences & ou
|
|||
extern "C"
|
||||
{
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
Java_app_organicmaps_editor_OsmOAuth_nativeOAuthParams(JNIEnv * env, jclass clazz)
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_app_organicmaps_editor_OsmOAuth_nativeGetOAuth2Url(JNIEnv * env, jclass)
|
||||
{
|
||||
OsmOAuth auth = OsmOAuth::ServerAuth();
|
||||
jobjectArray oauthParams = env->NewObjectArray(5, env->FindClass("java/lang/String"), nullptr);
|
||||
env->SetObjectArrayElement(oauthParams, 0, ToJavaString(env, auth.GetBaseUrl()));
|
||||
env->SetObjectArrayElement(oauthParams, 1, ToJavaString(env, auth.GetClientId()));
|
||||
env->SetObjectArrayElement(oauthParams, 2, ToJavaString(env, auth.GetClientSecret()));
|
||||
env->SetObjectArrayElement(oauthParams, 3, ToJavaString(env, auth.GetScope()));
|
||||
env->SetObjectArrayElement(oauthParams, 4, ToJavaString(env, auth.GetRedirectUri()));
|
||||
|
||||
return oauthParams;
|
||||
auto const auth = OsmOAuth::ServerAuth();
|
||||
return ToJavaString(env, auth.BuildOAuth2Url());
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
|
@ -67,7 +60,7 @@ Java_app_organicmaps_editor_OsmOAuth_nativeAuthWithPassword(JNIEnv * env, jclass
|
|||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_app_organicmaps_editor_OsmOAuth_nativeAuthWithOAuth2Code(JNIEnv * env, jclass clazz, jstring oauth2code)
|
||||
Java_app_organicmaps_editor_OsmOAuth_nativeAuthWithOAuth2Code(JNIEnv * env, jclass, jstring oauth2code)
|
||||
{
|
||||
OsmOAuth auth = OsmOAuth::ServerAuth();
|
||||
try
|
||||
|
@ -78,7 +71,7 @@ Java_app_organicmaps_editor_OsmOAuth_nativeAuthWithOAuth2Code(JNIEnv * env, jcla
|
|||
auth.SetAuthToken(token);
|
||||
return ToJavaString(env, token);
|
||||
}
|
||||
LOG(LWARNING, ("nativeAuthWithOAuth2Code: invalid OAuth2 code"));
|
||||
LOG(LWARNING, ("nativeAuthWithOAuth2Code: invalid OAuth2 code", oauth2code));
|
||||
}
|
||||
catch (std::exception const & ex)
|
||||
{
|
||||
|
|
|
@ -8,7 +8,6 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
@ -25,8 +24,6 @@ import app.organicmaps.util.concurrency.UiThread;
|
|||
|
||||
public class OsmLoginFragment extends BaseMwmToolbarFragment
|
||||
{
|
||||
private FrameLayout loginBottomSheet;
|
||||
//private BottomSheetBehavior<FrameLayout> standardBottomSheetBehavior;
|
||||
private Button mLoginUsernameButton;
|
||||
private Button mLoginWebsiteButton;
|
||||
|
||||
|
@ -75,18 +72,13 @@ public class OsmLoginFragment extends BaseMwmToolbarFragment
|
|||
|
||||
private void loginWithBrowser()
|
||||
{
|
||||
String[] oauthParams = OsmOAuth.nativeOAuthParams();
|
||||
Uri oauth2url = Uri.parse(oauthParams[0] + "/oauth2/authorize")
|
||||
.buildUpon()
|
||||
.appendQueryParameter("client_id", oauthParams[1])
|
||||
.appendQueryParameter("scope", oauthParams[3])
|
||||
.appendQueryParameter("redirect_uri", oauthParams[4])
|
||||
.appendQueryParameter("response_type", "code")
|
||||
.build();
|
||||
try {
|
||||
Intent myIntent = new Intent(Intent.ACTION_VIEW, oauth2url);
|
||||
try
|
||||
{
|
||||
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(OsmOAuth.nativeGetOAuth2Url()));
|
||||
startActivity(myIntent);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
}
|
||||
catch (ActivityNotFoundException e)
|
||||
{
|
||||
//Toast.makeText(this, "No application can handle this request."
|
||||
// + " Please install a webbrowser", Toast.LENGTH_LONG).show();
|
||||
e.printStackTrace();
|
||||
|
@ -100,9 +92,7 @@ public class OsmLoginFragment extends BaseMwmToolbarFragment
|
|||
return;
|
||||
|
||||
if (oauth2code == null || oauth2code.isEmpty())
|
||||
{
|
||||
onAuthFail();
|
||||
}
|
||||
else
|
||||
{
|
||||
ThreadPool.getWorker().execute(() -> {
|
||||
|
|
|
@ -104,7 +104,7 @@ public final class OsmOAuth
|
|||
Returns 5 strings: ServerURL, ClientId, ClientSecret, Scope, RedirectUri
|
||||
*/
|
||||
@NonNull
|
||||
public static native String[] nativeOAuthParams();
|
||||
public static native String nativeGetOAuth2Url();
|
||||
|
||||
/**
|
||||
* @return string with OAuth2 token
|
||||
|
|
|
@ -241,14 +241,7 @@ string OsmOAuth::SendAuthRequest(string const & requestTokenKey, SessionID const
|
|||
|
||||
string OsmOAuth::FetchRequestToken(SessionID const & sid) const
|
||||
{
|
||||
string const requestTokenUrl = m_baseUrl + "/oauth2/authorize";
|
||||
string const requestTokenQuery = BuildPostRequest({
|
||||
{"client_id", m_oauth2params.m_clientId},
|
||||
{"redirect_uri", m_oauth2params.m_redirectUri},
|
||||
{"scope", m_oauth2params.m_scope},
|
||||
{"response_type", "code"}
|
||||
});
|
||||
HttpClient request(requestTokenUrl + "?" + requestTokenQuery);
|
||||
HttpClient request(BuildOAuth2Url());
|
||||
request.SetCookies(sid.m_cookies)
|
||||
.SetFollowRedirects(false);
|
||||
|
||||
|
@ -280,6 +273,18 @@ string OsmOAuth::FetchRequestToken(SessionID const & sid) const
|
|||
}
|
||||
}
|
||||
|
||||
string OsmOAuth::BuildOAuth2Url() const
|
||||
{
|
||||
string const requestTokenUrl = m_baseUrl + "/oauth2/authorize";
|
||||
string const requestTokenQuery = BuildPostRequest({
|
||||
{"client_id", m_oauth2params.m_clientId},
|
||||
{"redirect_uri", m_oauth2params.m_redirectUri},
|
||||
{"scope", m_oauth2params.m_scope},
|
||||
{"response_type", "code"}
|
||||
});
|
||||
return requestTokenUrl + "?" + requestTokenQuery;
|
||||
}
|
||||
|
||||
string OsmOAuth::FinishAuthorization(string const & oauth2code) const
|
||||
{
|
||||
auto params = BuildPostRequest({
|
||||
|
|
|
@ -109,6 +109,7 @@ public:
|
|||
{
|
||||
return m_baseUrl + "/user/" + user + "/history";
|
||||
}
|
||||
std::string BuildOAuth2Url() const;
|
||||
//@}
|
||||
|
||||
private:
|
||||
|
@ -143,7 +144,6 @@ private:
|
|||
/// @returns valid key and secret or throws otherwise.
|
||||
std::string FetchRequestToken(SessionID const & sid) const;
|
||||
std::string FetchAccessToken(SessionID const & sid) const;
|
||||
//AuthResult FetchAccessToken(SessionID const & sid);
|
||||
};
|
||||
|
||||
std::string DebugPrint(OsmOAuth::Response const & code);
|
||||
|
|
|
@ -96,7 +96,7 @@ public:
|
|||
|
||||
std::string const & GetOAuth2Code() const
|
||||
{
|
||||
ASSERT_EQUAL(m_requestType, UrlType::OAuth2, ("Expected Search API"));
|
||||
ASSERT_EQUAL(m_requestType, UrlType::OAuth2, ("Expected OAuth2 API"));
|
||||
return m_oauth2code;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue