From ef3ba92c6e8d771205064a634530c07aebfffcbf Mon Sep 17 00:00:00 2001 From: Adam Pritchard Date: Tue, 30 Jul 2019 11:05:44 -0400 Subject: [PATCH] Add GetBuyPsiURL --- psicash.cpp | 11 +++++++++++ psicash.hpp | 4 ++++ psicash_test.cpp | 27 ++++++++++++++------------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/psicash.cpp b/psicash.cpp index 7d7a709..c94d6bb 100644 --- a/psicash.cpp +++ b/psicash.cpp @@ -296,6 +296,7 @@ Result PsiCash::ModifyLandingPage(const string& url_string) const { // Get the metadata (sponsor ID, etc.) psicash_data["metadata"] = user_data_->GetRequestMetadata(); + psicash_data["metadata"]["user_agent"] = user_agent_; string json_data; try { @@ -331,6 +332,16 @@ Result PsiCash::ModifyLandingPage(const string& url_string) const { return url.ToString(); } +Result PsiCash::GetBuyPsiURL() const { + // This is just a special case of the landing page format, EXCEPT that tokens MUST be + // present, or else it's an error. + auto auth_tokens = user_data_->GetAuthTokens(); + if (auth_tokens.count(kEarnerTokenType) == 0) { + return MakeNoncriticalError("no earner token available"); + } + return ModifyLandingPage("https://buy.psi.cash/"); +} + Result PsiCash::GetRewardedActivityData() const { /* The data is base64-encoded JSON-serialized with this structure: diff --git a/psicash.hpp b/psicash.hpp index 83ea62f..d0ceee6 100644 --- a/psicash.hpp +++ b/psicash.hpp @@ -231,6 +231,10 @@ class PsiCash { /// proceed with the original URL.) error::Result ModifyLandingPage(const std::string& url) const; + /// Utilizes stored tokens and metadata (and a configured base URL) to craft a URL + /// where the user can buy PsiCash for real money. + error::Result GetBuyPsiURL() const; + /// Creates a data package that should be included with a webhook for a user /// action that should be rewarded (such as watching a rewarded video). /// NOTE: The resulting string will still need to be encoded for use in a URL. diff --git a/psicash_test.cpp b/psicash_test.cpp index 9b47c42..0d84820 100644 --- a/psicash_test.cpp +++ b/psicash_test.cpp @@ -654,6 +654,8 @@ TEST_F(TestPsiCash, ModifyLandingPage) { // No metadata set // + auto encoded_data = base64::TrimPadding(base64::B64Encode(utils::Stringer(R"({"metadata":{"user_agent":")", user_agent_, R"("},"tokens":null,"v":1})"))); + url_in = {"https://asdf.sadf.gf", "", ""}; auto res = pc.ModifyLandingPage(url_in.ToString()); ASSERT_TRUE(res); @@ -661,7 +663,7 @@ TEST_F(TestPsiCash, ModifyLandingPage) { ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, url_in.query_); ASSERT_EQ(url_out.fragment_, - "!"s + key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{},\"tokens\":null,\"v\":1}"))); + "!"s + key_part + encoded_data); url_in = {"https://asdf.sadf.gf", "gfaf=asdf", ""}; res = pc.ModifyLandingPage(url_in.ToString()); @@ -670,7 +672,7 @@ TEST_F(TestPsiCash, ModifyLandingPage) { ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, url_in.query_); ASSERT_EQ(url_out.fragment_, - "!"s + key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{},\"tokens\":null,\"v\":1}"))); + "!"s + key_part + encoded_data); url_in = {"https://asdf.sadf.gf/asdfilj/adf", "gfaf=asdf", ""}; res = pc.ModifyLandingPage(url_in.ToString()); @@ -679,7 +681,7 @@ TEST_F(TestPsiCash, ModifyLandingPage) { ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, url_in.query_); ASSERT_EQ(url_out.fragment_, - "!"s + key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{},\"tokens\":null,\"v\":1}"))); + "!"s + key_part + encoded_data); url_in = {"https://asdf.sadf.gf/asdfilj/adf.html", "gfaf=asdf", ""}; res = pc.ModifyLandingPage(url_in.ToString()); @@ -688,7 +690,7 @@ TEST_F(TestPsiCash, ModifyLandingPage) { ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, url_in.query_); ASSERT_EQ(url_out.fragment_, - "!"s + key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{},\"tokens\":null,\"v\":1}"))); + "!"s + key_part + encoded_data); url_in = {"https://asdf.sadf.gf/asdfilj/adf.html", "", "regffd"}; res = pc.ModifyLandingPage(url_in.ToString()); @@ -696,7 +698,7 @@ TEST_F(TestPsiCash, ModifyLandingPage) { url_out.Parse(*res); ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, - key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{},\"tokens\":null,\"v\":1}"))); + key_part + encoded_data); ASSERT_EQ(url_out.fragment_, url_in.fragment_); url_in = {"https://asdf.sadf.gf/asdfilj/adf.html", "adfg=asdf&vfjnk=fadjn", "regffd"}; @@ -705,8 +707,7 @@ TEST_F(TestPsiCash, ModifyLandingPage) { url_out.Parse(*res); ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, - url_in.query_ + "&" + key_part + - base64::TrimPadding(base64::B64Encode("{\"metadata\":{},\"tokens\":null,\"v\":1}"))); + url_in.query_ + "&" + key_part + encoded_data); ASSERT_EQ(url_out.fragment_, url_in.fragment_); // @@ -721,8 +722,8 @@ TEST_F(TestPsiCash, ModifyLandingPage) { url_out.Parse(*res); ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, url_in.query_); - ASSERT_EQ(url_out.fragment_, - "!"s + key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{\"k\":\"v\"},\"tokens\":null,\"v\":1}"))); + encoded_data = base64::TrimPadding(base64::B64Encode(utils::Stringer(R"({"metadata":{"k":"v","user_agent":")", user_agent_, R"("},"tokens":null,"v":1})"))); + ASSERT_EQ(url_out.fragment_, "!"s + key_part + encoded_data); // With tokens @@ -737,8 +738,8 @@ TEST_F(TestPsiCash, ModifyLandingPage) { url_out.Parse(*res); ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, url_in.query_); - ASSERT_EQ(url_out.fragment_, - "!"s + key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{\"k\":\"v\"},\"tokens\":\"kEarnerTokenType\",\"v\":1}"))); + encoded_data = base64::TrimPadding(base64::B64Encode(utils::Stringer(R"({"metadata":{"k":"v","user_agent":")", user_agent_, R"("},"tokens":"kEarnerTokenType","v":1})"))); + ASSERT_EQ(url_out.fragment_, "!"s + key_part + encoded_data); // Some tokens, but no earner token (different code path) auth_tokens = {{kSpenderTokenType, "kSpenderTokenType"}, @@ -751,8 +752,8 @@ TEST_F(TestPsiCash, ModifyLandingPage) { url_out.Parse(*res); ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_); ASSERT_EQ(url_out.query_, url_in.query_); - ASSERT_EQ(url_out.fragment_, - "!"s + key_part + base64::TrimPadding(base64::B64Encode("{\"metadata\":{\"k\":\"v\"},\"tokens\":null,\"v\":1}"))); + encoded_data = base64::TrimPadding(base64::B64Encode(utils::Stringer(R"({"metadata":{"k":"v","user_agent":")", user_agent_, R"("},"tokens":null,"v":1})"))); + ASSERT_EQ(url_out.fragment_, "!"s + key_part + encoded_data); // // Errors