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 // Generates a FilePath using the user_name for use with the read/write | |
14 // 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.
| |
15 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
| |
16 base::FilePath refresh_token_dir_path; | |
17 if (!GetTempDir(&refresh_token_dir_path)) { | |
18 LOG(WARNING) << "Failed to retrieve temporary directory path."; | |
19 return base::FilePath(); | |
20 } | |
21 | |
22 // 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.
| |
23 refresh_token_dir_path = refresh_token_dir_path.Append("remoting"); | |
24 refresh_token_dir_path = refresh_token_dir_path.Append("test_driver"); | |
25 refresh_token_dir_path = refresh_token_dir_path.Append(user_name); | |
26 | |
27 return refresh_token_dir_path; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 namespace remoting { | |
33 namespace test { | |
34 | |
35 // Provides functionality to write a refresh token to a local folder on disk and | |
36 // read it back during subsequent tool runs. | |
37 class RefreshTokenStore : public RefreshTokenStoreInterface { | |
38 public: | |
39 RefreshTokenStore(); | |
40 ~RefreshTokenStore() override; | |
41 | |
42 // Reads the refresh token from a file in a user unique directory if it exists | |
43 // 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.
| |
44 std::string ReadRefreshTokenFromDisk(const std::string& user_name) override; | |
45 | |
46 // Stores the given refresh_token in a folder on the user's local disk. It | |
47 // will create the directory and file if they do not exist and set permissions | |
48 // on the local file so that it is only readable by the current user. Returns | |
49 // true if the token was successfully written otherwise false. | |
50 bool WriteRefreshTokenToDisk(const std::string& user_name, | |
51 const std::string& refresh_token) override; | |
52 }; | |
53 | |
54 scoped_ptr<RefreshTokenStoreInterface> GetRefreshTokenStoreForDisk() { | |
55 return make_scoped_ptr<RefreshTokenStore>(new RefreshTokenStore()); | |
56 } | |
57 | |
58 RefreshTokenStore::RefreshTokenStore() {} | |
59 | |
60 RefreshTokenStore::~RefreshTokenStore() {} | |
61 | |
62 std::string RefreshTokenStore::ReadRefreshTokenFromDisk( | |
63 const std::string& user_name) { | |
64 DCHECK(!user_name.empty()); | |
65 | |
66 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); | |
67 DCHECK(!token_dir_path.empty()); | |
68 | |
69 DVLOG(2) << "Reading token from path: " << token_dir_path.value(); | |
70 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); | |
71 | |
72 std::string refresh_token; | |
73 if (!base::ReadFileToString(token_file_path, &refresh_token)) { | |
74 DVLOG(1) << "Failed to read token file from: " << token_dir_path.value(); | |
75 return std::string(); | |
76 } | |
77 | |
78 return refresh_token; | |
79 } | |
80 | |
81 bool RefreshTokenStore::WriteRefreshTokenToDisk( | |
82 const std::string& user_name, | |
83 const std::string& refresh_token) { | |
84 DCHECK(!user_name.empty()); | |
85 DCHECK(!refresh_token.empty()); | |
86 | |
87 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); | |
88 if (token_dir_path.empty()) { | |
89 return false; | |
90 } | |
91 | |
92 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); | |
93 if (!base::DirectoryExists(token_dir_path) && | |
94 !base::CreateDirectory(token_dir_path)) { | |
95 LOG(ERROR) << "Failed to create directory, refresh token not stored."; | |
96 return false; | |
97 } | |
98 | |
99 #if defined(OS_POSIX) | |
100 // For POSIX we can set permissions on the token file so we do so here. | |
101 // The test code should not run on other platforms since the code to safely | |
102 // store the token has not been implemented yet. | |
103 | |
104 // Create an empty stub file if one does not exist. | |
105 if (!base::PathExists(token_file_path) && | |
106 base::WriteFile(token_file_path, "", 0) < 0) { | |
107 LOG(ERROR) << "Failed to create stub file, refresh token not stored."; | |
108 return false; | |
109 } | |
110 | |
111 // Set permissions on the stub file. | |
112 int mode = | |
113 base::FILE_PERMISSION_READ_BY_USER | base::FILE_PERMISSION_WRITE_BY_USER; | |
114 if (!SetPosixFilePermissions(token_file_path, mode)) { | |
115 LOG(ERROR) << "Failed to set file permissions, refresh token not stored."; | |
116 return false; | |
117 } | |
118 | |
119 // Write the refresh token to our newly created file. | |
120 if (base::WriteFile(token_file_path, refresh_token.c_str(), | |
121 refresh_token.size()) < 0) { | |
122 LOG(ERROR) << "Failed to save refresh token to the file on disk."; | |
123 return false; | |
124 } | |
125 | |
126 return true; | |
127 #else | |
128 NOTIMPLEMENTED() | |
129 #endif // OS_POSIX | |
130 } | |
131 | |
132 } // namespace test | |
133 } // namespace remoting | |
OLD | NEW |