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_storage.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. | |
15 base::FilePath GetRefreshTokenDirPath( | |
16 const std::string& user_name) { | |
17 base::FilePath refresh_token_dir_path; | |
18 if (!GetTempDir(&refresh_token_dir_path)) { | |
19 LOG(WARNING) << "Failed to retrieve temporary directory path."; | |
20 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.
| |
21 } | |
22 | |
23 // 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
| |
24 refresh_token_dir_path = refresh_token_dir_path.Append("remoting"); | |
25 refresh_token_dir_path = refresh_token_dir_path.Append("test_driver"); | |
26 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.
| |
27 | |
28 return refresh_token_dir_path; | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 namespace remoting { | |
34 namespace test { | |
35 | |
36 // Reads the refresh token from a file in a user unique directory if it exists | |
37 // and returns the value. | |
38 std::string RefreshTokenStorage::ReadRefreshTokenFromDisk( | |
39 const std::string& user_name) { | |
40 DCHECK(!user_name.empty()); | |
41 | |
42 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.
| |
43 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); | |
44 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.
| |
45 return refresh_token; | |
Wez
2015/02/13 03:01:54
You mean return std::string()?
joedow
2015/02/14 02:31:29
Acknowledged.
| |
46 } | |
47 | |
48 DVLOG(2) << "Reading token from path: " << token_dir_path.value(); | |
49 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); | |
50 | |
51 if (!base::ReadFileToString(token_file_path, &refresh_token)) { | |
52 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.
| |
53 } | |
54 | |
55 return refresh_token; | |
56 } | |
57 | |
58 // Stores the given refresh_token in a folder on the user's local disk. It | |
59 // will create the directory and file if they do not exist and set permissions | |
60 // on the local file so that it is only readable by the current user. | |
61 bool RefreshTokenStorage::WriteRefreshTokenToDisk( | |
62 const std::string& user_name, | |
63 const std::string& refresh_token) { | |
64 DCHECK(!user_name.empty()); | |
65 DCHECK(!refresh_token.empty()); | |
66 | |
67 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); | |
68 if (token_dir_path.empty()) { | |
69 return false; | |
70 } | |
71 | |
72 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); | |
73 if (!base::DirectoryExists(token_dir_path) && | |
74 !base::CreateDirectory(token_dir_path)) { | |
75 LOG(ERROR) << "Failed to create directory, refresh token not stored."; | |
76 return false; | |
77 } | |
78 | |
79 #if defined(OS_POSIX) | |
80 // For POSIX we can set permissions on the token file so we do so here. | |
81 // The test code should not run on other platforms since the code to safely | |
82 // store the token has not been implemented yet. | |
83 | |
84 // Create an empty stub file if one does not exist. | |
85 if (!base::PathExists(token_file_path) && | |
86 base::WriteFile(token_file_path, "", 0) < 0) { | |
87 LOG(ERROR) << "Failed to create stub file, refresh token not stored."; | |
88 return false; | |
89 } | |
90 | |
91 // Set permissions on the stub file. | |
92 int mode = base::FILE_PERMISSION_READ_BY_USER | | |
93 base::FILE_PERMISSION_WRITE_BY_USER; | |
94 if (!SetPosixFilePermissions(token_file_path, mode)) { | |
95 LOG(ERROR) << "Failed to set file permissions, refresh token not stored."; | |
96 return false; | |
97 } | |
98 | |
99 // Write the refresh token to our newly created file. | |
100 if (base::WriteFile(token_file_path, | |
101 refresh_token.c_str(), | |
102 refresh_token.size()) < 0) { | |
103 LOG(ERROR) << "Failed to save refresh token to the file on disk."; | |
104 return false; | |
105 } | |
106 | |
107 return true; | |
108 #else | |
109 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
| |
110 #endif // OS_POSIX | |
111 } | |
112 | |
113 } // namespace test | |
114 } // namespace remoting | |
OLD | NEW |