Merge lp:~marcustomlinson/unity-scope-vimeo/request_token_from_run into lp:unity-scope-vimeo
- request_token_from_run
- Merge into 14.10
Proposed by
Marcus Tomlinson
Status: | Merged |
---|---|
Merged at revision: | 83 |
Proposed branch: | lp:~marcustomlinson/unity-scope-vimeo/request_token_from_run |
Merge into: | lp:unity-scope-vimeo |
Diff against target: |
475 lines (+126/-146) 7 files modified
CMakeLists.txt (+1/-1) include/vimeo/api/client.h (+4/-2) include/vimeo/scope/query.h (+2/-1) include/vimeo/scope/scope.h (+0/-18) src/vimeo/api/client.cpp (+112/-15) src/vimeo/scope/query.cpp (+5/-4) src/vimeo/scope/scope.cpp (+2/-105) |
To merge this branch: | bzr merge lp:~marcustomlinson/unity-scope-vimeo/request_token_from_run |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Unity API Team | Pending | ||
Review via email: mp+243650@code.launchpad.net |
Commit message
Create one OnlineAccountClient instance and pass it into Query and Preview objects. This instance is then used by an internal Client object to keep its config up to date (by refreshing the service statuses each time the token is needed).
This way the config can be completely encapsulated within Client, and Client can be encapsulated within Query and Preview.
Description of the change
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'CMakeLists.txt' | |||
2 | --- CMakeLists.txt 2014-11-18 17:34:05 +0000 | |||
3 | +++ CMakeLists.txt 2014-12-04 12:41:10 +0000 | |||
4 | @@ -49,7 +49,7 @@ | |||
5 | 49 | ${SCOPE_INCLUDE_DIRS} | 49 | ${SCOPE_INCLUDE_DIRS} |
6 | 50 | ) | 50 | ) |
7 | 51 | 51 | ||
9 | 52 | set(SCOPE_VERSION "1.0.1-${BZR_REVNO}") | 52 | set(SCOPE_VERSION "1.0.2-${BZR_REVNO}") |
10 | 53 | set(CMAKE_INSTALL_PREFIX /) | 53 | set(CMAKE_INSTALL_PREFIX /) |
11 | 54 | set(SCOPE_NAME "com.ubuntu.scopes.vimeo") | 54 | set(SCOPE_NAME "com.ubuntu.scopes.vimeo") |
12 | 55 | set(SCOPE_INSTALL_NAME "${SCOPE_NAME}_vimeo") | 55 | set(SCOPE_INSTALL_NAME "${SCOPE_NAME}_vimeo") |
13 | 56 | 56 | ||
14 | === modified file 'include/vimeo/api/client.h' | |||
15 | --- include/vimeo/api/client.h 2014-11-17 12:44:51 +0000 | |||
16 | +++ include/vimeo/api/client.h 2014-12-04 12:41:10 +0000 | |||
17 | @@ -23,6 +23,8 @@ | |||
18 | 23 | #include <vimeo/api/config.h> | 23 | #include <vimeo/api/config.h> |
19 | 24 | #include <vimeo/api/video.h> | 24 | #include <vimeo/api/video.h> |
20 | 25 | 25 | ||
21 | 26 | #include <unity/scopes/OnlineAccountClient.h> | ||
22 | 27 | |||
23 | 26 | #include <atomic> | 28 | #include <atomic> |
24 | 27 | #include <deque> | 29 | #include <deque> |
25 | 28 | #include <future> | 30 | #include <future> |
26 | @@ -44,7 +46,7 @@ | |||
27 | 44 | 46 | ||
28 | 45 | typedef std::deque<Video::Ptr> VideoList; | 47 | typedef std::deque<Video::Ptr> VideoList; |
29 | 46 | 48 | ||
31 | 47 | Client(Config::Ptr config); | 49 | Client(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client); |
32 | 48 | 50 | ||
33 | 49 | virtual ~Client() = default; | 51 | virtual ~Client() = default; |
34 | 50 | 52 | ||
35 | @@ -58,7 +60,7 @@ | |||
36 | 58 | 60 | ||
37 | 59 | virtual void cancel(); | 61 | virtual void cancel(); |
38 | 60 | 62 | ||
40 | 61 | virtual Config::Ptr config(); | 63 | virtual bool authenticated(); |
41 | 62 | 64 | ||
42 | 63 | protected: | 65 | protected: |
43 | 64 | class Priv; | 66 | class Priv; |
44 | 65 | 67 | ||
45 | === modified file 'include/vimeo/scope/query.h' | |||
46 | --- include/vimeo/scope/query.h 2014-06-25 14:41:07 +0000 | |||
47 | +++ include/vimeo/scope/query.h 2014-12-04 12:41:10 +0000 | |||
48 | @@ -30,7 +30,8 @@ | |||
49 | 30 | class Query: public unity::scopes::SearchQueryBase { | 30 | class Query: public unity::scopes::SearchQueryBase { |
50 | 31 | public: | 31 | public: |
51 | 32 | Query(const unity::scopes::CannedQuery &query, | 32 | Query(const unity::scopes::CannedQuery &query, |
53 | 33 | const unity::scopes::SearchMetadata &metadata, vimeo::api::Config::Ptr config); | 33 | const unity::scopes::SearchMetadata &metadata, |
54 | 34 | std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client); | ||
55 | 34 | 35 | ||
56 | 35 | ~Query() = default; | 36 | ~Query() = default; |
57 | 36 | 37 | ||
58 | 37 | 38 | ||
59 | === modified file 'include/vimeo/scope/scope.h' | |||
60 | --- include/vimeo/scope/scope.h 2014-11-03 10:04:16 +0000 | |||
61 | +++ include/vimeo/scope/scope.h 2014-12-04 12:41:10 +0000 | |||
62 | @@ -19,8 +19,6 @@ | |||
63 | 19 | #ifndef VIMEO_SCOPE_SCOPE_H_ | 19 | #ifndef VIMEO_SCOPE_SCOPE_H_ |
64 | 20 | #define VIMEO_SCOPE_SCOPE_H_ | 20 | #define VIMEO_SCOPE_SCOPE_H_ |
65 | 21 | 21 | ||
66 | 22 | #include <vimeo/api/config.h> | ||
67 | 23 | |||
68 | 24 | #include <unity/scopes/ScopeBase.h> | 22 | #include <unity/scopes/ScopeBase.h> |
69 | 25 | #include <unity/scopes/OnlineAccountClient.h> | 23 | #include <unity/scopes/OnlineAccountClient.h> |
70 | 26 | #include <unity/scopes/QueryBase.h> | 24 | #include <unity/scopes/QueryBase.h> |
71 | @@ -28,8 +26,6 @@ | |||
72 | 28 | #include <unity/scopes/QueryBase.h> | 26 | #include <unity/scopes/QueryBase.h> |
73 | 29 | #include <unity/scopes/PreviewQueryBase.h> | 27 | #include <unity/scopes/PreviewQueryBase.h> |
74 | 30 | 28 | ||
75 | 31 | #include <condition_variable> | ||
76 | 32 | |||
77 | 33 | namespace vimeo { | 29 | namespace vimeo { |
78 | 34 | 30 | ||
79 | 35 | namespace scope { | 31 | namespace scope { |
80 | @@ -48,20 +44,6 @@ | |||
81 | 48 | unity::scopes::SearchMetadata const&) override; | 44 | unity::scopes::SearchMetadata const&) override; |
82 | 49 | 45 | ||
83 | 50 | protected: | 46 | protected: |
84 | 51 | void anonymous_login(); | ||
85 | 52 | |||
86 | 53 | void service_update(unity::scopes::OnlineAccountClient::ServiceStatus const& status); | ||
87 | 54 | |||
88 | 55 | void update_config(); | ||
89 | 56 | |||
90 | 57 | void init_config(); | ||
91 | 58 | |||
92 | 59 | vimeo::api::Config::Ptr config_; | ||
93 | 60 | |||
94 | 61 | std::mutex config_mutex_; | ||
95 | 62 | |||
96 | 63 | std::condition_variable config_cond_; | ||
97 | 64 | |||
98 | 65 | std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client_; | 47 | std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client_; |
99 | 66 | }; | 48 | }; |
100 | 67 | 49 | ||
101 | 68 | 50 | ||
102 | === modified file 'src/vimeo/api/client.cpp' | |||
103 | --- src/vimeo/api/client.cpp 2014-11-17 12:54:17 +0000 | |||
104 | +++ src/vimeo/api/client.cpp 2014-12-04 12:41:10 +0000 | |||
105 | @@ -17,7 +17,10 @@ | |||
106 | 17 | */ | 17 | */ |
107 | 18 | 18 | ||
108 | 19 | #include <vimeo/api/client.h> | 19 | #include <vimeo/api/client.h> |
109 | 20 | #include <vimeo/api/login.h> | ||
110 | 20 | 21 | ||
111 | 22 | #include <boost/filesystem/operations.hpp> | ||
112 | 23 | #include <boost/filesystem/path.hpp> | ||
113 | 21 | #include <boost/iostreams/filtering_stream.hpp> | 24 | #include <boost/iostreams/filtering_stream.hpp> |
114 | 22 | #include <boost/iostreams/filter/gzip.hpp> | 25 | #include <boost/iostreams/filter/gzip.hpp> |
115 | 23 | #include <core/net/error.h> | 26 | #include <core/net/error.h> |
116 | @@ -26,7 +29,10 @@ | |||
117 | 26 | #include <core/net/http/response.h> | 29 | #include <core/net/http/response.h> |
118 | 27 | #include <json/json.h> | 30 | #include <json/json.h> |
119 | 28 | 31 | ||
120 | 32 | #include <fstream> | ||
121 | 33 | |||
122 | 29 | namespace http = core::net::http; | 34 | namespace http = core::net::http; |
123 | 35 | namespace fs = boost::filesystem; | ||
124 | 30 | namespace io = boost::iostreams; | 36 | namespace io = boost::iostreams; |
125 | 31 | namespace json = Json; | 37 | namespace json = Json; |
126 | 32 | namespace net = core::net; | 38 | namespace net = core::net; |
127 | @@ -34,6 +40,9 @@ | |||
128 | 34 | using namespace vimeo::api; | 40 | using namespace vimeo::api; |
129 | 35 | using namespace std; | 41 | using namespace std; |
130 | 36 | 42 | ||
131 | 43 | static const char* CLIENT_ID = "b6758ff9f929cdb9f45a8477732bdbc4c6a89c7e"; | ||
132 | 44 | static const char* CLIENT_SECRET = "a3222f38f799b3b528e29418fe062c02c677a249"; | ||
133 | 45 | |||
134 | 37 | namespace { | 46 | namespace { |
135 | 38 | 47 | ||
136 | 39 | template<typename T> | 48 | template<typename T> |
137 | @@ -50,9 +59,9 @@ | |||
138 | 50 | 59 | ||
139 | 51 | class Client::Priv { | 60 | class Client::Priv { |
140 | 52 | public: | 61 | public: |
144 | 53 | Priv(Config::Ptr config) : | 62 | Priv(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client) : |
145 | 54 | client_(http::make_client()), worker_ { [this]() {client_->run();} }, config_( | 63 | client_(http::make_client()), worker_ { [this]() {client_->run();} }, |
146 | 55 | config), cancelled_(false) { | 64 | oa_client_(oa_client), cancelled_(false) { |
147 | 56 | } | 65 | } |
148 | 57 | 66 | ||
149 | 58 | ~Priv() { | 67 | ~Priv() { |
150 | @@ -66,31 +75,36 @@ | |||
151 | 66 | 75 | ||
152 | 67 | std::thread worker_; | 76 | std::thread worker_; |
153 | 68 | 77 | ||
155 | 69 | Config::Ptr config_; | 78 | Config config_; |
156 | 79 | std::mutex config_mutex_; | ||
157 | 80 | |||
158 | 81 | std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client_; | ||
159 | 70 | 82 | ||
160 | 71 | std::atomic<bool> cancelled_; | 83 | std::atomic<bool> cancelled_; |
161 | 72 | 84 | ||
162 | 73 | void get(const net::Uri::Path &path, | 85 | void get(const net::Uri::Path &path, |
163 | 74 | const net::Uri::QueryParameters ¶meters, | 86 | const net::Uri::QueryParameters ¶meters, |
164 | 75 | http::Request::Handler &handler) { | 87 | http::Request::Handler &handler) { |
165 | 88 | std::lock_guard<std::mutex> lock(config_mutex_); | ||
166 | 89 | update_config(); | ||
167 | 76 | 90 | ||
168 | 77 | http::Request::Configuration configuration; | 91 | http::Request::Configuration configuration; |
169 | 78 | net::Uri::QueryParameters complete_parameters(parameters); | 92 | net::Uri::QueryParameters complete_parameters(parameters); |
170 | 79 | 93 | ||
172 | 80 | net::Uri uri = net::make_uri(config_->apiroot, path, | 94 | net::Uri uri = net::make_uri(config_.apiroot, path, |
173 | 81 | complete_parameters); | 95 | complete_parameters); |
174 | 82 | configuration.uri = client_->uri_to_string(uri); | 96 | configuration.uri = client_->uri_to_string(uri); |
176 | 83 | if (!config_->access_token.empty()) { | 97 | if (!config_.access_token.empty()) { |
177 | 84 | configuration.header.add("Authorization", | 98 | configuration.header.add("Authorization", |
180 | 85 | "bearer " + config_->access_token); | 99 | "bearer " + config_.access_token); |
181 | 86 | } else if (!config_->client_id.empty() && !config_->client_secret.empty()) { | 100 | } else if (!config_.client_id.empty() && !config_.client_secret.empty()) { |
182 | 87 | string auth = "basic " | 101 | string auth = "basic " |
183 | 88 | + client_->base64_encode( | 102 | + client_->base64_encode( |
185 | 89 | config_->client_id + ":" + config_->client_secret); | 103 | config_.client_id + ":" + config_.client_secret); |
186 | 90 | configuration.header.add("Authorization", auth); | 104 | configuration.header.add("Authorization", auth); |
187 | 91 | } | 105 | } |
190 | 92 | configuration.header.add("Accept", config_->accept); | 106 | configuration.header.add("Accept", config_.accept); |
191 | 93 | configuration.header.add("User-Agent", config_->user_agent + " (gzip)"); | 107 | configuration.header.add("User-Agent", config_.user_agent + " (gzip)"); |
192 | 94 | configuration.header.add("Accept-Encoding", "gzip"); | 108 | configuration.header.add("Accept-Encoding", "gzip"); |
193 | 95 | 109 | ||
194 | 96 | auto request = client_->head(configuration); | 110 | auto request = client_->head(configuration); |
195 | @@ -151,10 +165,93 @@ | |||
196 | 151 | 165 | ||
197 | 152 | return prom->get_future(); | 166 | return prom->get_future(); |
198 | 153 | } | 167 | } |
199 | 168 | |||
200 | 169 | bool authenticated() { | ||
201 | 170 | std::lock_guard<std::mutex> lock(config_mutex_); | ||
202 | 171 | update_config(); | ||
203 | 172 | return config_.authenticated; | ||
204 | 173 | } | ||
205 | 174 | |||
206 | 175 | void anonymous_login() { | ||
207 | 176 | fs::path saved_token_dir = fs::path(getenv("HOME")) | ||
208 | 177 | / ".local" / "share" / "unity-scopes" / "leaf-net" / SCOPE_NAME; | ||
209 | 178 | fs::path saved_token_path = saved_token_dir | ||
210 | 179 | / "anonymous_auth_token"; | ||
211 | 180 | |||
212 | 181 | bool save_auth_token = getenv("VIMEO_SCOPE_IGNORE_ACCOUNTS") == nullptr; | ||
213 | 182 | |||
214 | 183 | config_.client_id = CLIENT_ID; | ||
215 | 184 | config_.client_secret = CLIENT_SECRET; | ||
216 | 185 | |||
217 | 186 | if (fs::exists(saved_token_path) && save_auth_token) { | ||
218 | 187 | ifstream in(saved_token_path.native(), ios::in | ios::binary); | ||
219 | 188 | if (in) { | ||
220 | 189 | ostringstream contents; | ||
221 | 190 | contents << in.rdbuf(); | ||
222 | 191 | in.close(); | ||
223 | 192 | config_.access_token = contents.str(); | ||
224 | 193 | } | ||
225 | 194 | cerr << " re-using saved auth_token" << endl; | ||
226 | 195 | } else { | ||
227 | 196 | config_.access_token = unauthenticated(CLIENT_ID, CLIENT_SECRET, | ||
228 | 197 | config_.apiroot); | ||
229 | 198 | if (save_auth_token) { | ||
230 | 199 | fs::create_directories(saved_token_dir); | ||
231 | 200 | ofstream out(saved_token_path.native(), ios::out | ios::binary); | ||
232 | 201 | if (out) { | ||
233 | 202 | out << config_.access_token; | ||
234 | 203 | out.close(); | ||
235 | 204 | } | ||
236 | 205 | } | ||
237 | 206 | cerr << " new auth_token" << endl; | ||
238 | 207 | } | ||
239 | 208 | } | ||
240 | 209 | |||
241 | 210 | void update_config() { | ||
242 | 211 | if (getenv("VIMEO_SCOPE_APIROOT")) { | ||
243 | 212 | config_.apiroot = getenv("VIMEO_SCOPE_APIROOT"); | ||
244 | 213 | } | ||
245 | 214 | |||
246 | 215 | if (getenv("VIMEO_SCOPE_IGNORE_ACCOUNTS") != nullptr) { | ||
247 | 216 | return; | ||
248 | 217 | } | ||
249 | 218 | |||
250 | 219 | /// TODO: The code commented out below should be uncommented as soon as | ||
251 | 220 | /// OnlineAccountClient::refresh_service_statuses() is fixed (Bug #1398813). | ||
252 | 221 | /// For now we have to re-instantiate a new OnlineAccountClient each time. | ||
253 | 222 | |||
254 | 223 | ///if (oa_client_ == nullptr) { | ||
255 | 224 | oa_client_.reset( | ||
256 | 225 | new unity::scopes::OnlineAccountClient(SCOPE_INSTALL_NAME, | ||
257 | 226 | "sharing", SCOPE_ACCOUNTS_NAME)); | ||
258 | 227 | ///} else { | ||
259 | 228 | /// oa_client_->refresh_service_statuses(); | ||
260 | 229 | ///} | ||
261 | 230 | |||
262 | 231 | for (auto const& status : oa_client_->get_service_statuses()) { | ||
263 | 232 | if (status.service_authenticated) { | ||
264 | 233 | config_.authenticated = true; | ||
265 | 234 | config_.access_token = status.access_token; | ||
266 | 235 | config_.client_id = status.client_id; | ||
267 | 236 | config_.client_secret = status.client_secret; | ||
268 | 237 | break; | ||
269 | 238 | } | ||
270 | 239 | } | ||
271 | 240 | |||
272 | 241 | if (!config_.authenticated) { | ||
273 | 242 | config_.access_token = ""; | ||
274 | 243 | config_.client_id = ""; | ||
275 | 244 | config_.client_secret = ""; | ||
276 | 245 | std::cerr << "Vimeo scope is unauthenticated" << std::endl; | ||
277 | 246 | anonymous_login(); | ||
278 | 247 | } else { | ||
279 | 248 | std::cerr << "Vimeo scope is authenticated" << std::endl; | ||
280 | 249 | } | ||
281 | 250 | } | ||
282 | 154 | }; | 251 | }; |
283 | 155 | 252 | ||
286 | 156 | Client::Client(Config::Ptr config) : | 253 | Client::Client(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client) : |
287 | 157 | p(new Priv(config)) { | 254 | p(new Priv(oa_client)) { |
288 | 158 | } | 255 | } |
289 | 159 | 256 | ||
290 | 160 | future<Client::VideoList> Client::videos(const string &query) { | 257 | future<Client::VideoList> Client::videos(const string &query) { |
291 | @@ -190,7 +287,7 @@ | |||
292 | 190 | p->cancelled_ = true; | 287 | p->cancelled_ = true; |
293 | 191 | } | 288 | } |
294 | 192 | 289 | ||
297 | 193 | Config::Ptr Client::config() { | 290 | bool Client::authenticated() { |
298 | 194 | return p->config_; | 291 | return p->authenticated(); |
299 | 195 | } | 292 | } |
300 | 196 | 293 | ||
301 | 197 | 294 | ||
302 | === modified file 'src/vimeo/scope/query.cpp' | |||
303 | --- src/vimeo/scope/query.cpp 2014-11-17 12:44:51 +0000 | |||
304 | +++ src/vimeo/scope/query.cpp 2014-12-04 12:41:10 +0000 | |||
305 | @@ -88,8 +88,9 @@ | |||
306 | 88 | } | 88 | } |
307 | 89 | 89 | ||
308 | 90 | Query::Query(const sc::CannedQuery &query, const sc::SearchMetadata &metadata, | 90 | Query::Query(const sc::CannedQuery &query, const sc::SearchMetadata &metadata, |
311 | 91 | Config::Ptr config) : | 91 | std::shared_ptr<sc::OnlineAccountClient> oa_client) : |
312 | 92 | sc::SearchQueryBase(query, metadata), client_(config) { | 92 | sc::SearchQueryBase(query, metadata), |
313 | 93 | client_(oa_client) { | ||
314 | 93 | } | 94 | } |
315 | 94 | 95 | ||
316 | 95 | void Query::cancelled() { | 96 | void Query::cancelled() { |
317 | @@ -132,7 +133,7 @@ | |||
318 | 132 | "My Feed"); | 133 | "My Feed"); |
319 | 133 | sc::Department::SPtr dummy_dept; | 134 | sc::Department::SPtr dummy_dept; |
320 | 134 | 135 | ||
322 | 135 | bool include_login_nag = !client_.config()->authenticated; | 136 | bool include_login_nag = !client_.authenticated(); |
323 | 136 | 137 | ||
324 | 137 | if (query_string.empty()) { | 138 | if (query_string.empty()) { |
325 | 138 | channels_future = client_.channels(); | 139 | channels_future = client_.channels(); |
326 | @@ -147,7 +148,7 @@ | |||
327 | 147 | videos_future = client_.channels_videos("staffpicks"); | 148 | videos_future = client_.channels_videos("staffpicks"); |
328 | 148 | } else if (!query.department_id().empty()) { | 149 | } else if (!query.department_id().empty()) { |
329 | 149 | videos_future = client_.channels_videos(query.department_id()); | 150 | videos_future = client_.channels_videos(query.department_id()); |
331 | 150 | } else if (client_.config()->authenticated) { | 151 | } else if (client_.authenticated()) { |
332 | 151 | videos_future = client_.feed(); | 152 | videos_future = client_.feed(); |
333 | 152 | } else { | 153 | } else { |
334 | 153 | videos_future = client_.channels_videos("staffpicks"); | 154 | videos_future = client_.channels_videos("staffpicks"); |
335 | 154 | 155 | ||
336 | === modified file 'src/vimeo/scope/scope.cpp' | |||
337 | --- src/vimeo/scope/scope.cpp 2014-11-07 18:54:12 +0000 | |||
338 | +++ src/vimeo/scope/scope.cpp 2014-12-04 12:41:10 +0000 | |||
339 | @@ -16,102 +16,16 @@ | |||
340 | 16 | * Author: Pete Woods <pete.woods@canonical.com> | 16 | * Author: Pete Woods <pete.woods@canonical.com> |
341 | 17 | */ | 17 | */ |
342 | 18 | 18 | ||
344 | 19 | #include <vimeo/api/login.h> | 19 | #include <vimeo/scope/localisation.h> |
345 | 20 | #include <vimeo/scope/scope.h> | 20 | #include <vimeo/scope/scope.h> |
346 | 21 | #include <vimeo/scope/query.h> | 21 | #include <vimeo/scope/query.h> |
347 | 22 | #include <vimeo/scope/preview.h> | 22 | #include <vimeo/scope/preview.h> |
348 | 23 | 23 | ||
349 | 24 | #include <boost/filesystem/path.hpp> | ||
350 | 25 | #include <boost/filesystem/operations.hpp> | ||
351 | 26 | |||
352 | 27 | #include <iostream> | ||
353 | 28 | #include <sstream> | ||
354 | 29 | #include <fstream> | ||
355 | 30 | |||
356 | 31 | namespace sc = unity::scopes; | 24 | namespace sc = unity::scopes; |
357 | 32 | using namespace std; | 25 | using namespace std; |
358 | 33 | using namespace boost; | ||
359 | 34 | using namespace vimeo::scope; | 26 | using namespace vimeo::scope; |
360 | 35 | using namespace vimeo::api; | 27 | using namespace vimeo::api; |
361 | 36 | 28 | ||
362 | 37 | static const char* CLIENT_ID = "b6758ff9f929cdb9f45a8477732bdbc4c6a89c7e"; | ||
363 | 38 | static const char* CLIENT_SECRET = "a3222f38f799b3b528e29418fe062c02c677a249"; | ||
364 | 39 | |||
365 | 40 | void Scope::anonymous_login() { | ||
366 | 41 | filesystem::path saved_token_dir = filesystem::path(getenv("HOME")) | ||
367 | 42 | / ".local" / "share" / "unity-scopes" / "leaf-net" / SCOPE_NAME; | ||
368 | 43 | filesystem::path saved_token_path = saved_token_dir | ||
369 | 44 | / "anonymous_auth_token"; | ||
370 | 45 | |||
371 | 46 | bool save_auth_token = getenv("VIMEO_SCOPE_IGNORE_ACCOUNTS") == nullptr; | ||
372 | 47 | |||
373 | 48 | config_->client_id = CLIENT_ID; | ||
374 | 49 | config_->client_secret = CLIENT_SECRET; | ||
375 | 50 | |||
376 | 51 | if (filesystem::exists(saved_token_path) && save_auth_token) { | ||
377 | 52 | ifstream in(saved_token_path.native(), ios::in | ios::binary); | ||
378 | 53 | if (in) { | ||
379 | 54 | ostringstream contents; | ||
380 | 55 | contents << in.rdbuf(); | ||
381 | 56 | in.close(); | ||
382 | 57 | config_->access_token = contents.str(); | ||
383 | 58 | } | ||
384 | 59 | cerr << " re-using saved auth_token" << endl; | ||
385 | 60 | } else { | ||
386 | 61 | config_->access_token = unauthenticated(CLIENT_ID, CLIENT_SECRET, | ||
387 | 62 | config_->apiroot); | ||
388 | 63 | if (save_auth_token) { | ||
389 | 64 | filesystem::create_directories(saved_token_dir); | ||
390 | 65 | ofstream out(saved_token_path.native(), ios::out | ios::binary); | ||
391 | 66 | if (out) { | ||
392 | 67 | out << config_->access_token; | ||
393 | 68 | out.close(); | ||
394 | 69 | } | ||
395 | 70 | } | ||
396 | 71 | cerr << " new auth_token" << endl; | ||
397 | 72 | } | ||
398 | 73 | } | ||
399 | 74 | |||
400 | 75 | void Scope::service_update(sc::OnlineAccountClient::ServiceStatus const&) | ||
401 | 76 | { | ||
402 | 77 | update_config(); | ||
403 | 78 | } | ||
404 | 79 | |||
405 | 80 | void Scope::update_config() | ||
406 | 81 | { | ||
407 | 82 | std::lock_guard<std::mutex> lock(config_mutex_); | ||
408 | 83 | init_config(); | ||
409 | 84 | |||
410 | 85 | for (auto const& status : oa_client_->get_service_statuses()) | ||
411 | 86 | { | ||
412 | 87 | if (status.service_authenticated) | ||
413 | 88 | { | ||
414 | 89 | config_->authenticated = true; | ||
415 | 90 | config_->access_token = status.access_token; | ||
416 | 91 | config_->client_id = status.client_id; | ||
417 | 92 | config_->client_secret = status.client_secret; | ||
418 | 93 | break; | ||
419 | 94 | } | ||
420 | 95 | } | ||
421 | 96 | |||
422 | 97 | if (!config_->authenticated) { | ||
423 | 98 | cerr << "Vimeo scope is unauthenticated" << endl; | ||
424 | 99 | anonymous_login(); | ||
425 | 100 | } else { | ||
426 | 101 | cerr << "Vimeo scope is authenticated" << endl; | ||
427 | 102 | } | ||
428 | 103 | |||
429 | 104 | config_cond_.notify_all(); | ||
430 | 105 | } | ||
431 | 106 | |||
432 | 107 | void Scope::init_config() | ||
433 | 108 | { | ||
434 | 109 | config_ = make_shared<Config>(); | ||
435 | 110 | if (getenv("VIMEO_SCOPE_APIROOT")) { | ||
436 | 111 | config_->apiroot = getenv("VIMEO_SCOPE_APIROOT"); | ||
437 | 112 | } | ||
438 | 113 | } | ||
439 | 114 | |||
440 | 115 | void Scope::start(string const&) { | 29 | void Scope::start(string const&) { |
441 | 116 | setlocale(LC_ALL, ""); | 30 | setlocale(LC_ALL, ""); |
442 | 117 | string translation_directory = ScopeBase::scope_directory() | 31 | string translation_directory = ScopeBase::scope_directory() |
443 | @@ -122,23 +36,6 @@ | |||
444 | 122 | oa_client_.reset( | 36 | oa_client_.reset( |
445 | 123 | new sc::OnlineAccountClient(SCOPE_INSTALL_NAME, | 37 | new sc::OnlineAccountClient(SCOPE_INSTALL_NAME, |
446 | 124 | "sharing", SCOPE_ACCOUNTS_NAME)); | 38 | "sharing", SCOPE_ACCOUNTS_NAME)); |
447 | 125 | oa_client_->set_service_update_callback( | ||
448 | 126 | std::bind(&Scope::service_update, this, std::placeholders::_1)); | ||
449 | 127 | |||
450 | 128 | ///! TODO: We should only be waiting here if we know that there is at least one Google account enabled. | ||
451 | 129 | /// OnlineAccountClient needs to expose some functionality for us to determine that. | ||
452 | 130 | |||
453 | 131 | // Allow 1 second for the callback to initialize config_ | ||
454 | 132 | std::unique_lock<std::mutex> lock(config_mutex_); | ||
455 | 133 | config_cond_.wait_for(lock, std::chrono::seconds(1), [this] { return config_ != nullptr; }); | ||
456 | 134 | } | ||
457 | 135 | |||
458 | 136 | if (config_ == nullptr) | ||
459 | 137 | { | ||
460 | 138 | // If the callback was not invoked, default initialize config_ | ||
461 | 139 | init_config(); | ||
462 | 140 | cerr << "Vimeo scope is unauthenticated" << endl; | ||
463 | 141 | anonymous_login(); | ||
464 | 142 | } | 39 | } |
465 | 143 | } | 40 | } |
466 | 144 | 41 | ||
467 | @@ -147,7 +44,7 @@ | |||
468 | 147 | 44 | ||
469 | 148 | sc::SearchQueryBase::UPtr Scope::search(const sc::CannedQuery &query, | 45 | sc::SearchQueryBase::UPtr Scope::search(const sc::CannedQuery &query, |
470 | 149 | const sc::SearchMetadata &metadata) { | 46 | const sc::SearchMetadata &metadata) { |
472 | 150 | return sc::SearchQueryBase::UPtr(new Query(query, metadata, config_)); | 47 | return sc::SearchQueryBase::UPtr(new Query(query, metadata, oa_client_)); |
473 | 151 | } | 48 | } |
474 | 152 | 49 | ||
475 | 153 | sc::PreviewQueryBase::UPtr Scope::preview(sc::Result const& result, | 50 | sc::PreviewQueryBase::UPtr Scope::preview(sc::Result const& result, |