|
| 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_INTERNAL_OAUTH2_CREDENTIALS_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_OAUTH2_CREDENTIALS_H |
| 17 | + |
| 18 | +#include "google/cloud/status.h" |
| 19 | +#include "google/cloud/status_or.h" |
| 20 | +#include "google/cloud/version.h" |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace google { |
| 25 | +namespace cloud { |
| 26 | +namespace oauth2_internal { |
| 27 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 28 | +/** |
| 29 | + * Interface for OAuth 2.0 credentials for use with Google's Unified Auth Client |
| 30 | + * (GUAC) library. Internally, GUAC credentials are mapped to the appropriate |
| 31 | + * OAuth 2.0 credential for use with GCP services with a REST API. |
| 32 | + * |
| 33 | + * Instantiating a specific kind of `Credentials` should usually be done via the |
| 34 | + * GUAC convenience methods declared in google/cloud/credentials.h. |
| 35 | + * |
| 36 | + * @see https://cloud.google.com/docs/authentication/ for an overview of |
| 37 | + * authenticating to Google Cloud Platform APIs. |
| 38 | + */ |
| 39 | +class Credentials { |
| 40 | + public: |
| 41 | + virtual ~Credentials() = default; |
| 42 | + |
| 43 | + /** |
| 44 | + * Attempts to obtain a value for the Authorization HTTP header. |
| 45 | + * |
| 46 | + * If unable to obtain a value for the Authorization header, which could |
| 47 | + * happen for `Credentials` that need to be periodically refreshed, the |
| 48 | + * underlying `Status` will indicate failure details from the refresh HTTP |
| 49 | + * request. Otherwise, the returned value will contain the Authorization |
| 50 | + * header to be used in HTTP requests. |
| 51 | + */ |
| 52 | + virtual StatusOr<std::pair<std::string, std::string>> |
| 53 | + AuthorizationHeader() = 0; |
| 54 | + |
| 55 | + /** |
| 56 | + * Try to sign @p string_to_sign using @p service_account. |
| 57 | + * |
| 58 | + * Some %Credentials types can locally sign a blob, most often just on behalf |
| 59 | + * of an specific service account. This function returns an error if the |
| 60 | + * credentials cannot sign the blob at all, or if the service account is a |
| 61 | + * mismatch. |
| 62 | + */ |
| 63 | + virtual StatusOr<std::vector<std::uint8_t>> SignBlob( |
| 64 | + std::string const& signing_service_account, |
| 65 | + std::string const& string_to_sign) const; |
| 66 | + |
| 67 | + /// Return the account's email associated with these credentials, if any. |
| 68 | + virtual std::string AccountEmail() const { return std::string{}; } |
| 69 | + |
| 70 | + /// Return the account's key_id associated with these credentials, if any. |
| 71 | + virtual std::string KeyId() const { return std::string{}; } |
| 72 | +}; |
| 73 | + |
| 74 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 75 | +} // namespace oauth2_internal |
| 76 | +} // namespace cloud |
| 77 | +} // namespace google |
| 78 | + |
| 79 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_OAUTH2_CREDENTIALS_H |
0 commit comments