Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / remoting / host / setup / oauth_helper.cc
blob65f06bc3993c21e35d09657de93b6b955917226e
1 // Copyright (c) 2012 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.
5 #include "remoting/host/setup/oauth_helper.h"
7 #include "base/strings/stringprintf.h"
8 #include "google_apis/google_api_keys.h"
9 #include "net/base/escape.h"
10 #include "url/url_parse.h"
12 namespace {
14 std::string GetComponent(const std::string& url,
15 const url::Component component) {
16 if (component.len < 0) {
17 return std::string();
19 return url.substr(component.begin, component.len);
22 } // namespace
24 namespace remoting {
26 std::string GetOauthScope() {
27 return
28 "https://www.googleapis.com/auth/chromoting "
29 "https://www.googleapis.com/auth/googletalk "
30 "https://www.googleapis.com/auth/userinfo.email ";
33 std::string GetDefaultOauthRedirectUrl() {
34 return
35 "https://chromoting-oauth.talkgadget.google.com/talkgadget/oauth/"
36 "chrome-remote-desktop/rel/kgngmbheleoaphbjbaiobfdepmghbfah";
39 std::string GetOauthStartUrl(const std::string& redirect_url) {
40 return base::StringPrintf(
41 "https://accounts.google.com/o/oauth2/auth"
42 "?scope=%s"
43 "&redirect_uri=%s"
44 "&response_type=code"
45 "&client_id=%s"
46 "&access_type=offline"
47 "&approval_prompt=force",
48 net::EscapeUrlEncodedData(GetOauthScope(), true).c_str(),
49 redirect_url.c_str(),
50 net::EscapeUrlEncodedData(google_apis::GetOAuth2ClientID(
51 google_apis::CLIENT_REMOTING), true).c_str());
54 std::string GetOauthCodeInUrl(const std::string& url,
55 const std::string& redirect_url) {
56 url::Parsed url_parsed;
57 ParseStandardURL(url.c_str(), url.length(), &url_parsed);
58 url::Parsed redirect_url_parsed;
59 ParseStandardURL(redirect_url.c_str(), redirect_url.length(),
60 &redirect_url_parsed);
61 if (GetComponent(url, url_parsed.scheme) !=
62 GetComponent(redirect_url, redirect_url_parsed.scheme)) {
63 return std::string();
65 if (GetComponent(url, url_parsed.host) !=
66 GetComponent(redirect_url, redirect_url_parsed.host)) {
67 return std::string();
69 url::Component query = url_parsed.query;
70 url::Component key;
71 url::Component value;
72 while (ExtractQueryKeyValue(url.c_str(), &query, &key, &value)) {
73 if (GetComponent(url, key) == "code") {
74 return GetComponent(url, value);
77 return std::string();
80 } // namespace remoting