OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/identity/identity_api.h" | 5 #include "chrome/browser/extensions/api/identity/identity_api.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/extension_install_prompt.h" | 8 #include "chrome/browser/extensions/extension_install_prompt.h" |
9 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 9 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/permissions_updater.h" | 11 #include "chrome/browser/extensions/permissions_updater.h" |
12 #include "chrome/browser/signin/token_service.h" | 12 #include "chrome/browser/signin/token_service.h" |
13 #include "chrome/browser/signin/token_service_factory.h" | 13 #include "chrome/browser/signin/token_service_factory.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 15 #include "chrome/browser/ui/browser_navigator.h" |
16 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | |
17 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
16 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
19 #include "chrome/common/url_constants.h" | |
20 #include "content/public/common/page_transition_types.h" | |
17 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
22 #include "webkit/glue/window_open_disposition.h" | |
18 | 23 |
19 namespace extensions { | 24 namespace extensions { |
20 | 25 |
21 namespace { | 26 namespace { |
22 | 27 |
23 const char kInvalidClientId[] = "Invalid OAuth2 Client ID."; | 28 const char kInvalidClientId[] = "Invalid OAuth2 Client ID."; |
24 const char kInvalidScopes[] = "Invalid OAuth2 scopes."; | 29 const char kInvalidScopes[] = "Invalid OAuth2 scopes."; |
25 const char kInvalidRedirect[] = "Did not redirect to the right URL."; | 30 const char kInvalidRedirect[] = "Did not redirect to the right URL."; |
26 const char kAuthFailure[] = "OAuth2 request failed: "; | 31 const char kAuthFailure[] = "OAuth2 request failed: "; |
27 const char kNoGrant[] = "OAuth2 not granted or revoked."; | 32 const char kNoGrant[] = "OAuth2 not granted or revoked."; |
28 const char kUserRejected[] = "The user did not approve access."; | 33 const char kUserRejected[] = "The user did not approve access."; |
34 const char kUserNotSignedIn[] = "The user is not signed in."; | |
29 | 35 |
30 } // namespace | 36 } // namespace |
31 | 37 |
32 GetAuthTokenFunction::GetAuthTokenFunction() : interactive_(false) {} | 38 GetAuthTokenFunction::GetAuthTokenFunction() : interactive_(false) {} |
33 GetAuthTokenFunction::~GetAuthTokenFunction() {} | 39 GetAuthTokenFunction::~GetAuthTokenFunction() {} |
34 | 40 |
35 bool GetAuthTokenFunction::RunImpl() { | 41 bool GetAuthTokenFunction::RunImpl() { |
36 DictionaryValue* arg = NULL; | 42 DictionaryValue* arg = NULL; |
37 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); | 43 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); |
38 arg->GetBoolean("interactive", &interactive_); | 44 arg->GetBoolean("interactive", &interactive_); |
39 | 45 |
46 const Extension* extension = GetExtension(); | |
47 Extension::OAuth2Info oauth2_info = extension->oauth2_info(); | |
48 | |
49 // Check that the necessary information is present in the manfist. | |
50 if (oauth2_info.client_id.empty()) { | |
51 error_ = kInvalidClientId; | |
52 return false; | |
53 } | |
54 | |
55 if (oauth2_info.scopes.size() == 0) { | |
56 error_ = kInvalidScopes; | |
57 return false; | |
58 } | |
59 | |
40 // Balanced in OnIssueAdviceSuccess|OnMintTokenSuccess|OnMintTokenFailure| | 60 // Balanced in OnIssueAdviceSuccess|OnMintTokenSuccess|OnMintTokenFailure| |
41 // InstallUIAbort. | 61 // InstallUIAbort|OnLoginUIClosed. |
42 AddRef(); | 62 AddRef(); |
43 | 63 |
44 if (StartFlow(ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? | 64 if (!HasLoginToken()) { |
45 OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : | 65 if (StartLogin()) { |
46 OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE)) { | 66 return true; |
67 } else { | |
68 Release(); | |
69 return false; | |
70 } | |
71 } | |
72 | |
73 if (StartFlow(GetTokenFlowMode())) { | |
47 return true; | 74 return true; |
48 } else { | 75 } else { |
49 Release(); | 76 Release(); |
50 return false; | 77 return false; |
51 } | 78 } |
52 } | 79 } |
53 | 80 |
54 void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) { | 81 void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) { |
55 result_.reset(Value::CreateStringValue(access_token)); | 82 result_.reset(Value::CreateStringValue(access_token)); |
56 SendResponse(true); | 83 SendResponse(true); |
(...skipping 15 matching lines...) Expand all Loading... | |
72 install_ui_.reset( | 99 install_ui_.reset( |
73 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); | 100 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); |
74 install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); | 101 install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); |
75 } else { | 102 } else { |
76 error_ = kNoGrant; | 103 error_ = kNoGrant; |
77 SendResponse(false); | 104 SendResponse(false); |
78 Release(); // Balanced in RunImpl. | 105 Release(); // Balanced in RunImpl. |
79 } | 106 } |
80 } | 107 } |
81 | 108 |
109 void GetAuthTokenFunction::OnLoginUIShown(LoginUIService::LoginUI* ui) { | |
110 // Do nothing when login ui is shown. | |
111 } | |
112 | |
113 void GetAuthTokenFunction::OnLoginUIClosed(LoginUIService::LoginUI* ui) { | |
114 LoginUIService* login_ui_service = | |
115 LoginUIServiceFactory::GetForProfile(profile()); | |
116 login_ui_service->RemoveObserver(this); | |
117 if (!StartFlow(GetTokenFlowMode())) { | |
118 SendResponse(false); | |
119 Release(); | |
120 } | |
121 } | |
122 | |
82 void GetAuthTokenFunction::InstallUIProceed() { | 123 void GetAuthTokenFunction::InstallUIProceed() { |
83 DCHECK(install_ui_->record_oauth2_grant()); | 124 DCHECK(install_ui_->record_oauth2_grant()); |
84 // The user has accepted the scopes, so we may now force (recording a grant | 125 // The user has accepted the scopes, so we may now force (recording a grant |
85 // and receiving a token). | 126 // and receiving a token). |
86 bool success = StartFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE); | 127 bool success = StartFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE); |
87 DCHECK(success); | 128 DCHECK(success); |
88 } | 129 } |
89 | 130 |
90 void GetAuthTokenFunction::InstallUIAbort(bool user_initiated) { | 131 void GetAuthTokenFunction::InstallUIAbort(bool user_initiated) { |
91 error_ = kUserRejected; | 132 error_ = kUserRejected; |
92 SendResponse(false); | 133 SendResponse(false); |
93 Release(); // Balanced in RunImpl. | 134 Release(); // Balanced in RunImpl. |
94 } | 135 } |
95 | 136 |
96 bool GetAuthTokenFunction::StartFlow(OAuth2MintTokenFlow::Mode mode) { | 137 bool GetAuthTokenFunction::StartFlow(OAuth2MintTokenFlow::Mode mode) { |
97 const Extension* extension = GetExtension(); | 138 const Extension* extension = GetExtension(); |
98 Extension::OAuth2Info oauth2_info = extension->oauth2_info(); | 139 Extension::OAuth2Info oauth2_info = extension->oauth2_info(); |
99 | 140 |
100 if (oauth2_info.client_id.empty()) { | 141 if (!HasLoginToken()) { |
101 error_ = kInvalidClientId; | 142 error_ = kUserNotSignedIn; |
102 return false; | |
103 } | |
104 | |
105 if (oauth2_info.scopes.size() == 0) { | |
106 error_ = kInvalidScopes; | |
107 return false; | 143 return false; |
108 } | 144 } |
109 | 145 |
110 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | 146 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
111 flow_.reset(new OAuth2MintTokenFlow( | 147 flow_.reset(new OAuth2MintTokenFlow( |
112 profile()->GetRequestContext(), | 148 profile()->GetRequestContext(), |
113 this, | 149 this, |
114 OAuth2MintTokenFlow::Parameters( | 150 OAuth2MintTokenFlow::Parameters( |
115 token_service->GetOAuth2LoginRefreshToken(), | 151 token_service->GetOAuth2LoginRefreshToken(), |
116 extension->id(), | 152 extension->id(), |
117 oauth2_info.client_id, | 153 oauth2_info.client_id, |
118 oauth2_info.scopes, | 154 oauth2_info.scopes, |
119 mode))); | 155 mode))); |
120 flow_->Start(); | 156 flow_->Start(); |
121 return true; | 157 return true; |
122 } | 158 } |
123 | 159 |
160 bool GetAuthTokenFunction::StartLogin() { | |
161 if (!interactive_) { | |
162 error_ = kUserNotSignedIn; | |
163 return false; | |
164 } | |
165 | |
166 LoginUIService* login_ui_service = | |
167 LoginUIServiceFactory::GetForProfile(profile()); | |
168 login_ui_service->AddObserver(this); | |
169 | |
170 LoginUIService::LoginUI* login_ui = login_ui_service->current_login_ui(); | |
171 if (login_ui) { | |
172 login_ui->FocusUI(); | |
173 } else { | |
174 chrome::NavigateParams params(NULL, | |
175 GURL(chrome::kChromeUISyncPromoURL), | |
176 content::PAGE_TRANSITION_START_PAGE); | |
177 params.profile = profile(); | |
178 params.disposition = NEW_POPUP; | |
179 chrome::Navigate(¶ms); | |
180 } | |
181 | |
182 return true; | |
183 } | |
184 | |
185 bool GetAuthTokenFunction::HasLoginToken() const { | |
186 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | |
187 return token_service->HasOAuthLoginToken(); | |
188 } | |
189 | |
190 OAuth2MintTokenFlow::Mode GetAuthTokenFunction::GetTokenFlowMode() const { | |
191 return ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? | |
192 OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : | |
Evan Stade
2012/07/13 08:57:33
this indent should be 4 spaces relative to 'return
Munjal (Google)
2012/07/16 20:13:30
Done.
| |
193 OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE; | |
194 } | |
195 | |
124 LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {} | 196 LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {} |
125 LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {} | 197 LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {} |
126 | 198 |
127 bool LaunchWebAuthFlowFunction::RunImpl() { | 199 bool LaunchWebAuthFlowFunction::RunImpl() { |
128 DictionaryValue* arg = NULL; | 200 DictionaryValue* arg = NULL; |
129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); | 201 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); |
130 | 202 |
131 std::string url; | 203 std::string url; |
132 EXTENSION_FUNCTION_VALIDATE(arg->GetString("url", &url)); | 204 EXTENSION_FUNCTION_VALIDATE(arg->GetString("url", &url)); |
133 | 205 |
(...skipping 18 matching lines...) Expand all Loading... | |
152 Release(); // Balanced in RunImpl. | 224 Release(); // Balanced in RunImpl. |
153 } | 225 } |
154 | 226 |
155 void LaunchWebAuthFlowFunction::OnAuthFlowFailure() { | 227 void LaunchWebAuthFlowFunction::OnAuthFlowFailure() { |
156 error_ = kInvalidRedirect; | 228 error_ = kInvalidRedirect; |
157 SendResponse(false); | 229 SendResponse(false); |
158 Release(); // Balanced in RunImpl. | 230 Release(); // Balanced in RunImpl. |
159 } | 231 } |
160 | 232 |
161 } // namespace extensions | 233 } // namespace extensions |
OLD | NEW |