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 | |
Wez
2015/02/23 20:17:15
nit: Remove this blank line, so it's clear that "R
joedow
2015/02/24 02:01:24
Done.
| |
43 bool StoreRefreshToken(const std::string& user_name, | |
44 const std::string& refresh_token) override; | |
45 }; | |
46 | |
47 RefreshTokenStoreOnDisk::RefreshTokenStoreOnDisk() {} | |
48 | |
49 RefreshTokenStoreOnDisk::~RefreshTokenStoreOnDisk() {} | |
50 | |
51 std::string RefreshTokenStoreOnDisk::FetchRefreshToken( | |
52 const std::string& user_name) { | |
53 DCHECK(!user_name.empty()); | |
54 | |
55 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); | |
56 DCHECK(!token_dir_path.empty()); | |
57 | |
58 DVLOG(2) << "Reading token from path: " << token_dir_path.value(); | |
59 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); | |
60 | |
61 std::string refresh_token; | |
62 if (!base::ReadFileToString(token_file_path, &refresh_token)) { | |
63 DVLOG(1) << "Failed to read token file from: " << token_dir_path.value(); | |
64 return std::string(); | |
65 } | |
66 | |
67 return refresh_token; | |
68 } | |
69 | |
70 bool RefreshTokenStoreOnDisk::StoreRefreshToken( | |
71 const std::string& user_name, | |
72 const std::string& refresh_token) { | |
73 DCHECK(!user_name.empty()); | |
74 DCHECK(!refresh_token.empty()); | |
75 | |
76 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name)); | |
77 if (token_dir_path.empty()) { | |
78 return false; | |
79 } | |
80 | |
81 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); | |
82 if (!base::DirectoryExists(token_dir_path) && | |
83 !base::CreateDirectory(token_dir_path)) { | |
84 LOG(ERROR) << "Failed to create directory, refresh token not stored."; | |
85 return false; | |
86 } | |
87 | |
88 #if defined(OS_POSIX) | |
89 // For POSIX we can set permissions on the token file so we do so here. | |
90 // The test code should not run on other platforms since the code to safely | |
91 // store the token has not been implemented yet. | |
92 | |
93 // Create an empty stub file if one does not exist. | |
94 if (!base::PathExists(token_file_path) && | |
95 base::WriteFile(token_file_path, "", 0) < 0) { | |
96 LOG(ERROR) << "Failed to create stub file, refresh token not stored."; | |
97 return false; | |
98 } | |
99 | |
100 // Set permissions on the stub file. | |
101 int mode = | |
102 base::FILE_PERMISSION_READ_BY_USER | base::FILE_PERMISSION_WRITE_BY_USER; | |
103 if (!SetPosixFilePermissions(token_file_path, mode)) { | |
104 LOG(ERROR) << "Failed to set file permissions, refresh token not stored."; | |
105 return false; | |
106 } | |
107 | |
108 // Write the refresh token to our newly created file. | |
109 if (base::WriteFile(token_file_path, refresh_token.c_str(), | |
110 refresh_token.size()) < 0) { | |
111 LOG(ERROR) << "Failed to save refresh token to the file on disk."; | |
112 return false; | |
113 } | |
114 | |
115 return true; | |
116 #else | |
117 NOTIMPLEMENTED() | |
118 #endif // OS_POSIX | |
119 } | |
120 | |
121 scoped_ptr<RefreshTokenStore> RefreshTokenStore::OnDisk() { | |
122 return make_scoped_ptr<RefreshTokenStore>(new RefreshTokenStoreOnDisk()); | |
123 } | |
124 | |
125 } // namespace test | |
126 } // namespace remoting | |
OLD | NEW |