Skip to content

Commit

Permalink
refactor: Update metadata keys in fetchSession functions and refreshG…
Browse files Browse the repository at this point in the history
…rpcHeaders

Update the metadata keys to use constants instead of literal strings in the 
fetchSessionPeerID, fetchSessionUserAddress, and refreshGrpcHeaders functions.
  • Loading branch information
prnk28 committed Jun 4, 2024
1 parent 1559ff4 commit eaa9c40
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/ipfs/kubo v0.27.0
github.com/labstack/echo/v4 v4.12.0
github.com/mr-tron/base58 v1.2.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/segmentio/ksuid v1.0.4
github.com/spf13/cast v1.6.0
Expand Down Expand Up @@ -263,7 +264,6 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/orcaman/concurrent-map v1.0.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
Expand Down
30 changes: 15 additions & 15 deletions internal/local/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
)

// Default Key in gRPC Metadata for the Session ID
const kMetadataSessionIDKey = "sonr-session-id"
const MetadataSessionIDKey = "sonr-session-id"

// Default Key in gRPC Metadata for the Session Authentication JWT Token
const kMetadataAuthTokenKey = "sonr-auth-token"
const MetadataAuthTokenKey = "sonr-auth-token"

// Default Key in gRPC Metadata for the Session User Address
const kMetadataUserAddressKey = "sonr-user-address"
const MetadataUserAddressKey = "sonr-user-address"

// Default Key in gRPC Metadata for the Service Origin
const kMetadataServiceOriginKey = "sonr-service-origin"
const MetadataServiceOriginKey = "sonr-service-origin"

// Default Key in gRPC Metadata for the IPFS Peer ID
const kMetadataIPFSPeerIDKey = "sonr-ipfs-peer-id"
const MetadataIPFSPeerIDKey = "sonr-ipfs-peer-id"

// SonrContext is the context for the Sonr API
type SonrContext struct {
Expand Down Expand Up @@ -71,7 +71,7 @@ func findOrSetSessionID(ctx context.Context) string {
if !ok {
return ksuid.New().String()
}
vals := md.Get(kMetadataSessionIDKey)
vals := md.Get(MetadataSessionIDKey)
if len(vals) == 0 {
return ksuid.New().String()
}
Expand All @@ -84,7 +84,7 @@ func fetchSessionAuthToken(ctx context.Context) (string, error) {
if !ok {
return "", errors.New("no metadata found")
}
vals := md.Get(kMetadataAuthTokenKey)
vals := md.Get(MetadataAuthTokenKey)
if len(vals) == 0 {
return "", errors.New("no values found")
}
Expand All @@ -97,7 +97,7 @@ func fetchSessionServiceOrigin(ctx context.Context) (string, error) {
if !ok {
return "", errors.New("no metadata found")
}
vals := md.Get(kMetadataServiceOriginKey)
vals := md.Get(MetadataServiceOriginKey)
if len(vals) == 0 {
return "", errors.New("no values found")
}
Expand All @@ -110,7 +110,7 @@ func fetchSessionPeerID(ctx context.Context) (string, error) {
if !ok {
return "", errors.New("no metadata found")
}
vals := md.Get(kMetadataIPFSPeerIDKey)
vals := md.Get(MetadataIPFSPeerIDKey)
if len(vals) == 0 {
return "", errors.New("no values found")
}
Expand All @@ -123,7 +123,7 @@ func fetchSessionUserAddress(ctx context.Context) (string, error) {
if !ok {
return "", errors.New("no metadata found")
}
vals := md.Get(kMetadataAuthTokenKey)
vals := md.Get(MetadataAuthTokenKey)
if len(vals) == 0 {
return "", errors.New("no values found")
}
Expand All @@ -137,17 +137,17 @@ func refreshGrpcHeaders(ctx SonrContext) {
header := metadata.Pairs(key, value)
return grpc.SendHeader(ctx.Context, header)
}
sendHeader(kMetadataSessionIDKey, ctx.SessionID)
sendHeader(MetadataSessionIDKey, ctx.SessionID)
if ctx.Token != "" {
sendHeader(kMetadataAuthTokenKey, ctx.Token)
sendHeader(MetadataAuthTokenKey, ctx.Token)
}
if ctx.UserAddress != "" {
sendHeader(kMetadataUserAddressKey, ctx.UserAddress)
sendHeader(MetadataUserAddressKey, ctx.UserAddress)
}
if ctx.ServiceOrigin != "" {
sendHeader(kMetadataServiceOriginKey, ctx.ServiceOrigin)
sendHeader(MetadataServiceOriginKey, ctx.ServiceOrigin)
}
if ctx.PeerID != "" {
sendHeader(kMetadataIPFSPeerIDKey, ctx.PeerID)
sendHeader(MetadataIPFSPeerIDKey, ctx.PeerID)
}
}
70 changes: 70 additions & 0 deletions internal/local/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package local_test

import (
"context"
"testing"

"github.com/di-dao/sonr/internal/local"
"github.com/segmentio/ksuid"
"google.golang.org/grpc/metadata"
)

const (
valAddr = "validator-address"
chainID = "chain-id"
)

func setupMetadata() metadata.MD {
return metadata.Pairs(
local.MetadataSessionIDKey, ksuid.New().String(),
local.MetadataAuthTokenKey, "token",
local.MetadataUserAddressKey, "user-address",
local.MetadataServiceOriginKey, "service-origin",
local.MetadataIPFSPeerIDKey, "peer-id",
)
}

func TestUnwrapContext_NoMetadata(t *testing.T) {
ctx := context.Background()

// Unwrap context with no metadata
sctx := local.UnwrapContext(ctx)
if sctx.SessionID == "" {
t.Errorf("Expected SessionID to be set, got empty string")
}
if sctx.Token != "" {
t.Errorf("Expected Token to be empty, got %s", sctx.Token)
}
if sctx.UserAddress != "" {
t.Errorf("Expected UserAddress to be empty, got %s", sctx.UserAddress)
}
if sctx.ServiceOrigin != "" {
t.Errorf("Expected ServiceOrigin to be empty, got %s", sctx.ServiceOrigin)
}
if sctx.PeerID != "" {
t.Errorf("Expected PeerID to be empty, got %s", sctx.PeerID)
}
}

func TestUnwrapContext_WithMetadata(t *testing.T) {
md := setupMetadata()
ctx := metadata.NewIncomingContext(context.Background(), md)

// Unwrap context with metadata
sctx := local.UnwrapContext(ctx)
if sctx.SessionID == "" {
t.Errorf("Expected SessionID to be set, got empty string")
}
if sctx.Token == "" {
t.Errorf("Expected Token to be set, got empty string")
}
if sctx.UserAddress == "" {
t.Errorf("Expected UserAddress to be set, got empty string")
}
if sctx.ServiceOrigin == "" {
t.Errorf("Expected ServiceOrigin to be set, got empty string")
}
if sctx.PeerID == "" {
t.Errorf("Expected PeerID to be set, got empty string")
}
}

0 comments on commit eaa9c40

Please sign in to comment.