OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / Documentation / grpc-auth-support.md
1 # Authentication
2
3 As outlined in the [gRPC authentication guide](https://grpc.io/docs/guides/auth.html) there are a number of different mechanisms for asserting identity between an client and server. We'll present some code-samples here demonstrating how to provide TLS support encryption and identity assertions as well as passing OAuth2 tokens to services that support it.
4
5 # Enabling TLS on a gRPC client
6
7 ```Go
8 conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
9 ```
10
11 # Enabling TLS on a gRPC server
12
13 ```Go
14 creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
15 if err != nil {
16   log.Fatalf("Failed to generate credentials %v", err)
17 }
18 lis, err := net.Listen("tcp", ":0")
19 server := grpc.NewServer(grpc.Creds(creds))
20 ...
21 server.Serve(lis)
22 ```
23
24 # Authenticating with Google
25
26 ## Google Compute Engine (GCE)
27
28 ```Go
29 conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
30 ```
31
32 ## JWT
33
34 ```Go
35 jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope)
36 if err != nil {
37   log.Fatalf("Failed to create JWT credentials: %v", err)
38 }
39 conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(jwtCreds))
40 ```
41