auth_service.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef GOOGLE_APIS_COMMON_AUTH_SERVICE_H_
  5. #define GOOGLE_APIS_COMMON_AUTH_SERVICE_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "google_apis/common/auth_service_interface.h"
  14. #include "google_apis/gaia/core_account_id.h"
  15. namespace network {
  16. class SharedURLLoaderFactory;
  17. }
  18. namespace signin {
  19. class IdentityManager;
  20. }
  21. namespace google_apis {
  22. class AuthServiceObserver;
  23. // This class provides authentication for Google services.
  24. // It integrates specific service integration with the Identity service
  25. // (IdentityManager) and provides OAuth2 token refresh infrastructure.
  26. // All public functions must be called on UI thread.
  27. class AuthService : public AuthServiceInterface {
  28. public:
  29. // |url_loader_factory| is used to perform authentication with
  30. // SimpleURLLoader.
  31. //
  32. // |scopes| specifies OAuth2 scopes.
  33. AuthService(signin::IdentityManager* identity_manager,
  34. const CoreAccountId& account_id,
  35. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  36. const std::vector<std::string>& scopes);
  37. AuthService(const AuthService&) = delete;
  38. AuthService& operator=(const AuthService&) = delete;
  39. ~AuthService() override;
  40. // Overriden from AuthServiceInterface:
  41. void AddObserver(AuthServiceObserver* observer) override;
  42. void RemoveObserver(AuthServiceObserver* observer) override;
  43. void StartAuthentication(AuthStatusCallback callback) override;
  44. bool HasAccessToken() const override;
  45. bool HasRefreshToken() const override;
  46. const std::string& access_token() const override;
  47. void ClearAccessToken() override;
  48. void ClearRefreshToken() override;
  49. private:
  50. class IdentityManagerObserver;
  51. // Called when the state of the refresh token changes.
  52. void OnHandleRefreshToken(const CoreAccountId& account_id,
  53. bool has_refresh_token);
  54. // Called when authentication request from StartAuthentication() is
  55. // completed.
  56. void OnAuthCompleted(AuthStatusCallback callback,
  57. ApiErrorCode error,
  58. const std::string& access_token);
  59. raw_ptr<signin::IdentityManager> identity_manager_;
  60. std::unique_ptr<IdentityManagerObserver> identity_manager_observer_;
  61. CoreAccountId account_id_;
  62. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  63. bool has_refresh_token_;
  64. std::string access_token_;
  65. std::vector<std::string> scopes_;
  66. base::ObserverList<AuthServiceObserver>::Unchecked observers_;
  67. base::ThreadChecker thread_checker_;
  68. // Note: This should remain the last member so it'll be destroyed and
  69. // invalidate its weak pointers before any other members are destroyed.
  70. base::WeakPtrFactory<AuthService> weak_ptr_factory_{this};
  71. };
  72. } // namespace google_apis
  73. #endif // GOOGLE_APIS_COMMON_AUTH_SERVICE_H_