Skip to content

Commit

Permalink
Merge pull request #2 from Psiphon-Inc/buy-psi
Browse files Browse the repository at this point in the history
Add GetBuyPsiURL
  • Loading branch information
adam-p authored Jul 30, 2019
2 parents 48b60a2 + ef3ba92 commit f6a2346
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
11 changes: 11 additions & 0 deletions psicash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ Result<string> 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 {
Expand Down Expand Up @@ -331,6 +332,16 @@ Result<string> PsiCash::ModifyLandingPage(const string& url_string) const {
return url.ToString();
}

Result<string> 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<string> PsiCash::GetRewardedActivityData() const {
/*
The data is base64-encoded JSON-serialized with this structure:
Expand Down
4 changes: 4 additions & 0 deletions psicash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ class PsiCash {
/// proceed with the original URL.)
error::Result<std::string> 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<std::string> 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.
Expand Down
27 changes: 14 additions & 13 deletions psicash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,16 @@ 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);
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\":{},\"tokens\":null,\"v\":1}")));
"!"s + key_part + encoded_data);

url_in = {"https://asdf.sadf.gf", "gfaf=asdf", ""};
res = pc.ModifyLandingPage(url_in.ToString());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -688,15 +690,15 @@ 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());
ASSERT_TRUE(res);
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"};
Expand All @@ -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_);

//
Expand All @@ -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

Expand All @@ -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"},
Expand All @@ -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
Expand Down

0 comments on commit f6a2346

Please sign in to comment.