Google Cloud Storage C++ Client  1.42.0
A C++ Client Library for Google Cloud Storage
refreshing_credentials_wrapper.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_REFRESHING_CREDENTIALS_WRAPPER_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_REFRESHING_CREDENTIALS_WRAPPER_H
17 
18 #include "google/cloud/storage/version.h"
19 #include "google/cloud/status.h"
20 #include "google/cloud/status_or.h"
21 #include <chrono>
22 #include <string>
23 #include <utility>
24 
25 namespace google {
26 namespace cloud {
27 namespace storage {
29 namespace oauth2 {
30 
31 /**
32  * Wrapper for refreshable parts of a Credentials object.
33  */
35  public:
36  struct TemporaryToken {
37  std::string token;
38  std::chrono::system_clock::time_point expiration_time;
39  };
40 
41  template <typename RefreshFunctor>
42  StatusOr<std::string> AuthorizationHeader(
43  std::chrono::system_clock::time_point now,
44  RefreshFunctor refresh_fn) const {
45  if (IsValid(now)) {
46  return temporary_token_.token;
47  }
48 
49  StatusOr<TemporaryToken> new_token = refresh_fn();
50  if (new_token) {
51  temporary_token_ = *std::move(new_token);
52  return temporary_token_.token;
53  }
54  return new_token.status();
55  }
56 
57  /**
58  * Returns whether the current access token should be considered expired.
59  *
60  * When determining if a Credentials object needs to be refreshed, the IsValid
61  * method should be used instead; there may be cases where a Credentials is
62  * not expired but should be considered invalid.
63  *
64  * If a Credentials is close to expiration but not quite expired, this method
65  * may still return false. This helps prevent the case where an access token
66  * expires between when it is obtained and when it is used.
67  */
68  bool IsExpired(std::chrono::system_clock::time_point now) const;
69 
70  /**
71  * Returns whether the current access token should be considered valid.
72  *
73  * This method should be used to determine whether a Credentials object needs
74  * to be refreshed.
75  */
76  bool IsValid(std::chrono::system_clock::time_point now) const;
77 
78  private:
79  mutable TemporaryToken temporary_token_;
80 };
81 
82 } // namespace oauth2
84 } // namespace storage
85 } // namespace cloud
86 } // namespace google
87 
88 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_REFRESHING_CREDENTIALS_WRAPPER_H