GRPC C++  1.39.1
jwt_credentials.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_CREDENTIALS_H
20 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_CREDENTIALS_H
21 
23 
24 #include <string>
25 
26 #include <grpc/support/time.h>
27 
28 #include "absl/strings/str_format.h"
29 #include "absl/time/time.h"
32 
34  : public grpc_call_credentials {
35  public:
37  gpr_timespec token_lifetime);
39 
43  grpc_closure* on_request_metadata,
44  grpc_error_handle* error) override;
45 
47  grpc_error_handle error) override;
48 
49  const gpr_timespec& jwt_lifetime() const { return jwt_lifetime_; }
50  const grpc_auth_json_key& key() const { return key_; }
51 
52  std::string debug_string() override {
53  return absl::StrFormat(
54  "JWTAccessCredentials{ExpirationTime:%s}",
55  absl::FormatTime(absl::FromUnixMicros(
56  static_cast<int64_t>(gpr_timespec_to_micros(jwt_lifetime_)))));
57  };
58 
59  private:
60  void reset_cache();
61 
62  // Have a simple cache for now with just 1 entry. We could have a map based on
63  // the service_url for a more sophisticated one.
64  gpr_mu cache_mu_;
65  struct {
67  char* service_url = nullptr;
69  } cached_;
70 
71  grpc_auth_json_key key_;
72  gpr_timespec jwt_lifetime_;
73 };
74 
75 // Private constructor for jwt credentials from an already parsed json key.
76 // Takes ownership of the key.
79  grpc_auth_json_key key, gpr_timespec token_lifetime);
80 
81 #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_CREDENTIALS_H */
Definition: jwt_credentials.h:34
const grpc_auth_json_key & key() const
Definition: jwt_credentials.h:50
bool get_request_metadata(grpc_polling_entity *pollent, grpc_auth_metadata_context context, grpc_credentials_mdelem_array *md_array, grpc_closure *on_request_metadata, grpc_error_handle *error) override
Definition: jwt_credentials.cc:59
const gpr_timespec & jwt_lifetime() const
Definition: jwt_credentials.h:49
grpc_mdelem jwt_md
Definition: jwt_credentials.h:66
gpr_timespec jwt_expiration
Definition: jwt_credentials.h:68
std::string debug_string() override
Definition: jwt_credentials.h:52
grpc_service_account_jwt_access_credentials(grpc_auth_json_key key, gpr_timespec token_lifetime)
Definition: jwt_credentials.cc:117
char * service_url
Definition: jwt_credentials.h:67
void cancel_get_request_metadata(grpc_credentials_mdelem_array *md_array, grpc_error_handle error) override
Definition: jwt_credentials.cc:111
~grpc_service_account_jwt_access_credentials() override
Definition: jwt_credentials.cc:53
GPRAPI double gpr_timespec_to_micros(gpr_timespec t)
Definition: time.cc:235
pthread_mutex_t gpr_mu
Definition: sync_posix.h:45
grpc_core::RefCountedPtr< grpc_call_credentials > grpc_service_account_jwt_access_credentials_create_from_auth_json_key(grpc_auth_json_key key, gpr_timespec token_lifetime)
Definition: jwt_credentials.cc:133
grpc_error_handle error
Definition: lame_client.cc:54
#define GRPC_MDNULL
Definition: metadata.h:403
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: json_token.h:35
Context that can be used by metadata credentials plugin in order to create auth related metadata.
Definition: grpc_security.h:402
Definition: credentials.h:173
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
Definition: credentials.h:153
Definition: error_internal.h:41
Definition: metadata.h:98
Definition: polling_entity.h:37