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_SRC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_CREDENTIALS_H
20 #define GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_CREDENTIALS_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stdint.h>
25 
26 #include <initializer_list>
27 #include <string>
28 
29 #include "absl/status/statusor.h"
30 #include "absl/strings/str_format.h"
31 #include "absl/strings/string_view.h"
32 #include "absl/time/time.h"
33 #include "absl/types/optional.h"
34 
35 #include <grpc/grpc_security.h>
36 #include <grpc/support/sync.h>
37 #include <grpc/support/time.h>
38 
39 #include "src/core/lib/gpr/useful.h"
40 #include "src/core/lib/gprpp/ref_counted_ptr.h"
41 #include "src/core/lib/gprpp/unique_type_name.h"
42 #include "src/core/lib/promise/arena_promise.h"
43 #include "src/core/lib/security/credentials/credentials.h"
44 #include "src/core/lib/security/credentials/jwt/json_token.h"
45 #include "src/core/lib/slice/slice.h"
46 #include "src/core/lib/transport/transport.h"
47 
48 class grpc_service_account_jwt_access_credentials
49     : public grpc_call_credentials {
50  public:
51   grpc_service_account_jwt_access_credentials(grpc_auth_json_key key,
52                                               gpr_timespec token_lifetime);
53   ~grpc_service_account_jwt_access_credentials() override;
54 
55   grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>>
56   GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata,
57                      const GetRequestMetadataArgs* args) override;
58 
jwt_lifetime()59   const gpr_timespec& jwt_lifetime() const { return jwt_lifetime_; }
key()60   const grpc_auth_json_key& key() const { return key_; }
61 
debug_string()62   std::string debug_string() override {
63     return absl::StrFormat(
64         "JWTAccessCredentials{ExpirationTime:%s}",
65         absl::FormatTime(absl::FromUnixMicros(
66             static_cast<int64_t>(gpr_timespec_to_micros(jwt_lifetime_)))));
67   };
68 
69   static grpc_core::UniqueTypeName Type();
70 
type()71   grpc_core::UniqueTypeName type() const override { return Type(); }
72 
73  private:
cmp_impl(const grpc_call_credentials * other)74   int cmp_impl(const grpc_call_credentials* other) const override {
75     // TODO(yashykt): Check if we can do something better here
76     return grpc_core::QsortCompare(
77         static_cast<const grpc_call_credentials*>(this), other);
78   }
79 
80   // Have a simple cache for now with just 1 entry. We could have a map based on
81   // the service_url for a more sophisticated one.
82   gpr_mu cache_mu_;
83   struct Cache {
84     grpc_core::Slice jwt_value;
85     std::string service_url;
86     gpr_timespec jwt_expiration;
87   };
88   absl::optional<Cache> cached_;
89 
90   grpc_auth_json_key key_;
91   gpr_timespec jwt_lifetime_;
92 };
93 
94 // Private constructor for jwt credentials from an already parsed json key.
95 // Takes ownership of the key.
96 grpc_core::RefCountedPtr<grpc_call_credentials>
97 grpc_service_account_jwt_access_credentials_create_from_auth_json_key(
98     grpc_auth_json_key key, gpr_timespec token_lifetime);
99 
100 namespace grpc_core {
101 
102 // Exposed for testing purposes only.
103 absl::StatusOr<std::string> RemoveServiceNameFromJwtUri(absl::string_view uri);
104 
105 }  // namespace grpc_core
106 
107 #endif  // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_CREDENTIALS_H
108