| 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 82 |
| 83 // Initiate/complete the sub-flows. | 83 // Initiate/complete the sub-flows. |
| 84 void StartSigninFlow(); | 84 void StartSigninFlow(); |
| 85 void StartMintTokenFlow(IdentityMintRequestQueue::MintType type); | 85 void StartMintTokenFlow(IdentityMintRequestQueue::MintType type); |
| 86 void CompleteMintTokenFlow(); | 86 void CompleteMintTokenFlow(); |
| 87 | 87 |
| 88 // IdentityMintRequestQueue::Request implementation: | 88 // IdentityMintRequestQueue::Request implementation: |
| 89 virtual void StartMintToken(IdentityMintRequestQueue::MintType type) OVERRIDE; | 89 virtual void StartMintToken(IdentityMintRequestQueue::MintType type) OVERRIDE; |
| 90 | 90 |
| 91 // OAuth2MintTokenFlow::Delegate implementation: | 91 // OAuth2MintTokenFlow::Delegate implementation: |
| 92 virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; | 92 virtual void OnMintTokenSuccess(const std::string& access_token, |
| 93 int time_to_live) OVERRIDE; |
| 93 virtual void OnMintTokenFailure( | 94 virtual void OnMintTokenFailure( |
| 94 const GoogleServiceAuthError& error) OVERRIDE; | 95 const GoogleServiceAuthError& error) OVERRIDE; |
| 95 virtual void OnIssueAdviceSuccess( | 96 virtual void OnIssueAdviceSuccess( |
| 96 const IssueAdviceInfo& issue_advice) OVERRIDE; | 97 const IssueAdviceInfo& issue_advice) OVERRIDE; |
| 97 | 98 |
| 98 // IdentitySigninFlow::Delegate implementation: | 99 // IdentitySigninFlow::Delegate implementation: |
| 99 virtual void SigninSuccess(const std::string& token) OVERRIDE; | 100 virtual void SigninSuccess(const std::string& token) OVERRIDE; |
| 100 virtual void SigninFailed() OVERRIDE; | 101 virtual void SigninFailed() OVERRIDE; |
| 101 | 102 |
| 102 // ExtensionInstallPrompt::Delegate implementation: | 103 // ExtensionInstallPrompt::Delegate implementation: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 122 std::string refresh_token_; | 123 std::string refresh_token_; |
| 123 bool should_prompt_for_signin_; | 124 bool should_prompt_for_signin_; |
| 124 | 125 |
| 125 // When launched in interactive mode, and if there is no existing grant, | 126 // When launched in interactive mode, and if there is no existing grant, |
| 126 // a permissions prompt will be popped up to the user. | 127 // a permissions prompt will be popped up to the user. |
| 127 IssueAdviceInfo issue_advice_; | 128 IssueAdviceInfo issue_advice_; |
| 128 scoped_ptr<ExtensionInstallPrompt> install_ui_; | 129 scoped_ptr<ExtensionInstallPrompt> install_ui_; |
| 129 scoped_ptr<IdentitySigninFlow> signin_flow_; | 130 scoped_ptr<IdentitySigninFlow> signin_flow_; |
| 130 }; | 131 }; |
| 131 | 132 |
| 133 class IdentityInvalidateAuthTokenFunction : public SyncExtensionFunction { |
| 134 public: |
| 135 DECLARE_EXTENSION_FUNCTION("experimental.identity.invalidateAuthToken", |
| 136 EXPERIMENTAL_IDENTITY_INVALIDATEAUTHTOKEN) |
| 137 IdentityInvalidateAuthTokenFunction(); |
| 138 |
| 139 protected: |
| 140 virtual ~IdentityInvalidateAuthTokenFunction(); |
| 141 |
| 142 // SyncExtensionFunction implementation: |
| 143 virtual bool RunImpl() OVERRIDE; |
| 144 }; |
| 145 |
| 132 class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction, | 146 class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction, |
| 133 public WebAuthFlow::Delegate { | 147 public WebAuthFlow::Delegate { |
| 134 public: | 148 public: |
| 135 DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow", | 149 DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow", |
| 136 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW) | 150 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW) |
| 137 | 151 |
| 138 IdentityLaunchWebAuthFlowFunction(); | 152 IdentityLaunchWebAuthFlowFunction(); |
| 139 | 153 |
| 140 // URL checking helpers. Public for testing. | 154 // URL checking helpers. Public for testing. |
| 141 // Checks to see if the current URL ends the flow. | 155 // Checks to see if the current URL ends the flow. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 152 virtual void OnAuthFlowFailure(WebAuthFlow::Failure failure) OVERRIDE; | 166 virtual void OnAuthFlowFailure(WebAuthFlow::Failure failure) OVERRIDE; |
| 153 virtual void OnAuthFlowURLChange(const GURL& redirect_url) OVERRIDE; | 167 virtual void OnAuthFlowURLChange(const GURL& redirect_url) OVERRIDE; |
| 154 | 168 |
| 155 // Helper to initialize final URLs vector. | 169 // Helper to initialize final URLs vector. |
| 156 void InitFinalRedirectURLPrefixes(const std::string& extension_id); | 170 void InitFinalRedirectURLPrefixes(const std::string& extension_id); |
| 157 | 171 |
| 158 scoped_ptr<WebAuthFlow> auth_flow_; | 172 scoped_ptr<WebAuthFlow> auth_flow_; |
| 159 std::vector<GURL> final_prefixes_; | 173 std::vector<GURL> final_prefixes_; |
| 160 }; | 174 }; |
| 161 | 175 |
| 176 class IdentityTokenCacheValue { |
| 177 public: |
| 178 IdentityTokenCacheValue(); |
| 179 explicit IdentityTokenCacheValue(const IssueAdviceInfo& issue_advice); |
| 180 IdentityTokenCacheValue(const std::string& token, |
| 181 base::TimeDelta time_to_live); |
| 182 ~IdentityTokenCacheValue(); |
| 183 |
| 184 // Order of these entries is used to determine whether or not new |
| 185 // entries supercede older ones in SetCachedToken. |
| 186 enum CacheValueStatus { |
| 187 CACHE_STATUS_NOTFOUND, |
| 188 CACHE_STATUS_ADVICE, |
| 189 CACHE_STATUS_TOKEN |
| 190 }; |
| 191 |
| 192 CacheValueStatus status() const; |
| 193 const IssueAdviceInfo& issue_advice() const; |
| 194 const std::string& token() const; |
| 195 |
| 196 private: |
| 197 bool is_expired() const; |
| 198 |
| 199 CacheValueStatus status_; |
| 200 IssueAdviceInfo issue_advice_; |
| 201 std::string token_; |
| 202 base::Time expiration_time_; |
| 203 }; |
| 204 |
| 162 class IdentityAPI : public ProfileKeyedAPI, | 205 class IdentityAPI : public ProfileKeyedAPI, |
| 163 public SigninGlobalError::AuthStatusProvider, | 206 public SigninGlobalError::AuthStatusProvider, |
| 164 public content::NotificationObserver { | 207 public content::NotificationObserver { |
| 165 public: | 208 public: |
| 166 explicit IdentityAPI(Profile* profile); | 209 explicit IdentityAPI(Profile* profile); |
| 167 virtual ~IdentityAPI(); | 210 virtual ~IdentityAPI(); |
| 168 void Initialize(); | 211 void Initialize(); |
| 169 | 212 |
| 170 // Request serialization queue for getAuthToken. | 213 // Request serialization queue for getAuthToken. |
| 171 IdentityMintRequestQueue* mint_queue(); | 214 IdentityMintRequestQueue* mint_queue(); |
| 172 | 215 |
| 216 // Token cache |
| 217 void SetCachedToken(const std::string& extension_id, |
| 218 const std::vector<std::string> scopes, |
| 219 const IdentityTokenCacheValue& token_data); |
| 220 void EraseCachedToken(const std::string& extension_id, |
| 221 const std::string& token); |
| 222 void EraseAllCachedTokens(); |
| 223 const IdentityTokenCacheValue& GetCachedToken( |
| 224 const std::string& extension_id, const std::vector<std::string> scopes); |
| 225 |
| 173 void ReportAuthError(const GoogleServiceAuthError& error); | 226 void ReportAuthError(const GoogleServiceAuthError& error); |
| 174 | 227 |
| 175 // ProfileKeyedAPI implementation. | 228 // ProfileKeyedAPI implementation. |
| 176 virtual void Shutdown() OVERRIDE; | 229 virtual void Shutdown() OVERRIDE; |
| 177 static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); | 230 static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); |
| 178 | 231 |
| 179 // AuthStatusProvider implementation. | 232 // AuthStatusProvider implementation. |
| 180 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; | 233 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; |
| 181 | 234 |
| 182 // content::NotificationObserver implementation. | 235 // content::NotificationObserver implementation. |
| 183 virtual void Observe(int type, | 236 virtual void Observe(int type, |
| 184 const content::NotificationSource& source, | 237 const content::NotificationSource& source, |
| 185 const content::NotificationDetails& details) OVERRIDE; | 238 const content::NotificationDetails& details) OVERRIDE; |
| 186 | 239 |
| 187 private: | 240 private: |
| 188 friend class ProfileKeyedAPIFactory<IdentityAPI>; | 241 friend class ProfileKeyedAPIFactory<IdentityAPI>; |
| 189 | 242 |
| 243 struct TokenCacheKey { |
| 244 TokenCacheKey(const std::string& extension_id, |
| 245 const std::set<std::string> scopes); |
| 246 ~TokenCacheKey(); |
| 247 bool operator<(const TokenCacheKey& rhs) const; |
| 248 std::string extension_id; |
| 249 std::set<std::string> scopes; |
| 250 }; |
| 251 |
| 190 // ProfileKeyedAPI implementation. | 252 // ProfileKeyedAPI implementation. |
| 191 static const char* service_name() { | 253 static const char* service_name() { |
| 192 return "IdentityAPI"; | 254 return "IdentityAPI"; |
| 193 } | 255 } |
| 194 static const bool kServiceIsNULLWhileTesting = true; | 256 static const bool kServiceIsNULLWhileTesting = true; |
| 195 | 257 |
| 196 Profile* profile_; | 258 Profile* profile_; |
| 197 SigninManager* signin_manager_; | 259 SigninManager* signin_manager_; |
| 198 GoogleServiceAuthError error_; | 260 GoogleServiceAuthError error_; |
| 199 // Used to listen to notifications from the TokenService. | 261 // Used to listen to notifications from the TokenService. |
| 200 content::NotificationRegistrar registrar_; | 262 content::NotificationRegistrar registrar_; |
| 201 IdentityMintRequestQueue mint_queue_; | 263 IdentityMintRequestQueue mint_queue_; |
| 264 std::map<TokenCacheKey, IdentityTokenCacheValue> token_cache_; |
| 202 }; | 265 }; |
| 203 | 266 |
| 204 template <> | 267 template <> |
| 205 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); | 268 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); |
| 206 | 269 |
| 207 } // namespace extensions | 270 } // namespace extensions |
| 208 | 271 |
| 209 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 272 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
| OLD | NEW |