Google Cloud Storage C++ Client 2.13.0
A C++ Client Library for Google Cloud Storage
Loading...
Searching...
No Matches
refreshing_credentials_wrapper.h
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/internal/oauth2_refreshing_credentials_wrapper.h"
20#include "google/cloud/status.h"
21#include "google/cloud/status_or.h"
22#include <chrono>
23#include <string>
24#include <utility>
25
26namespace google {
27namespace cloud {
28namespace storage {
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
30namespace oauth2 {
31
32/**
33 * Wrapper for refreshable parts of a Credentials object.
34 *
35 * @deprecated Prefer using the unified credentials documented in @ref guac
36 */
38 public:
40
41 struct TemporaryToken {
42 std::string token;
43 std::chrono::system_clock::time_point expiration_time;
44 };
45
46 template <typename RefreshFunctor>
47 StatusOr<std::string> AuthorizationHeader(
48 std::chrono::system_clock::time_point, RefreshFunctor refresh_fn) const {
49 auto refresh_fn_wrapper =
50 [refresh_fn]() -> StatusOr<google::cloud::internal::AccessToken> {
51 auto temp_token = refresh_fn();
52 if (!temp_token.ok()) return temp_token.status();
53 auto token = SplitToken(temp_token->token);
54 return google::cloud::internal::AccessToken{std::move(token.second),
55 temp_token->expiration_time};
56 };
57 auto header = impl_->AuthorizationHeader(refresh_fn_wrapper);
58 if (!header.ok()) return std::move(header).status();
59 return header->first + ": " + header->second;
60 }
61
62 /**
63 * Returns whether the current access token should be considered expired.
64 *
65 * When determining if a Credentials object needs to be refreshed, the IsValid
66 * method should be used instead; there may be cases where a Credentials is
67 * not expired but should be considered invalid.
68 *
69 * If a Credentials is close to expiration but not quite expired, this method
70 * may still return false. This helps prevent the case where an access token
71 * expires between when it is obtained and when it is used.
72 *
73 * @deprecated Prefer using the unified credentials documented in @ref guac
74 */
75 bool IsExpired(std::chrono::system_clock::time_point now) const;
76
77 /**
78 * Returns whether the current access token should be considered valid.
79 *
80 * This method should be used to determine whether a Credentials object needs
81 * to be refreshed.
82 *
83 * @deprecated Prefer using the unified credentials documented in @ref guac
84 */
85 bool IsValid(std::chrono::system_clock::time_point now) const;
86
87 private:
88 static std::pair<std::string, std::string> SplitToken(
89 std::string const& token);
90
91 std::unique_ptr<oauth2_internal::RefreshingCredentialsWrapper> impl_;
92};
93
94} // namespace oauth2
95GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
96} // namespace storage
97} // namespace cloud
98} // namespace google
99
100#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_REFRESHING_CREDENTIALS_WRAPPER_H
Wrapper for refreshable parts of a Credentials object.
Definition: refreshing_credentials_wrapper.h:37
bool IsValid(std::chrono::system_clock::time_point now) const
Returns whether the current access token should be considered valid.
bool IsExpired(std::chrono::system_clock::time_point now) const
Returns whether the current access token should be considered expired.
StatusOr< std::string > AuthorizationHeader(std::chrono::system_clock::time_point, RefreshFunctor refresh_fn) const
Definition: refreshing_credentials_wrapper.h:47
Authentication components for Google Cloud Storage.
Definition: anonymous_credentials.h:26
Contains all the Google Cloud Storage C++ client APIs.
Definition: auto_finalize.h:24
std::string token
Definition: refreshing_credentials_wrapper.h:42
std::chrono::system_clock::time_point expiration_time
Definition: refreshing_credentials_wrapper.h:43