3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H
20 #define GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H
24 #include <grpcpp/security/credentials.h>
25 #include <grpcpp/security/server_credentials.h>
26 #include <grpcpp/support/channel_arguments.h>
31 const char kInsecureCredentialsType[] = "INSECURE_CREDENTIALS";
32 // For real credentials, like tls/ssl, this name should match the AuthContext
33 // property "transport_security_type".
34 const char kTlsCredentialsType[] = "ssl";
35 const char kAltsCredentialsType[] = "alts";
36 const char kGoogleDefaultCredentialsType[] = "google_default_credentials";
38 // Provide test credentials of a particular type.
39 class CredentialTypeProvider {
41 virtual ~CredentialTypeProvider() {}
43 virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
44 ChannelArguments* args) = 0;
45 virtual std::shared_ptr<ServerCredentials> GetServerCredentials() = 0;
48 // Provide test credentials. Thread-safe.
49 class CredentialsProvider {
51 virtual ~CredentialsProvider() {}
53 // Add a secure type in addition to the defaults. The default provider has
54 // (kInsecureCredentialsType, kTlsCredentialsType).
55 virtual void AddSecureType(
56 const std::string& type,
57 std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
59 // Provide channel credentials according to the given type. Alter the channel
60 // arguments if needed. Return nullptr if type is not registered.
61 virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
62 const std::string& type, ChannelArguments* args) = 0;
64 // Provide server credentials according to the given type.
65 // Return nullptr if type is not registered.
66 virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
67 const std::string& type) = 0;
69 // Provide a list of secure credentials type.
70 virtual std::vector<std::string> GetSecureCredentialsTypeList() = 0;
73 // Get the current provider. Create a default one if not set.
75 CredentialsProvider* GetCredentialsProvider();
77 // Set the global provider. Takes ownership. The previous set provider will be
80 void SetCredentialsProvider(CredentialsProvider* provider);
82 } // namespace testing
85 #endif // GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H