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(const std::string user_name); | |
38 ~RefreshTokenStoreOnDisk() override; | |
39 | |
40 // RefreshTokenStore interface. | |
41 std::string FetchRefreshToken() override; | |
42 bool StoreRefreshToken(const std::string& refresh_token) override; | |
43 | |
44 private: | |
45 // Used to access the user specific token file. | |
46 std::string user_name_; | |
Wez
2015/02/25 03:53:43
DISALLOW_COPY_AND_ASSIGN
| |
47 }; | |
48 | |
49 RefreshTokenStoreOnDisk::RefreshTokenStoreOnDisk(const std::string user_name) : | |
50 user_name_(user_name) {} | |
51 | |
52 RefreshTokenStoreOnDisk::~RefreshTokenStoreOnDisk() {} | |
53 | |
54 std::string RefreshTokenStoreOnDisk::FetchRefreshToken() { | |
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& refresh_token) { | |
72 DCHECK(!refresh_token.empty()); | |
73 | |
74 base::FilePath token_dir_path(GetRefreshTokenDirPath(user_name_)); | |
75 if (token_dir_path.empty()) { | |
76 return false; | |
77 } | |
78 | |
79 base::FilePath token_file_path(token_dir_path.Append(kTokenFileName)); | |
80 if (!base::DirectoryExists(token_dir_path) && | |
81 !base::CreateDirectory(token_dir_path)) { | |
82 LOG(ERROR) << "Failed to create directory, refresh token not stored."; | |
83 return false; | |
84 } | |
85 | |
86 #if defined(OS_POSIX) | |
87 // For POSIX we can set permissions on the token file so we do so here. | |
88 // The test code should not run on other platforms since the code to safely | |
89 // store the token has not been implemented yet. | |
90 | |
91 // Create an empty stub file if one does not exist. | |
92 if (!base::PathExists(token_file_path) && | |
93 base::WriteFile(token_file_path, "", 0) < 0) { | |
94 LOG(ERROR) << "Failed to create stub file, refresh token not stored."; | |
95 return false; | |
96 } | |
97 | |
98 // Set permissions on the stub file. | |
99 int mode = | |
100 base::FILE_PERMISSION_READ_BY_USER | base::FILE_PERMISSION_WRITE_BY_USER; | |
101 if (!SetPosixFilePermissions(token_file_path, mode)) { | |
102 LOG(ERROR) << "Failed to set file permissions, refresh token not stored."; | |
103 return false; | |
104 } | |
105 | |
106 // Write the refresh token to our newly created file. | |
107 if (base::WriteFile(token_file_path, refresh_token.c_str(), | |
108 refresh_token.size()) < 0) { | |
109 LOG(ERROR) << "Failed to save refresh token to the file on disk."; | |
110 return false; | |
111 } | |
112 | |
113 return true; | |
114 #else | |
115 NOTIMPLEMENTED() | |
116 #endif // OS_POSIX | |
117 } | |
118 | |
119 scoped_ptr<RefreshTokenStore> RefreshTokenStore::OnDisk( | |
120 const std::string& user_name) { | |
121 return make_scoped_ptr<RefreshTokenStore>(new RefreshTokenStoreOnDisk( | |
122 user_name)); | |
123 } | |
124 | |
125 } // namespace test | |
126 } // namespace remoting | |
OLD | NEW |