Add DialOption with metadata on client connection
authorJulien Andrieux <julien@pantomath.io>
Fri, 6 Oct 2017 08:23:36 +0000 (10:23 +0200)
committerJulien Andrieux <julien@pantomath.io>
Fri, 6 Oct 2017 12:32:57 +0000 (14:32 +0200)
client/main.go

index 5df1cdd..d571fef 100644 (file)
@@ -9,6 +9,25 @@ import (
        "google.golang.org/grpc/credentials"
 )
 
+// Authentication holds the login/password
+type Authentication struct {
+       Login    string
+       Password string
+}
+
+// GetRequestMetadata gets the current request metadata
+func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
+       return map[string]string{
+               "login":    a.Login,
+               "password": a.Password,
+       }, nil
+}
+
+// RequireTransportSecurity indicates whether the credentials requires transport security
+func (a *Authentication) RequireTransportSecurity() bool {
+       return true
+}
+
 func main() {
        var conn *grpc.ClientConn
 
@@ -18,8 +37,14 @@ func main() {
                log.Fatalf("could not load tls cert: %s", err)
        }
 
+       // Setup the login/pass
+       auth := Authentication{
+               Login:    "john",
+               Password: "doe",
+       }
+
        // Initiate a connection with the server
-       conn, err = grpc.Dial("localhost:7777", grpc.WithTransportCredentials(creds))
+       conn, err = grpc.Dial("localhost:7777", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
        if err != nil {
                log.Fatalf("did not connect: %s", err)
        }