oauth2_access_token_consumer.h 2.7 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_GAIA_OAUTH2_ACCESS_TOKEN_CONSUMER_H_
  5. #define GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_CONSUMER_H_
  6. #include <string>
  7. #include "base/time/time.h"
  8. class GoogleServiceAuthError;
  9. // An interface that defines the callbacks for consumers to which
  10. // OAuth2AccessTokenFetcher can return results.
  11. class OAuth2AccessTokenConsumer {
  12. public:
  13. // Structure representing information contained in OAuth2 access token.
  14. struct TokenResponse {
  15. TokenResponse();
  16. TokenResponse(const TokenResponse& response);
  17. TokenResponse(TokenResponse&& response);
  18. ~TokenResponse();
  19. TokenResponse& operator=(const TokenResponse& response);
  20. TokenResponse& operator=(TokenResponse&& response);
  21. // OAuth2 access token.
  22. std::string access_token;
  23. // OAuth2 refresh token. May be empty.
  24. std::string refresh_token;
  25. // The date until which the |access_token| can be used.
  26. // This value has a built-in safety margin, so it can be used as-is.
  27. base::Time expiration_time;
  28. // Contains extra information regarding the user's currently registered
  29. // services.
  30. std::string id_token;
  31. // Helper class to make building TokenResponse objects clearer.
  32. class Builder {
  33. public:
  34. Builder();
  35. ~Builder();
  36. Builder& WithAccessToken(const std::string& token);
  37. Builder& WithRefreshToken(const std::string& token);
  38. Builder& WithExpirationTime(const base::Time& time);
  39. Builder& WithIdToken(const std::string& token);
  40. TokenResponse build();
  41. private:
  42. std::string access_token_;
  43. std::string refresh_token_;
  44. base::Time expiration_time_;
  45. std::string id_token_;
  46. };
  47. private:
  48. friend class Builder;
  49. friend class OAuth2AccessTokenConsumer;
  50. TokenResponse(const std::string& access_token,
  51. const std::string& refresh_token,
  52. const base::Time& expiration_time,
  53. const std::string& id_token);
  54. };
  55. OAuth2AccessTokenConsumer() = default;
  56. OAuth2AccessTokenConsumer(const OAuth2AccessTokenConsumer&) = delete;
  57. OAuth2AccessTokenConsumer& operator=(const OAuth2AccessTokenConsumer&) =
  58. delete;
  59. virtual ~OAuth2AccessTokenConsumer();
  60. // Success callback.
  61. virtual void OnGetTokenSuccess(const TokenResponse& token_response);
  62. // Failure callback.
  63. virtual void OnGetTokenFailure(const GoogleServiceAuthError& error);
  64. // Returns the OAuth token consumer name, should be used for logging only.
  65. virtual std::string GetConsumerName() const = 0;
  66. };
  67. #endif // GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_CONSUMER_H_