diff options
Diffstat (limited to 'cloud_print/gcp20')
-rw-r--r-- | cloud_print/gcp20/prototype/x_privet_token.cc | 15 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/x_privet_token.h | 2 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/x_privet_token_unittest.cc | 9 |
3 files changed, 12 insertions, 14 deletions
diff --git a/cloud_print/gcp20/prototype/x_privet_token.cc b/cloud_print/gcp20/prototype/x_privet_token.cc index b9afd10..df26d8e 100644 --- a/cloud_print/gcp20/prototype/x_privet_token.cc +++ b/cloud_print/gcp20/prototype/x_privet_token.cc @@ -16,6 +16,7 @@ namespace { const char kXPrivetTokenDelimeter = ':'; const uint64 kTimeExpiration = 24*60*60; // in seconds +const uint64 kTimeSecretRefresh = 24*60*60; // in seconds } // namespace @@ -33,17 +34,13 @@ XPrivetToken::XPrivetToken(const std::string& secret, } std::string XPrivetToken::GenerateXToken() { - if (Time::Now() > last_gen_time_ + TimeDelta::FromSeconds(kTimeExpiration)) + if (Time::Now() > last_gen_time_ + TimeDelta::FromSeconds(kTimeSecretRefresh)) UpdateSecret(); return GenerateXTokenWithTime(static_cast<uint64>(Time::Now().ToTimeT())); } -bool XPrivetToken::CheckValidXToken(const std::string& token_encoded) const { - std::string token; - if (!base::Base64Decode(token_encoded, &token)) - return false; - +bool XPrivetToken::CheckValidXToken(const std::string& token) const { size_t delimeter_pos = token.find(kXPrivetTokenDelimeter); if (delimeter_pos == std::string::npos) return false; @@ -53,7 +50,7 @@ bool XPrivetToken::CheckValidXToken(const std::string& token_encoded) const { if (!base::StringToUint64(issue_time_str, &issue_time)) return false; - if (GenerateXTokenWithTime(issue_time) != token_encoded) + if (GenerateXTokenWithTime(issue_time) != token) return false; return Time::FromTimeT(issue_time) - last_gen_time_ < @@ -66,8 +63,8 @@ std::string XPrivetToken::GenerateXTokenWithTime(uint64 issue_time) const { std::string hash = base::SHA1HashString(secret_ + kXPrivetTokenDelimeter + issue_time_str); - base::Base64Encode(hash + kXPrivetTokenDelimeter + issue_time_str, &result); - return result; + base::Base64Encode(hash, &result); + return result + kXPrivetTokenDelimeter + issue_time_str; } void XPrivetToken::UpdateSecret() { diff --git a/cloud_print/gcp20/prototype/x_privet_token.h b/cloud_print/gcp20/prototype/x_privet_token.h index edfc283..58c90f8 100644 --- a/cloud_print/gcp20/prototype/x_privet_token.h +++ b/cloud_print/gcp20/prototype/x_privet_token.h @@ -25,7 +25,7 @@ class XPrivetToken { std::string GenerateXToken(); // Checks - bool CheckValidXToken(const std::string& token_encoded) const; + bool CheckValidXToken(const std::string& token) const; private: FRIEND_TEST_ALL_PREFIXES(XPrivetTokenTest, Generation); diff --git a/cloud_print/gcp20/prototype/x_privet_token_unittest.cc b/cloud_print/gcp20/prototype/x_privet_token_unittest.cc index 4317459..38d783b 100644 --- a/cloud_print/gcp20/prototype/x_privet_token_unittest.cc +++ b/cloud_print/gcp20/prototype/x_privet_token_unittest.cc @@ -23,16 +23,17 @@ TEST(XPrivetTokenTest, Generation) { XPrivetToken xtoken(secret, base::Time::FromTimeT(gen_time)); - std::string issue_time_str = base::StringPrintf("%"PRIu64, issue_time); + std::string issue_time_str = base::StringPrintf("%" PRIu64, issue_time); std::string sha1_val = base::SHA1HashString(secret + ":" + issue_time_str); - EXPECT_STRCASEEQ("2216828f9eefc3931c1b9a110dcca3dbec23571d", + ASSERT_STRCASEEQ("2216828f9eefc3931c1b9a110dcca3dbec23571d", base::HexEncode(sha1_val.data(), sha1_val.size()).c_str()); std::string base64_val; - base::Base64Encode(sha1_val + ":" + issue_time_str, &base64_val); + base::Base64Encode(sha1_val, &base64_val); + std::string token = base64_val + ":" + issue_time_str; - EXPECT_EQ(base64_val, xtoken.GenerateXTokenWithTime(issue_time)); + ASSERT_EQ(token, xtoken.GenerateXTokenWithTime(issue_time)); EXPECT_NE(xtoken.GenerateXTokenWithTime(issue_time), xtoken.GenerateXTokenWithTime(issue_time + 1)); |