oauth_token_getter_impl.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2014 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. #ifndef REMOTING_BASE_OAUTH_TOKEN_GETTER_IMPL_H_
  5. #define REMOTING_BASE_OAUTH_TOKEN_GETTER_IMPL_H_
  6. #include "base/callback.h"
  7. #include "base/containers/queue.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "base/time/time.h"
  11. #include "base/timer/timer.h"
  12. #include "google_apis/gaia/gaia_oauth_client.h"
  13. #include "remoting/base/oauth_token_getter.h"
  14. namespace network {
  15. class SharedURLLoaderFactory;
  16. } // namespace network
  17. namespace remoting {
  18. // OAuthTokenGetter accepts an authorization code in the intermediate
  19. // credentials or a refresh token in the authorization credentials. It will
  20. // convert authorization code into a refresh token and access token.
  21. // OAuthTokenGetter will exchange refresh tokens for access tokens and will
  22. // cache access tokens, refreshing them as needed.
  23. // On first usage it is likely an application will only have an auth code,
  24. // from this you can get a refresh token which can be reused next app launch.
  25. class OAuthTokenGetterImpl : public OAuthTokenGetter,
  26. public gaia::GaiaOAuthClient::Delegate {
  27. public:
  28. OAuthTokenGetterImpl(
  29. std::unique_ptr<OAuthIntermediateCredentials> intermediate_credentials,
  30. const OAuthTokenGetter::CredentialsUpdatedCallback& on_credentials_update,
  31. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  32. bool auto_refresh);
  33. OAuthTokenGetterImpl(
  34. std::unique_ptr<OAuthAuthorizationCredentials> authorization_credentials,
  35. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  36. bool auto_refresh);
  37. ~OAuthTokenGetterImpl() override;
  38. // OAuthTokenGetter interface.
  39. void CallWithToken(OAuthTokenGetter::TokenCallback on_access_token) override;
  40. void InvalidateCache() override;
  41. base::WeakPtr<OAuthTokenGetterImpl> GetWeakPtr();
  42. private:
  43. // gaia::GaiaOAuthClient::Delegate interface.
  44. void OnGetTokensResponse(const std::string& user_email,
  45. const std::string& access_token,
  46. int expires_seconds) override;
  47. void OnRefreshTokenResponse(const std::string& access_token,
  48. int expires_in_seconds) override;
  49. void OnGetUserEmailResponse(const std::string& user_email) override;
  50. void OnOAuthError() override;
  51. void OnNetworkError(int response_code) override;
  52. void UpdateAccessToken(const std::string& access_token, int expires_seconds);
  53. void NotifyTokenCallbacks(Status status,
  54. const std::string& user_email,
  55. const std::string& access_token);
  56. void NotifyUpdatedCallbacks(const std::string& user_email,
  57. const std::string& refresh_token);
  58. void GetOauthTokensFromAuthCode();
  59. void RefreshAccessToken();
  60. bool IsResponsePending() const;
  61. void SetResponsePending(bool is_pending);
  62. void OnResponseTimeout();
  63. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  64. std::unique_ptr<OAuthIntermediateCredentials> intermediate_credentials_;
  65. std::unique_ptr<OAuthAuthorizationCredentials> authorization_credentials_;
  66. std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
  67. OAuthTokenGetter::CredentialsUpdatedCallback credentials_updated_callback_;
  68. bool email_verified_ = false;
  69. bool email_discovery_ = false;
  70. std::string oauth_access_token_;
  71. base::Time access_token_expiry_time_;
  72. base::queue<OAuthTokenGetter::TokenCallback> pending_callbacks_;
  73. std::unique_ptr<base::OneShotTimer> refresh_timer_;
  74. base::OneShotTimer response_timeout_timer_;
  75. SEQUENCE_CHECKER(sequence_checker_);
  76. base::WeakPtrFactory<OAuthTokenGetterImpl> weak_factory_{this};
  77. };
  78. } // namespace remoting
  79. #endif // REMOTING_BASE_OAUTH_TOKEN_GETTER_IMPL_H_