Changed authentication scheme

This commit is contained in:
Roman Kuznetsov 2018-05-11 14:59:52 +03:00 committed by Aleksandr Zatsepin
parent 75a7047f10
commit b2d7c8543e
6 changed files with 99 additions and 19 deletions

View file

@ -1550,7 +1550,11 @@ Java_com_mapswithme_maps_Framework_nativeGetSearchBanners(JNIEnv * env, jclass)
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_Framework_nativeAuthenticateUser(JNIEnv * env, jclass, jstring socialToken,
jint socialTokenType, jobject listener)
jint socialTokenType,
jboolean privacyAccepted,
jboolean termsAccepted,
jboolean promoAccepted,
jobject listener)
{
std::shared_ptr<_jobject> gListener(env->NewGlobalRef(listener), [](jobject l)
{
@ -1571,7 +1575,9 @@ Java_com_mapswithme_maps_Framework_nativeAuthenticateUser(JNIEnv * env, jclass,
});
};
user.AddSubscriber(std::move(s));
user.Authenticate(tokenStr, static_cast<User::SocialTokenType>(socialTokenType));
user.Authenticate(tokenStr, static_cast<User::SocialTokenType>(socialTokenType),
static_cast<bool>(privacyAccepted), static_cast<bool>(termsAccepted),
static_cast<bool>(promoAccepted));
}
JNIEXPORT jboolean JNICALL
@ -1583,8 +1589,19 @@ Java_com_mapswithme_maps_Framework_nativeIsUserAuthenticated()
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_Framework_nativeGetPhoneAuthUrl(JNIEnv * env, jclass, jstring redirectUrl)
{
return jni::ToJavaString(env,
frm()->GetUser().GetPhoneAuthUrl(jni::ToNativeString(env, redirectUrl)));
return jni::ToJavaString(env, User::GetPhoneAuthUrl(jni::ToNativeString(env, redirectUrl)));
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_Framework_nativeGetPrivacyPolicyLink(JNIEnv * env, jclass)
{
return jni::ToJavaString(env, User::GetPrivacyPolicyLink());
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_Framework_nativeGetTermsOfUseLink(JNIEnv * env, jclass)
{
return jni::ToJavaString(env, User::GetTermsOfUseLink());
}
JNIEXPORT void JNICALL

View file

@ -418,9 +418,17 @@ public class Framework
public static native void nativeAuthenticateUser(@NonNull String socialToken,
@AuthTokenType int socialTokenType,
boolean privacyAccepted,
boolean termsAccepted,
boolean promoAccepted,
@NonNull AuthorizationListener listener);
public static native boolean nativeIsUserAuthenticated();
@NonNull
public static native String nativeGetPhoneAuthUrl(@NonNull String redirectUrl);
@NonNull
public static native String nativeGetPrivacyPolicyLink();
@NonNull
public static native String nativeGetTermsOfUseLink();
public static native void nativeShowFeatureByLatLon(double lat, double lon);

View file

@ -105,7 +105,10 @@ public class Authorizer implements AuthorizationListener
mIsAuthorizationInProgress = true;
if (mCallback != null)
mCallback.onAuthorizationStart();
Framework.nativeAuthenticateUser(socialToken, type, this);
//TODO: support privacy policy, terms of use and promo offers.
Framework.nativeAuthenticateUser(socialToken, type, false /* privacyAccepted */,
false /* termsAccepted */, false /* promoAccepted */, this);
}
}

View file

@ -99,7 +99,9 @@
});
};
user.AddSubscriber(std::move(s));
user.Authenticate(token.UTF8String, socialTokenType);
//TODO: support privacy policy, terms of use and promo offers.
user.Authenticate(token.UTF8String, socialTokenType, false /* privacyAccepted */,
false /* termsAccepted */, false /* promoAccepted */);
}
@end

View file

