OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/client/client_util.h" | 5 #include "remoting/client/client_util.h" |
6 | 6 |
7 #include <iostream> | 7 #include <string> |
| 8 #include <vector> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/string_util.h" |
| 12 #include "remoting/client/client_config.h" |
| 13 |
| 14 using std::string; |
| 15 using std::vector; |
10 | 16 |
11 namespace remoting { | 17 namespace remoting { |
12 | 18 |
13 // Get host JID from command line arguments, or stdin if not specified. | 19 // Get host JID from command line arguments, or stdin if not specified. |
14 bool GetLoginInfo(int argc, char** argv, | 20 bool GetLoginInfoFromArgs(int argc, char** argv, ClientConfig* config) { |
15 std::string* host_jid, | |
16 std::string* username, | |
17 std::string* auth_token) { | |
18 bool found_host_jid = false; | 21 bool found_host_jid = false; |
19 bool found_jid = false; | 22 bool found_jid = false; |
20 bool found_auth_token = false; | 23 bool found_auth_token = false; |
| 24 string host_jid; |
| 25 string username; |
| 26 string auth_token; |
21 | 27 |
22 for (int i = 1; i < argc; i++) { | 28 for (int i = 1; i < argc; i++) { |
23 std::string arg = argv[i]; | 29 std::string arg = argv[i]; |
24 if (arg == "--host_jid") { | 30 if (arg == "--host_jid") { |
25 if (++i >= argc) { | 31 if (++i >= argc) { |
26 LOG(WARNING) << "Expected Host JID to follow --host_jid option" | 32 LOG(WARNING) << "Expected Host JID to follow --host_jid option" |
27 << std::endl; | 33 << std::endl; |
28 } else { | 34 } else { |
29 found_host_jid = true; | 35 found_host_jid = true; |
30 *host_jid = argv[i]; | 36 host_jid = argv[i]; |
31 } | 37 } |
32 } else if (arg == "--jid") { | 38 } else if (arg == "--jid") { |
33 if (++i >= argc) { | 39 if (++i >= argc) { |
34 LOG(WARNING) << "Expected JID to follow --jid option" << std::endl; | 40 LOG(WARNING) << "Expected JID to follow --jid option" << std::endl; |
35 } else { | 41 } else { |
36 found_jid = true; | 42 found_jid = true; |
37 *username = argv[i]; | 43 username = argv[i]; |
38 } | 44 } |
39 } else if (arg == "--token") { | 45 } else if (arg == "--token") { |
40 if (++i >= argc) { | 46 if (++i >= argc) { |
41 LOG(WARNING) << "Expected Auth token to follow --token option" | 47 LOG(WARNING) << "Expected Auth token to follow --token option" |
42 << std::endl; | 48 << std::endl; |
43 } else { | 49 } else { |
44 found_auth_token = true; | 50 found_auth_token = true; |
45 *auth_token = argv[i]; | 51 auth_token = argv[i]; |
46 } | 52 } |
47 } else { | 53 } else { |
48 LOG(WARNING) << "Unrecognized option: " << arg << std::endl; | 54 LOG(WARNING) << "Unrecognized option: " << arg << std::endl; |
49 } | 55 } |
50 } | 56 } |
51 | 57 |
52 if (!found_host_jid) { | 58 if (!found_host_jid) { |
53 std::cout << "Host JID: "; | 59 return false; |
54 std::cin >> *host_jid; | |
55 std::cin.ignore(); // Consume the leftover '\n' | |
56 } | 60 } |
57 | 61 |
58 // Validate the chromoting host JID. | 62 // Validate the chromoting host JID. |
59 if (host_jid->find("/chromoting") == std::string::npos) { | 63 if (host_jid.find("/chromoting") == std::string::npos) { |
60 std::cerr << "Error: Expected Host JID in format: <jid>/chromoting<id>" | |
61 << std::endl; | |
62 return false; | 64 return false; |
63 } | 65 } |
64 | 66 |
65 if (!found_jid) { | 67 if (!found_jid) { |
66 // Get username (JID). | 68 return false; |
67 // Extract default JID from host_jid. | |
68 std::string default_username; | |
69 size_t jid_end = host_jid->find('/'); | |
70 if (jid_end != std::string::npos) { | |
71 default_username = host_jid->substr(0, jid_end); | |
72 } | |
73 std::cout << "JID [" << default_username << "]: "; | |
74 getline(std::cin, *username); | |
75 if (username->length() == 0) { | |
76 username->swap(default_username); | |
77 } | |
78 if (username->length() == 0) { | |
79 std::cerr << "Error: Expected valid JID username" << std::endl; | |
80 return false; | |
81 } | |
82 } | 69 } |
83 | 70 |
84 if (!found_auth_token) { | 71 if (!found_auth_token) { |
85 // Get authentication token. | 72 return false; |
86 std::cout << "Auth token: "; | |
87 getline(std::cin, *auth_token); | |
88 std::cout << std::endl; | |
89 } | 73 } |
90 | 74 |
| 75 config->set_host_jid(host_jid); |
| 76 config->set_username(username); |
| 77 config->set_auth_token(auth_token); |
| 78 return true; |
| 79 } |
| 80 |
| 81 // Get host JID from command line arguments, or stdin if not specified. |
| 82 bool GetLoginInfoFromUrlParams(const std::string& url, ClientConfig* config) { |
| 83 // TODO(ajwong): We should use GURL or something. Don't parse this by hand! |
| 84 |
| 85 // The Url should be of the form: |
| 86 // |
| 87 // chrome://remoting?user=<userid>&auth=<authtoken>&jid=<hostjid> |
| 88 // |
| 89 vector<string> parts; |
| 90 SplitString(url, '&', &parts); |
| 91 if (parts.size() != 3) { |
| 92 return false; |
| 93 } |
| 94 |
| 95 size_t pos = parts[0].rfind('='); |
| 96 if (pos == string::npos && (pos + 1) != string::npos) { |
| 97 return false; |
| 98 } |
| 99 std::string username = parts[0].substr(pos + 1); |
| 100 |
| 101 pos = parts[1].rfind('='); |
| 102 if (pos == string::npos && (pos + 1) != string::npos) { |
| 103 return false; |
| 104 } |
| 105 std::string auth_token = parts[1].substr(pos + 1); |
| 106 |
| 107 pos = parts[2].rfind('='); |
| 108 if (pos == string::npos && (pos + 1) != string::npos) { |
| 109 return false; |
| 110 } |
| 111 std::string host_jid = parts[2].substr(pos + 1); |
| 112 |
| 113 config->set_host_jid(host_jid); |
| 114 config->set_username(username); |
| 115 config->set_auth_token(auth_token); |
91 return true; | 116 return true; |
92 } | 117 } |
93 | 118 |
94 } // namespace remoting | 119 } // namespace remoting |
OLD | NEW |