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

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 if (IsValidRedirectUrl(url)) {
109 ReportResult(url);
110 return;
111 }
112 }
113
114 WebContents* WebAuthFlow::CreateWebContents() {
115 return WebContents::Create(
116 browser_context_, NULL, MSG_ROUTING_NONE, NULL, NULL);
117 }
118
119 WebAuthFlowWindow* WebAuthFlow::CreateAuthWindow() {
120 return WebAuthFlowWindow::Create(this, browser_context_, contents_);
121 }
122
123 void WebAuthFlow::OnUrlLoaded() {
124 if (!window_) {
125 window_ = CreateAuthWindow();
126 // TODO(munjal): Remove this null check once we have implementations
127 // of WebAuthFlowWindow on all platforms.
128 if (!window_)
129 ReportResult(GURL());
130 else
131 window_->Show();
132 }
133 }
134
135 void WebAuthFlow::OnClose() {
136 ReportResult(GURL());
137 }
138
139 void WebAuthFlow::ReportResult(const GURL& result) {
140 if (!delegate_)
141 return;
142
143 if (result.is_empty()) {
144 delegate_->OnAuthFlowFailure();
145 } else {
146 // TODO(munjal): Consider adding code to parse out access token
147 // from some common places (e.g. URL fragment) so the apps don't
148 // have to do that work.
149 delegate_->OnAuthFlowSuccess(result.spec());
150 }
151
152 // IMPORTANT: Do not access any members after calling the delegate
153 // since the delegate can destroy |this| in the callback and hence
154 // all data members are invalid after that.
155 }
156
157 void WebAuthFlow::InitValidRedirectUrlPrefixes(
158 const std::string& extension_id) {
159 valid_prefixes_.push_back(base::StringPrintf(
160 kChromeExtensionSchemeUrlPattern, extension_id.c_str()));
161 valid_prefixes_.push_back(base::StringPrintf(
162 kChromiumDomainRedirectUrlPattern, extension_id.c_str()));
163 }
164
165 bool WebAuthFlow::IsValidRedirectUrl(const GURL& url) const {
166 std::vector<std::string>::const_iterator iter;
167 for (iter = valid_prefixes_.begin(); iter != valid_prefixes_.end(); ++iter) {
168 if (StartsWithASCII(url.spec(), *iter, false))
169 return true;
170 }
171 return false;
172 }
173
174 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698