@ -33,6 +33,7 @@ std::string const kReviewIdsKey = "UserReviewIds";
std::string const kPassportServerUrl = PASSPORT_URL;
std::string const kAppName = PASSPORT_APP_NAME;
std::string const kUGCServerUrl = UGC_URL;
std::string const kApplicationJson = "application/json";
enum class ReviewReceiverProtocol : uint8_t
{
@ -53,14 +54,12 @@ std::string AuthenticationUrl(std::string const & socialToken,
{
case User::SocialTokenType::Facebook:
{
ss << "/register-by-token/facebook/?access_token=" << UrlEncode(socialToken)
<< "&app=" << kAppName;
ss << "/register-by-token/facebook/";
return ss.str();
}
case User::SocialTokenType::Google:
{
ss << "/register-by-token/google-oauth2/?access_token=" << UrlEncode(socialToken)
<< "&app=" << kAppName;
ss << "/register-by-token/google-oauth2/";
return ss.str();
}
case User::SocialTokenType::Phone:
@ -145,6 +144,25 @@ struct PhoneAuthRequestData
DECLARE_VISITOR(visitor(m_cliendId, "client_id"),
visitor(m_code, "code"))
};
struct SocialNetworkAuthRequestData
{
std::string m_accessToken;
std::string m_clientId;
std::string m_privacyLink;
std::string m_termsLink;
bool m_privacyAccepted = false;
bool m_termsAccepted = false;
bool m_promoAccepted = false;
DECLARE_VISITOR(visitor(m_accessToken, "access_token"),
visitor(m_clientId, "client_id"),
visitor(m_privacyLink, "privacy_link"),
visitor(m_termsLink, "terms_link"),
visitor(m_privacyAccepted, "privacy_accepted"),
visitor(m_termsAccepted, "terms_accepted"),
visitor(m_promoAccepted, "promo_accepted"))
};
template<typename DataType>
std::string SerializeToJson(DataType const & data)
@ -230,7 +248,8 @@ void User::SetAccessToken(std::string const & accessToken)
NotifySubscribersImpl();
}
void User::Authenticate(std::string const & socialToken, SocialTokenType socialTokenType)
void User::Authenticate(std::string const & socialToken, SocialTokenType socialTokenType,
bool privacyAccepted, bool termsAccepted, bool promoAccepted)
{
std::string const url = AuthenticationUrl(socialToken, socialTokenType);
if (url.empty())
@ -242,20 +261,36 @@ void User::Authenticate(std::string const & socialToken, SocialTokenType socialT
if (!StartAuthentication())
return;
BuildRequestHandler phoneAuthParams;
BuildRequestHandler authParams;
if (socialTokenType == SocialTokenType::Phone)
{
phoneAuthParams = [socialToken](platform::HttpClient & request)
authParams = [socialToken](platform::HttpClient & request)
{
auto jsonData = SerializeToJson(PhoneAuthRequestData(socialToken));
request.SetBodyData(jsonData, "application/json");
request.SetBodyData(jsonData, kApplicationJson);
};
}
else
{
SocialNetworkAuthRequestData authData;
authData.m_accessToken = socialToken;
authData.m_clientId = kAppName;
authData.m_termsLink = GetTermsOfUseLink();
authData.m_privacyLink = GetPrivacyPolicyLink();
authData.m_termsAccepted = termsAccepted;
authData.m_privacyAccepted = privacyAccepted;
authData.m_promoAccepted = promoAccepted;
authParams = [authData = std::move(authData)](platform::HttpClient & request)
{
auto jsonData = SerializeToJson(authData);
request.SetBodyData(jsonData, kApplicationJson);
};
}
GetPlatform().RunTask(Platform::Thread::Network,
[this, url, phoneAuthParams = std::move(phoneAuthParams)]()
[this, url, authParams = std::move(authParams)]()
{
Request(url, phoneAuthParams, [this](std::string const & response)
Request(url, authParams, [this](std::string const & response)
{
SetAccessToken(ParseAccessToken(response));
FinishAuthentication();
@ -376,7 +411,7 @@ void User::UploadUserReviews(std::string && dataStr, size_t numberOfUnsynchroniz
Request(url, [this, dataStr](platform::HttpClient & request)
{
request.SetRawHeader("Authorization", BuildAuthorizationToken(m_accessToken));
request.SetBodyData(dataStr, "application/json");
request.SetBodyData(dataStr, kApplicationJson);
},
[bytesCount, onCompleteUploading](std::string const &)
{
@ -411,6 +446,18 @@ std::string User::GetPhoneAuthUrl(std::string const & redirectUri)
return os.str();
}
// static
std::string User::GetPrivacyPolicyLink()
{
return "https://legal.my.com/us/maps/privacy/";
}
// static
std::string User::GetTermsOfUseLink()
{
return "https://legal.my.com/us/maps/tou/";
}
void User::Request(std::string const & url, BuildRequestHandler const & onBuildRequest,
SuccessHandler const & onSuccess, ErrorHandler const & onError)
{
@ -431,7 +478,7 @@ void User::RequestImpl(std::string const & url, BuildRequestHandler const & onBu
bool isSuccessfulCode = false;
platform::HttpClient request(url);
request.SetRawHeader("Accept", "application/json");
request.SetRawHeader("Accept", kApplicationJson);
if (onBuildRequest)
onBuildRequest(request);

View file

@ -50,7 +50,8 @@ public:
using CompleteUploadingHandler = std::function<void(bool)>;
User();
void Authenticate(std::string const & socialToken, SocialTokenType socialTokenType);
void Authenticate(std::string const & socialToken, SocialTokenType socialTokenType,
bool privacyAccepted, bool termsAccepted, bool promoAccepted);
bool IsAuthenticated() const;
void ResetAccessToken();
void UpdateUserDetails();
@ -65,6 +66,8 @@ public:
CompleteUploadingHandler const & onCompleteUploading);
static std::string GetPhoneAuthUrl(std::string const & redirectUri);
static std::string GetPrivacyPolicyLink();
static std::string GetTermsOfUseLink();
private:
void Init();