OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/test/refresh_token_store.h" |
| 6 |
| 7 #include "base/files/file_util.h" |
| 8 #include "base/logging.h" |
| 9 |
| 10 namespace { |
| 11 const char kTokenFileName[] = "refresh_token.txt"; |
| 12 |
| 13 // Returns the FilePath of the token store file for |user_name|. |
| 14 base::FilePath GetRefreshTokenDirPath(const std::string& user_name) { |
| 15 base::FilePath refresh_token_dir_path; |
| 16 if (!GetTempDir(&refresh_token_dir_path)) { |
| 17 LOG(WARNING) << "Failed to retrieve temporary directory path."; |
| 18 return base::FilePath(); |
| 19 } |
| 20 |
| 21 refresh_token_dir_path = refresh_token_dir_path.Append("remoting"); |
| 22 refresh_token_dir_path = refresh_token_dir_path.Append("refresh_token_store"); |
| 23 refresh_token_dir_path = refresh_token_dir_path.Append(user_name); |
| 24 |
| 25 return refresh_token_dir_path; |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 namespace remoting { |
| 31 namespace test { |
| 32 |
| 33 // Provides functionality to write a refresh token to a local folder on disk and |
| 34 // read it back during subsequent tool runs. |
| 35 class RefreshTokenStoreOnDisk : public RefreshTokenStore { |
| 36 public: |
| 37 RefreshTokenStoreOnDisk(); |
| 38 ~RefreshTokenStoreOnDisk() override; |
| 39 |
| 40 // RefreshTokenStore interface. |
| 41 std::string FetchRefreshToken(const std::string& user_name) override; |
| 42 bool StoreRefreshToken(const std::string& user_name, |
| 43 const std::string& refresh_token) override; |
| 44 }; |
| 45 |
| 46 RefreshTokenStoreOnDisk::RefreshTokenStoreOnDisk() {} |
| 47 |
| 48 RefreshTokenStoreOnDisk::~RefreshTokenStoreOnDisk() {} |
| 49 |
| 50 std::string RefreshTokenStoreOnDisk::FetchRefreshToken( |
| 51 const std::string& user_name) { |
| 52 DCHECK(!user_name.empty()); |
| 53 |
| 54 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); |
| 55 DCHECK(!token_dir_path.empty()); |
| 56 |
| 57 DVLOG(2) << "Reading token from path: " << token_dir_path.value(); |
| 58 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); |
| 59 |
| 60 std::string refresh_token; |
| 61 if (!base::ReadFileToString(token_file_path, &refresh_token)) { |
| 62 DVLOG(1) << "Failed to read token file from: " << token_dir_path.value(); |
| 63 return std::string(); |
| 64 } |
| 65 |
| 66 return refresh_token; |
| 67 } |
| 68 |
| 69 bool RefreshTokenStoreOnDisk::StoreRefreshToken( |
| 70 const std::string& user_name, |
| 71 const std::string& refresh_token) { |
| 72 DCHECK(!user_name.empty()); |
| 73 DCHECK(!refresh_token.empty()); |
| 74 |
| 75 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); |
| 76 if (token_dir_path.empty()) { |
| 77 return false; |
| 78 } |
| 79 |
| 80 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); |
| 81 if (!base::DirectoryExists(token_dir_path) && |
| 82 !base::CreateDirectory(token_dir_path)) { |
| 83 LOG(ERROR) << "Failed to create directory, refresh token not stored."; |
| 84 return false; |
| 85 } |
| 86 |
| 87 #if defined(OS_POSIX) |
| 88 // For POSIX we can set permissions on the token file so we do so here. |
| 89 // The test code should not run on other platforms since the code to safely |
| 90 // store the token has not been implemented yet. |
| 91 |
| 92 // Create an empty stub file if one does not exist. |
| 93 if (!base::PathExists(token_file_path) && |
| 94 base::WriteFile(token_file_path, "", 0) < 0) { |
| 95 LOG(ERROR) << "Failed to create stub file, refresh token not stored."; |
| 96 return false; |
| 97 } |
| 98 |
| 99 // Set permissions on the stub file. |
| 100 int mode = |
| 101 base::FILE_PERMISSION_READ_BY_USER | base::FILE_PERMISSION_WRITE_BY_USER; |
| 102 if (!SetPosixFilePermissions(token_file_path, mode)) { |
| 103 LOG(ERROR) << "Failed to set file permissions, refresh token not stored."; |
| 104 return false; |
| 105 } |
| 106 |
| 107 // Write the refresh token to our newly created file. |
| 108 if (base::WriteFile(token_file_path, refresh_token.c_str(), |
| 109 refresh_token.size()) < 0) { |
| 110 LOG(ERROR) << "Failed to save refresh token to the file on disk."; |
| 111 return false; |
| 112 } |
| 113 |
| 114 return true; |
| 115 #else |
| 116 NOTIMPLEMENTED() |
| 117 #endif // OS_POSIX |
| 118 } |
| 119 |
| 120 scoped_ptr<RefreshTokenStore> RefreshTokenStore::OnDisk() { |
| 121 return make_scoped_ptr<RefreshTokenStore>(new RefreshTokenStoreOnDisk()); |
| 122 } |
| 123 |
| 124 } // namespace test |
| 125 } // namespace remoting |
OLD | NEW |