mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-03 16:59:22 +02:00
pkg: add grpcutil package (#1032)
This commit is contained in:
parent
fae02791f5
commit
09621ee263
2 changed files with 60 additions and 0 deletions
31
pkg/grpcutil/grpcutil.go
Normal file
31
pkg/grpcutil/grpcutil.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Package grpcutil contains functions for interacting with gRPC.
|
||||
package grpcutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// SessionIDMetadataKey is the key in the metadata.
|
||||
const SessionIDMetadataKey = "sessionid"
|
||||
|
||||
// WithOutgoingSessionID appends a metadata header for the session ID to a context.
|
||||
func WithOutgoingSessionID(ctx context.Context, sessionID string) context.Context {
|
||||
return metadata.AppendToOutgoingContext(ctx, SessionIDMetadataKey, sessionID)
|
||||
}
|
||||
|
||||
// SessionIDFromGRPCRequest returns the session id from the gRPC request.
|
||||
func SessionIDFromGRPCRequest(ctx context.Context) (sessionID string, ok bool) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
sessionIDs := md.Get(SessionIDMetadataKey)
|
||||
if len(sessionIDs) == 0 {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return sessionIDs[0], true
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue