Skip to content

Commit

Permalink
add hash func
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Feb 6, 2024
1 parent f93eeed commit 6abcda0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
5 changes: 5 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,8 @@ func (c *Client) Shutdown() {
pool.Shutdown()
}
}

// GetID returns the client ID hash.
func (c *Client) GetID() string {
return mulch.HashKeyID(c.SecretKey, c.ID)
}
6 changes: 3 additions & 3 deletions mulch/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import "log"
// Leaving the interface nil disables all log output.
type Logger interface {
// Debugf is used sparingly.
Debugf(string, ...interface{})
Debugf(format string, v ...interface{})
// Errorf is the most used of the loggers.
Errorf(string, ...interface{})
Errorf(format string, v ...interface{})
// Printf is only used when a custom Handler is not provided.
Printf(string, ...interface{})
Printf(format string, v ...interface{})
}

// DefaultLogger is a simple wrapper around the provided Logger interface.
Expand Down
18 changes: 17 additions & 1 deletion mulch/mulch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// used by mulery client library, server library and server application.
package mulch

import "time"
import (
"crypto/sha256"
"encoding/hex"
"time"
)

const SecretKeyHeader = "x-secret-key"

Expand All @@ -17,3 +21,15 @@ type Handshake struct {
}

const HandshakeTimeout = 15 * time.Second

// HashKeyID creates a unique client ID hash.
func HashKeyID(secret string, clientID string) string {
if secret == "" {
return clientID
}

hash := sha256.New()
hash.Write([]byte(secret + clientID))

return hex.EncodeToString(hash.Sum(nil))
}
17 changes: 4 additions & 13 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package server

import (
"crypto/sha256"
"fmt"
"time"

"golift.io/mulery/mulch"
)

var (
Expand Down Expand Up @@ -210,18 +211,8 @@ func (s *Server) dispatchRequest(request *dispatchRequest, threadID uint) {
// Register the connection into server pools.
// This is called through a channel from the register handler.
func (s *Server) registerPool(client *PoolConfig) {
cID := client.ID

if client.secret != "" {
hash := sha256.New()
hash.Write([]byte(client.secret + client.ID))
// As promised, if a custom key validator returns a secret(string),
// hash that with the client id to create a new client id.
// This is custom logic you probably don't want, so don't return a string from your key validator.
cID = fmt.Sprintf("%x", hash.Sum(nil))
}

if pool, ok := s.pools[clientID(cID)]; !ok || pool == nil {
cID := mulch.HashKeyID(client.secret, client.ID)
if pool := s.pools[clientID(cID)]; pool == nil {
s.pools[clientID(cID)] = NewPool(s, client, cID+" ["+client.Name+"]")
}

Expand Down

0 comments on commit 6abcda0

Please sign in to comment.