Index: remoting/test/refresh_token_store.cc |
diff --git a/remoting/test/refresh_token_store.cc b/remoting/test/refresh_token_store.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..63e91b1225df2f1f7cb0c3685f4e6fe59c98b474 |
--- /dev/null |
+++ b/remoting/test/refresh_token_store.cc |
@@ -0,0 +1,133 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/test/refresh_token_store.h" |
+ |
+#include "base/files/file_util.h" |
+#include "base/logging.h" |
+ |
+namespace { |
+const char kTokenFileName[] = "refresh_token.txt"; |
+ |
+// Generates a FilePath using the user_name for use with the read/write |
+// functions in this file. |
Wez
2015/02/19 22:00:24
"Returns the FilePath of the token store file for
joedow
2015/02/20 02:58:36
Done.
|
+base::FilePath GetRefreshTokenDirPath(const std::string& user_name) { |
Wez
2015/02/19 22:00:24
You never use the directory path on it's own; you
joedow
2015/02/20 02:58:36
I use the path w/o file name to see if the directo
Wez
2015/02/23 20:17:14
Ah yes. It may be clearer to construct the file f
|
+ base::FilePath refresh_token_dir_path; |
+ if (!GetTempDir(&refresh_token_dir_path)) { |
+ LOG(WARNING) << "Failed to retrieve temporary directory path."; |
+ return base::FilePath(); |
+ } |
+ |
+ // Build up the token path by appending the sub-directories. |
Wez
2015/02/19 22:00:24
We can see that you're appending directories; no n
joedow
2015/02/20 02:58:36
Done.
|
+ refresh_token_dir_path = refresh_token_dir_path.Append("remoting"); |
+ refresh_token_dir_path = refresh_token_dir_path.Append("test_driver"); |
+ refresh_token_dir_path = refresh_token_dir_path.Append(user_name); |
+ |
+ return refresh_token_dir_path; |
+} |
+ |
+} // namespace |
+ |
+namespace remoting { |
+namespace test { |
+ |
+// Provides functionality to write a refresh token to a local folder on disk and |
+// read it back during subsequent tool runs. |
+class RefreshTokenStore : public RefreshTokenStoreInterface { |
+ public: |
+ RefreshTokenStore(); |
+ ~RefreshTokenStore() override; |
+ |
+ // Reads the refresh token from a file in a user unique directory if it exists |
+ // and returns the refresh token value if successful or empty string if not. |
Wez
2015/02/19 22:00:24
This and the method below are inherited from the i
joedow
2015/02/20 02:58:36
Done.
|
+ std::string ReadRefreshTokenFromDisk(const std::string& user_name) override; |
+ |
+ // Stores the given refresh_token in a folder on the user's local disk. It |
+ // will create the directory and file if they do not exist and set permissions |
+ // on the local file so that it is only readable by the current user. Returns |
+ // true if the token was successfully written otherwise false. |
+ bool WriteRefreshTokenToDisk(const std::string& user_name, |
+ const std::string& refresh_token) override; |
+}; |
+ |
+scoped_ptr<RefreshTokenStoreInterface> GetRefreshTokenStoreForDisk() { |
+ return make_scoped_ptr<RefreshTokenStore>(new RefreshTokenStore()); |
+} |
+ |
+RefreshTokenStore::RefreshTokenStore() {} |
+ |
+RefreshTokenStore::~RefreshTokenStore() {} |
+ |
+std::string RefreshTokenStore::ReadRefreshTokenFromDisk( |
+ const std::string& user_name) { |
+ DCHECK(!user_name.empty()); |
+ |
+ base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); |
+ DCHECK(!token_dir_path.empty()); |
+ |
+ DVLOG(2) << "Reading token from path: " << token_dir_path.value(); |
+ base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); |
+ |
+ std::string refresh_token; |
+ if (!base::ReadFileToString(token_file_path, &refresh_token)) { |
+ DVLOG(1) << "Failed to read token file from: " << token_dir_path.value(); |
+ return std::string(); |
+ } |
+ |
+ return refresh_token; |
+} |
+ |
+bool RefreshTokenStore::WriteRefreshTokenToDisk( |
+ const std::string& user_name, |
+ const std::string& refresh_token) { |
+ DCHECK(!user_name.empty()); |
+ DCHECK(!refresh_token.empty()); |
+ |
+ base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); |
+ if (token_dir_path.empty()) { |
+ return false; |
+ } |
+ |
+ base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); |
+ if (!base::DirectoryExists(token_dir_path) && |
+ !base::CreateDirectory(token_dir_path)) { |
+ LOG(ERROR) << "Failed to create directory, refresh token not stored."; |
+ return false; |
+ } |
+ |
+#if defined(OS_POSIX) |
+ // For POSIX we can set permissions on the token file so we do so here. |
+ // The test code should not run on other platforms since the code to safely |
+ // store the token has not been implemented yet. |
+ |
+ // Create an empty stub file if one does not exist. |
+ if (!base::PathExists(token_file_path) && |
+ base::WriteFile(token_file_path, "", 0) < 0) { |
+ LOG(ERROR) << "Failed to create stub file, refresh token not stored."; |
+ return false; |
+ } |
+ |
+ // Set permissions on the stub file. |
+ int mode = |
+ base::FILE_PERMISSION_READ_BY_USER | base::FILE_PERMISSION_WRITE_BY_USER; |
+ if (!SetPosixFilePermissions(token_file_path, mode)) { |
+ LOG(ERROR) << "Failed to set file permissions, refresh token not stored."; |
+ return false; |
+ } |
+ |
+ // Write the refresh token to our newly created file. |
+ if (base::WriteFile(token_file_path, refresh_token.c_str(), |
+ refresh_token.size()) < 0) { |
+ LOG(ERROR) << "Failed to save refresh token to the file on disk."; |
+ return false; |
+ } |
+ |
+ return true; |
+#else |
+ NOTIMPLEMENTED() |
+#endif // OS_POSIX |
+} |
+ |
+} // namespace test |
+} // namespace remoting |