1
#pragma once
2

            
3
#include "envoy/config/core/v3/grpc_service.pb.h"
4
#include "envoy/grpc/google_grpc_creds.h"
5

            
6
namespace Envoy {
7
namespace Extensions {
8
namespace GrpcCredentials {
9
namespace Example {
10

            
11
/**
12
 * Access token implementation of Google Grpc Credentials Factory
13
 * This implementation uses ssl creds for the grpc channel if available, similar to the default
14
 * implementation. Additionally, it uses MetadataCredentialsFromPlugin to add a static secret to a
15
 * header for call credentials. This implementation does the same thing as AccessTokenCredentials,
16
 * but it's implemented as a Google gRPC client library plugin to show how a custom implementation
17
 * would be created.
18
 *
19
 * This implementation uses the access_token field in the config to get the secret to add to the
20
 * header.
21
 *
22
 * This can be used as an example for how to implement a more complicated custom call credentials
23
 * implementation. Any blocking calls should be performed in the
24
 * MetadataCredentialsFromPlugin::GetMetadata to ensure that the main thread is not blocked while
25
 * initializing the channel.
26
 */
27
class AccessTokenExampleGrpcCredentialsFactory : public Grpc::GoogleGrpcCredentialsFactory {
28
public:
29
  std::shared_ptr<grpc::ChannelCredentials>
30
  getChannelCredentials(const envoy::config::core::v3::GrpcService& grpc_service_config,
31
                        Server::Configuration::CommonFactoryContext& context) override;
32

            
33
1
  std::string name() const override { return "envoy.grpc_credentials.access_token_example"; }
34
};
35

            
36
/*
37
 * Reference:
38
 * https://grpc.io/docs/guides/auth.html#extending-grpc-to-support-other-authentication-mechanisms
39
 */
40
class StaticHeaderAuthenticator : public grpc::MetadataCredentialsPlugin {
41
public:
42
5
  StaticHeaderAuthenticator(const grpc::string& ticket) : ticket_(ticket) {}
43

            
44
  grpc::Status GetMetadata(grpc::string_ref, grpc::string_ref, const grpc::AuthContext&,
45
                           std::multimap<grpc::string, grpc::string>* metadata) override;
46

            
47
private:
48
  grpc::string ticket_;
49
};
50

            
51
} // namespace Example
52
} // namespace GrpcCredentials
53
} // namespace Extensions
54
} // namespace Envoy