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

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

Issue 10178020: Start implementing an auth flow for platform apps to be able to do auth (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/extensions/api/identity/web_auth_flow.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/message_loop.h"
11 #include "base/stringprintf.h"
12 #include "base/string_util.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "content/public/browser/invalidate_type.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/web_contents.h"
17 #include "ipc/ipc_message.h"
18
19 using content::BrowserContext;
20 using content::WebContents;
21 using content::WebContentsDelegate;
22
23 namespace {
24
25 static const char kChromeExtensionSchemeUrlPattern[] =
26 "chrome-extension://%s/";
27 static const char kChromiumDomainRedirectUrlPattern[] =
28 "https://%s.chromiumapp.org/";
29
30 extensions::WebAuthFlow::InterceptorForTests* g_interceptor_for_tests = NULL;
31
32 } // namespace
33
34 namespace extensions {
35
36 // static
37 void WebAuthFlow::SetInterceptorForTests(InterceptorForTests* interceptor) {
38 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType));
39 CHECK(NULL == g_interceptor_for_tests); // Only one at a time.
40 g_interceptor_for_tests = interceptor;
41 }
42
43 WebAuthFlow::WebAuthFlow(
44 Delegate* delegate,
45 BrowserContext* browser_context,
46 const std::string& extension_id,
47 const GURL& provider_url)
48 : delegate_(delegate),
49 browser_context_(browser_context),
50 provider_url_(provider_url),
51 contents_(NULL),
52 window_(NULL) {
53 InitValidRedirectUrlPrefixes(extension_id);
54 }
55
56 WebAuthFlow::~WebAuthFlow() {
57 if (window_)
58 delete window_;
59 if (contents_) {
60 contents_->SetDelegate(NULL);
61 contents_->Stop();
62 // Tell message loop to delete contents_ instead of deleting it
63 // directly since destructor can run in response to a callback from
64 // contents_.
65 MessageLoop::current()->DeleteSoon(FROM_HERE, contents_);
66 }
67 }
68
69 void WebAuthFlow::Start() {
70 if (g_interceptor_for_tests != NULL) {
71 // We use PostTask, instead of calling the delegate directly, because the
72 // message loop will run a few times before we notify the delegate in the
73 // real implementation.
74 GURL result = g_interceptor_for_tests->DoIntercept(provider_url_);
75 MessageLoop::current()->PostTask(
76 FROM_HERE,
77 base::Bind(&WebAuthFlow::ReportResult,
78 base::Unretained(this), result));
79 return;
80 }
81
82 contents_ = CreateWebContents();
83 contents_->SetDelegate(this);
84 contents_->GetController().LoadURL(
85 provider_url_,
86 content::Referrer(),
87 content::PAGE_TRANSITION_START_PAGE,
88 std::string());
89 }
90
91 void WebAuthFlow::LoadingStateChanged(WebContents* source) {
92 if (source->IsLoading())
93 return;
94 OnUrlLoaded();
95 }
96
97 void WebAuthFlow::NavigationStateChanged(
98 const WebContents* source, unsigned changed_flags) {
99 // If the URL has not changed, do not perform the check again (for
100 // efficiency).
101 if ((changed_flags & content::INVALIDATE_TYPE_URL) == 0)
102 return;
103
104 // If the URL is a valid extension redirect URL, capture the
105 // results and end the flow.
106 const GURL& url = source->GetURL();
107
108 // TODO(munjal): Remove this after debugging.
109 LOG(INFO) << "Checking redirect URL: " << url.spec();
110
111 if (IsValidRedirectUrl(url)) {
112 ReportResult(url);
113 return;
114 }
115 }
116
117 WebContents* WebAuthFlow::CreateWebContents() {
118 return WebContents::Create(
119 browser_context_, NULL, MSG_ROUTING_NONE, NULL, NULL);
120 }
121
122 WebAuthFlowWindow* WebAuthFlow::CreateAuthWindow() {
123 return WebAuthFlowWindow::Create(this, browser_context_, contents_);
124 }
125
126 void WebAuthFlow::OnUrlLoaded() {
127 if (!window_) {
128 window_ = CreateAuthWindow();
129 // TODO(munjal): Remove this null check once we have implementations
130 // of WebAuthFlowWindow on all platforms.
131 if (!window_)
132 ReportResult(GURL());
133 else
134 window_->Show();
135 }
136 }
137
138 void WebAuthFlow::OnClose() {
139 ReportResult(GURL());
140 }
141
142 void WebAuthFlow::ReportResult(const GURL& result) {
143 if (!delegate_)
144 return;
145
146 if (result.is_empty()) {
147 delegate_->OnAuthFlowFailure();
148 } else {
149 // TODO(munjal): Consider adding code to parse out access token
150 // from some common places (e.g. URL fragment) so the apps don't
151 // have to do that work.
152 delegate_->OnAuthFlowSuccess(result.spec());
153 }
154
155 // IMPORTANT: Do not access any members after calling the delegate
156 // since the delegate can destroy |this| in the callback and hence
157 // all data members are invalid after that.
158 }
159
160 void WebAuthFlow::InitValidRedirectUrlPrefixes(
161 const std::string& extension_id) {
162 valid_prefixes_.push_back(base::StringPrintf(
163 kChromeExtensionSchemeUrlPattern, extension_id.c_str()));
164 valid_prefixes_.push_back(base::StringPrintf(
165 kChromiumDomainRedirectUrlPattern, extension_id.c_str()));
166 }
167
168 bool WebAuthFlow::IsValidRedirectUrl(const GURL& url) const {
169 std::vector<std::string>::const_iterator iter;
170 for (iter = valid_prefixes_.begin(); iter != valid_prefixes_.end(); ++iter) {
171 if (StartsWithASCII(url.spec(), *iter, false))
172 return true;
173 }
174 return false;
175 }
176
177 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698