Implemented parsing of om://oauth2/osm/callback URLs

Signed-off-by: Sergiy Kozyr <s.trump@gmail.com>
This commit is contained in:
Sergiy Kozyr 2024-08-02 15:09:53 +03:00
parent cc32c18732
commit c1cf2b5c94
9 changed files with 59 additions and 1 deletions

View file

@ -862,6 +862,13 @@ Java_app_organicmaps_Framework_nativeGetParsedAppName(JNIEnv * env, jclass)
return (appName.empty()) ? nullptr : jni::ToJavaString(env, appName);
}
JNIEXPORT jstring JNICALL
Java_app_organicmaps_Framework_nativeGetParsedOAuth2Code(JNIEnv * env, jclass)
{
std::string const & code = frm()->GetParsedOAuth2Code();
return jni::ToJavaString(env, code);
}
JNIEXPORT jstring JNICALL
Java_app_organicmaps_Framework_nativeGetParsedBackUrl(JNIEnv * env, jclass)
{

View file

@ -239,6 +239,7 @@ public class Framework
public static native ParsedRoutingData nativeGetParsedRoutingData();
public static native ParsedSearchRequest nativeGetParsedSearchRequest();
public static native @Nullable String nativeGetParsedAppName();
public static native @Nullable String nativeGetParsedOAuth2Code();
@Nullable @Size(2)
public static native double[] nativeGetParsedCenterLatLon();
public static native @Nullable String nativeGetParsedBackUrl();

View file

@ -6,7 +6,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.SOURCE)
@IntDef({RequestType.INCORRECT, RequestType.MAP, RequestType.ROUTE, RequestType.SEARCH, RequestType.CROSSHAIR})
@IntDef({RequestType.INCORRECT, RequestType.MAP, RequestType.ROUTE, RequestType.SEARCH, RequestType.CROSSHAIR, RequestType.OAUTH2})
public @interface RequestType
{
// Represents url_scheme::ParsedMapApi::UrlType from c++ part.
@ -15,4 +15,5 @@ public @interface RequestType
public static final int ROUTE = 2;
public static final int SEARCH = 3;
public static final int CROSSHAIR = 4;
public static final int OAUTH2 = 5;
}

View file

@ -3,6 +3,7 @@ package app.organicmaps.intent;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.content.IntentCompat;
@ -128,6 +129,15 @@ public class Factory
Framework.nativeSetViewportCenter(latlon[0], latlon[1], SEARCH_IN_VIEWPORT_ZOOM);
}
return true;
}
case RequestType.OAUTH2:
{
SearchEngine.INSTANCE.cancelInteractiveSearch();
final String oauth2code = Framework.nativeGetParsedOAuth2Code();
Log.i("TAG", oauth2code);
return true;
}
}

View file

@ -1738,6 +1738,11 @@ url_scheme::SearchRequest Framework::GetParsedSearchRequest() const
return m_parsedMapApi.GetSearchRequest();
}
std::string Framework::GetParsedOAuth2Code() const
{
return m_parsedMapApi.GetOAuth2Code();
}
std::string const & Framework::GetParsedAppName() const
{
return m_parsedMapApi.GetAppName();

View file

@ -578,6 +578,7 @@ public:
ParsedRoutingData GetParsedRoutingData() const;
url_scheme::SearchRequest GetParsedSearchRequest() const;
std::string GetParsedOAuth2Code() const;
std::string const & GetParsedAppName() const;
std::string const & GetParsedBackUrl() const;
ms::LatLon GetParsedCenterLatLon() const;

View file

@ -474,6 +474,13 @@ UNIT_TEST(AppNameTest)
}
}
UNIT_TEST(OAuth2Test)
{
ParsedMapApi api("om://oauth2/osm/callback?code=THE_MEGA_CODE");
TEST_EQUAL(api.GetRequestType(), UrlType::OAuth2, ());
TEST_EQUAL(api.GetOAuth2Code(), "THE_MEGA_CODE", ());
}
namespace
{
string generatePartOfUrl(url_scheme::MapPoint const & point)

View file

@ -175,6 +175,22 @@ ParsedMapApi::UrlType ParsedMapApi::SetUrlAndParse(std::string const & raw)
return m_requestType = UrlType::Crosshair;
}
else if (type == "oauth2")
{
if (url.GetPath() != "osm/callback")
return m_requestType = UrlType::Incorrect;
url.ForEachParam([this](auto const & key, auto const & value)
{
if (key == "code")
m_oauth2code = value;
});
if (m_oauth2code.empty())
return m_requestType = UrlType::Incorrect;
else
return m_requestType = UrlType::OAuth2;
}
else if (checkForGe0Link)
{
// The URL is prefixed by one of the kGe0Prefixes AND doesn't match any supported API call:
@ -386,6 +402,7 @@ void ParsedMapApi::Reset()
m_mapPoints.clear();
m_routePoints.clear();
m_searchRequest = {};
m_oauth2code = {};
m_globalBackUrl ={};
m_appName = {};
m_centerLatLon = ms::LatLon::Invalid();
@ -467,6 +484,7 @@ std::string DebugPrint(ParsedMapApi::UrlType type)
case ParsedMapApi::UrlType::Route: return "Route";
case ParsedMapApi::UrlType::Search: return "Search";
case ParsedMapApi::UrlType::Crosshair: return "Crosshair";
case ParsedMapApi::UrlType::OAuth2: return "OAuth2";
}
UNREACHABLE();
}

View file

@ -45,6 +45,7 @@ public:
Route = 2,
Search = 3,
Crosshair = 4,
OAuth2 = 5,
};
ParsedMapApi() = default;
@ -93,6 +94,12 @@ public:
return m_searchRequest;
}
std::string const & GetOAuth2Code() const
{
ASSERT_EQUAL(m_requestType, UrlType::OAuth2, ("Expected Search API"));
return m_oauth2code;
}
private:
void ParseMapParam(std::string const & key, std::string const & value,
bool & correctOrder);
@ -107,6 +114,7 @@ private:
SearchRequest m_searchRequest;
std::string m_globalBackUrl;
std::string m_appName;
std::string m_oauth2code;
ms::LatLon m_centerLatLon = ms::LatLon::Invalid();
std::string m_routingType;
int m_version = 0;