Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/browser/extensions/api/identity/gaia_web_auth_flow.cc

Issue 15148007: Identity API: web-based scope approval dialogs for getAuthToken (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: windows build fix Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h"
6
7 #include "base/string_util.h"
8 #include "base/stringprintf.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "google_apis/gaia/gaia_urls.h"
12 #include "net/base/escape.h"
13
14 namespace extensions {
15
16 GaiaWebAuthFlow::GaiaWebAuthFlow(Delegate* delegate,
17 Profile* profile,
18 chrome::HostDesktopType host_desktop_type,
19 const std::string& extension_id,
20 const OAuth2Info& oauth2_info)
21 : delegate_(delegate),
22 profile_(profile),
23 host_desktop_type_(host_desktop_type) {
24 const char kOAuth2RedirectPathFormat[] = "/%s#";
25 const char kOAuth2AuthorizeFormat[] =
26 "%s?response_type=token&approval_prompt=force&authuser=0&"
27 "client_id=%s&"
28 "scope=%s&"
29 "origin=chrome-extension://%s/&"
30 "redirect_uri=%s:/%s";
31
32 std::vector<std::string> client_id_parts;
33 base::SplitString(oauth2_info.client_id, '.', &client_id_parts);
34 std::reverse(client_id_parts.begin(), client_id_parts.end());
35 redirect_scheme_ = JoinString(client_id_parts, '.');
36
37 redirect_path_prefix_ =
38 base::StringPrintf(kOAuth2RedirectPathFormat, extension_id.c_str());
39
40 auth_url_ = base::StringPrintf(
41 kOAuth2AuthorizeFormat,
42 GaiaUrls::GetInstance()->oauth2_auth_url().c_str(),
43 oauth2_info.client_id.c_str(),
44 net::EscapeUrlEncodedData(JoinString(oauth2_info.scopes, ' '), true)
45 .c_str(),
46 extension_id.c_str(),
47 redirect_scheme_.c_str(),
48 extension_id.c_str());
49 }
50
51 GaiaWebAuthFlow::~GaiaWebAuthFlow() {}
52
53 void GaiaWebAuthFlow::Start() {
54 ubertoken_fetcher_.reset(new UbertokenFetcher(profile_, this));
55 ubertoken_fetcher_->StartFetchingToken();
56 }
57
58 void GaiaWebAuthFlow::OnUbertokenSuccess(const std::string& token) {
59 const char kMergeSessionQueryFormat[] = "?uberauth=%s&"
60 "continue=%s&"
61 "source=appsv2";
62
63 std::string merge_query =
64 base::StringPrintf(kMergeSessionQueryFormat,
65 net::EscapeUrlEncodedData(token, true).c_str(),
66 net::EscapeUrlEncodedData(auth_url_, true).c_str());
67 GURL merge_url(GaiaUrls::GetInstance()->merge_session_url() + merge_query);
68
69 web_flow_ = CreateWebAuthFlow(merge_url);
70 web_flow_->Start();
71 }
72
73 void GaiaWebAuthFlow::OnUbertokenFailure(const GoogleServiceAuthError& error) {
74 delegate_->OnGaiaFlowFailure(GaiaWebAuthFlow::SERVICE_AUTH_ERROR, error);
75 }
76
77 void GaiaWebAuthFlow::OnAuthFlowFailure(WebAuthFlow::Failure failure) {
78 DCHECK(failure == WebAuthFlow::WINDOW_CLOSED);
79 delegate_->OnGaiaFlowFailure(
80 GaiaWebAuthFlow::WINDOW_CLOSED,
81 GoogleServiceAuthError(GoogleServiceAuthError::NONE));
82 }
83
84 void GaiaWebAuthFlow::OnAuthFlowURLChange(const GURL& url) {
85 const char kOAuth2RedirectAccessTokenKey[] = "access_token";
86 const char kOAuth2RedirectErrorKey[] = "error";
87 const char kOAuth2ExpiresInKey[] = "expires_in";
88
89 // The format of the target URL is:
90 // reversed.oauth.client.id:/extensionid#access_token=TOKEN
91 //
92 // Because there is no double slash, everything after the scheme is
93 // interpreted as a path, including the fragment.
94
95 if (url.scheme() == redirect_scheme_ && !url.has_host() && !url.has_port() &&
96 StartsWithASCII(url.path(), redirect_path_prefix_, true)) {
97 web_flow_.reset();
98
99 std::string fragment =
100 url.path().substr(redirect_path_prefix_.length(), std::string::npos);
101 std::vector<std::pair<std::string, std::string> > pairs;
102 base::SplitStringIntoKeyValuePairs(fragment, '=', '&', &pairs);
103 std::string access_token;
104 std::string error;
105 std::string expiration;
106
107 for (std::vector<std::pair<std::string, std::string> >::iterator
108 it = pairs.begin();
109 it != pairs.end();
110 ++it) {
111 if (it->first == kOAuth2RedirectAccessTokenKey)
112 access_token = it->second;
113 else if (it->first == kOAuth2RedirectErrorKey)
114 error = it->second;
115 else if (it->first == kOAuth2ExpiresInKey)
116 expiration = it->second;
117 }
118
119 if (access_token.empty() && error.empty()) {
120 delegate_->OnGaiaFlowFailure(
121 GaiaWebAuthFlow::INVALID_REDIRECT,
122 GoogleServiceAuthError(GoogleServiceAuthError::NONE));
123 } else {
124 delegate_->OnGaiaFlowCompleted(access_token, expiration, error);
125 }
126 }
127 }
128
129 void GaiaWebAuthFlow::OnAuthFlowTitleChange(const std::string& title) {
130 // On the final page the title will be "Loading <redirect-url>".
131 // Treat it as though we'd really been redirected to <redirect-url>.
132 const char kRedirectPrefix[] = "Loading ";
133 std::string prefix(kRedirectPrefix);
134
135 if (StartsWithASCII(title, prefix, true)) {
136 GURL url(title.substr(prefix.length(), std::string::npos));
137 if (url.is_valid())
138 OnAuthFlowURLChange(url);
139 }
140 }
141
142 scoped_ptr<WebAuthFlow> GaiaWebAuthFlow::CreateWebAuthFlow(GURL url) {
143 gfx::Rect initial_bounds;
144 return scoped_ptr<WebAuthFlow>(new WebAuthFlow(this,
145 profile_,
146 url,
147 WebAuthFlow::INTERACTIVE,
148 initial_bounds,
149 host_desktop_type_));
150 }
151
152 } // extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698