Index: remoting/test/refresh_token_storage.cc |
diff --git a/remoting/test/refresh_token_storage.cc b/remoting/test/refresh_token_storage.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..db13b508465d5bb8eaf2be3a996f8efe2eaed596 |
--- /dev/null |
+++ b/remoting/test/refresh_token_storage.cc |
@@ -0,0 +1,114 @@ |
+// 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_storage.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. |
+base::FilePath GetRefreshTokenDirPath( |
+ const std::string& user_name) { |
+ base::FilePath refresh_token_dir_path; |
+ if (!GetTempDir(&refresh_token_dir_path)) { |
+ LOG(WARNING) << "Failed to retrieve temporary directory path."; |
+ return refresh_token_dir_path; |
Wez
2015/02/13 03:01:54
nit: return base::FilePath() for clarity
joedow
2015/02/14 02:31:29
Done.
|
+ } |
+ |
+ // Build up the token path by appending the sub-directories. |
Wez
2015/02/13 03:01:54
What happens if multiple instances of tests run at
joedow
2015/02/14 02:31:29
Nothing interesting in this function, I think the
|
+ 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.c_str()); |
Wez
2015/02/13 03:01:54
Why c_str()?
joedow
2015/02/14 02:31:29
Done.
|
+ |
+ return refresh_token_dir_path; |
+} |
+ |
+} // namespace |
+ |
+namespace remoting { |
+namespace test { |
+ |
+// Reads the refresh token from a file in a user unique directory if it exists |
+// and returns the value. |
+std::string RefreshTokenStorage::ReadRefreshTokenFromDisk( |
+ const std::string& user_name) { |
+ DCHECK(!user_name.empty()); |
+ |
+ std::string refresh_token; |
Wez
2015/02/13 03:01:54
Declare this only when you actually need it, i.e.
joedow
2015/02/14 02:31:29
Done.
|
+ base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); |
+ if (token_dir_path.empty()) { |
Wez
2015/02/13 03:01:54
How can this ever be empty - just DCHECK here?
joedow
2015/02/14 02:31:29
Done.
|
+ return refresh_token; |
Wez
2015/02/13 03:01:54
You mean return std::string()?
joedow
2015/02/14 02:31:29
Acknowledged.
|
+ } |
+ |
+ DVLOG(2) << "Reading token from path: " << token_dir_path.value(); |
+ base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); |
+ |
+ if (!base::ReadFileToString(token_file_path, &refresh_token)) { |
+ DVLOG(1) << "Failed to read token file from: " << token_dir_path.value(); |
Wez
2015/02/13 03:01:54
nit: Suggest explicitly returning std::string() he
joedow
2015/02/14 02:31:29
Done.
|
+ } |
+ |
+ return refresh_token; |
+} |
+ |
+// 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. |
+bool RefreshTokenStorage::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() |
Wez
2015/02/13 03:01:54
Do we compile the code for the other platforms? If
joedow
2015/02/14 02:31:29
I have not limited the platforms yet as I think it
|
+#endif // OS_POSIX |
+} |
+ |
+} // namespace test |
+} // namespace remoting |