Categorygithub.com/yogeshlonkar/go-grpc-hmac
modulepackage
0.3.6
Repository: https://github.com/yogeshlonkar/go-grpc-hmac.git
Documentation: pkg.go.dev

# README

go-grcp-hmac Go Reference Continuous Integration Go Report Card

HMAC Client and Server Interceptor for golang grpc

💻 Install

go get github.com/travix/protoc-gen-gotf

✏️ Example

🧑‍💻 Usage

Server

Add required interceptors to grpc server options

// getSecrets implements hmac.GetSecret func type that returns secret key for given keyId
interceptor := hmac.NewServerInterceptor(getSecrets)
opts := []grpc.ServerOption{
    interceptor.UnaryInterceptor(),
    interceptor.StreamInterceptor(),
    // ... other options
}
server := grpc.NewServer(opts...)

Client

Add required interceptors to grpc client options

// keyId for which secret_key is returned by hmac.GetSecret func type on server side
interceptor := hmac.NewClientInterceptor(keyId, secret_key)
opts := []grpc.DialOption{
    interceptor.WithUnaryInterceptor(),
    interceptor.WithStreamInterceptor(),
	// ... other options
}
conn, err := grpc.Dial(addr, opts...)

🔐 HMAC Authentication

HMAC is generated using

  • Request payload encoded using gob encoder, full method name concatenated with ; as separator
  • If request payload is empty, then only full method name is used.
  • Generated message is encrypted with given secret using SHA512_256

Authentication flow

  • Client interceptor adds x-hmac-key-id and x-hmac-signature to outgoing request context.
  • Server interceptor reads x-hmac-key-id and x-hmac-signature from incoming request context and verifies the signature using secret independently fetched on server using given key id.
  • If signature is valid, request is processed, otherwise Unauthenticated error is returned.

# Functions

Bytes generate a HMAC signature and return it as a base64 encoded []byte.
DisableLogging for this module.
EnableLogging for this module.
NewClientInterceptor returns a new client interceptor that adds HMAC authentication to outgoing requests.
NewMessage returns a string representation of the request and method.
NewServerInterceptor returns a new server interceptor that authenticates requests using GetSecret.
String generates a HMAC signature and returns it as a base64 encoded string.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ErrUnauthorized is returned when the request is not authorized for any reason.

# Interfaces

ClientInterceptor is a grpc client interceptor that adds HMAC authentication to outgoing requests.
ServerInterceptor that implements HMAC authentication for gRPC servers.

# Type aliases

GetSecret is a function that returns the secret for a given keyId.