From 99d4f18daf2637ec885cfb72e4193f7e8fedf970 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Thu, 20 Jul 2023 22:08:28 -0600 Subject: [PATCH] Moved user to user.go --- gql.go | 82 ------------------------------------------------------- node.go | 16 +++++++---- user.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 87 deletions(-) create mode 100644 user.go diff --git a/gql.go b/gql.go index bf362e7..3934d26 100644 --- a/gql.go +++ b/gql.go @@ -24,7 +24,6 @@ import ( "crypto/sha512" "crypto/rand" "crypto/x509" - "github.com/google/uuid" ) type AuthReqJSON struct { @@ -153,87 +152,6 @@ func ParseAuthRespJSON(resp AuthRespJSON, ecdsa_curve elliptic.Curve, ecdh_curve return shared_secret, nil } -type User struct { - SimpleLockable - - Granted time.Time - Pubkey *ecdsa.PublicKey - Shared []byte -} - -type UserJSON struct { - SimpleLockableJSON - Granted time.Time `json:"granted"` - Pubkey []byte `json:"pubkey"` - Shared []byte `json:"shared"` -} - -func KeyID(pub *ecdsa.PublicKey) NodeID { - ser := elliptic.Marshal(pub.Curve, pub.X, pub.Y) - str := uuid.NewHash(sha512.New(), ZeroUUID, ser, 3) - return NodeID(str) -} - -func (user *User) Type() NodeType { - return NodeType("gql_user") -} - -func (user *User) Serialize() ([]byte, error) { - lockable_json := NewSimpleLockableJSON(&user.SimpleLockable) - pubkey, err := x509.MarshalPKIXPublicKey(user.Pubkey) - if err != nil { - return nil, err - } - - return json.MarshalIndent(&UserJSON{ - SimpleLockableJSON: lockable_json, - Granted: user.Granted, - Shared: user.Shared, - Pubkey: pubkey, - }, "", " ") -} - -func LoadUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, error) { - var j UserJSON - err := json.Unmarshal(data, &j) - if err != nil { - return nil, err - } - - pub, err := x509.ParsePKIXPublicKey(j.Pubkey) - if err != nil { - return nil, err - } - - var pubkey *ecdsa.PublicKey - switch pub.(type) { - case *ecdsa.PublicKey: - pubkey = pub.(*ecdsa.PublicKey) - default: - return nil, fmt.Errorf("Invalid key type") - } - - user := NewUser(j.Name, j.Granted, pubkey, j.Shared) - nodes[id] = &user - - err = RestoreSimpleLockable(ctx, &user, j.SimpleLockableJSON, nodes) - if err != nil { - return nil, err - } - - return &user, nil -} - -func NewUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []byte) User { - id := KeyID(pubkey) - return User{ - SimpleLockable: NewSimpleLockable(id, name), - Granted: granted, - Pubkey: pubkey, - Shared: shared, - } -} - func AuthHandler(ctx *Context, server *GQLThread) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { ctx.Log.Logf("gql", "GQL_AUTH_REQUEST: %s", r.RemoteAddr) diff --git a/node.go b/node.go index af7f9ee..67656e1 100644 --- a/node.go +++ b/node.go @@ -6,7 +6,9 @@ import ( badger "github.com/dgraph-io/badger/v3" "fmt" "encoding/binary" - "crypto/sha256" + "crypto/sha512" + "crypto/ecdsa" + "crypto/elliptic" ) // IDs are how nodes are uniquely identified, and can be serialized for the database @@ -32,14 +34,18 @@ func ParseID(str string) (NodeID, error) { return NodeID(id_uuid), nil } +func KeyID(pub *ecdsa.PublicKey) NodeID { + ser := elliptic.Marshal(pub.Curve, pub.X, pub.Y) + str := uuid.NewHash(sha512.New(), ZeroUUID, ser, 3) + return NodeID(str) +} + // Types are how nodes are associated with structs at runtime(and from the DB) type NodeType string func (node_type NodeType) Hash() uint64 { - hash := sha256.New() - hash.Write([]byte(node_type)) - bytes := hash.Sum(nil) + hash := sha512.Sum512([]byte(node_type)) - return binary.BigEndian.Uint64(bytes[(len(bytes)-9):(len(bytes)-1)]) + return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) } // Generate a random NodeID diff --git a/user.go b/user.go new file mode 100644 index 0000000..e360904 --- /dev/null +++ b/user.go @@ -0,0 +1,84 @@ +package graphvent + +import ( + "time" + "fmt" + "encoding/json" + "crypto/ecdsa" + "crypto/x509" +) + +type User struct { + SimpleLockable + + Granted time.Time + Pubkey *ecdsa.PublicKey + Shared []byte +} + +type UserJSON struct { + SimpleLockableJSON + Granted time.Time `json:"granted"` + Pubkey []byte `json:"pubkey"` + Shared []byte `json:"shared"` +} + +func (user *User) Type() NodeType { + return NodeType("gql_user") +} + +func (user *User) Serialize() ([]byte, error) { + lockable_json := NewSimpleLockableJSON(&user.SimpleLockable) + pubkey, err := x509.MarshalPKIXPublicKey(user.Pubkey) + if err != nil { + return nil, err + } + + return json.MarshalIndent(&UserJSON{ + SimpleLockableJSON: lockable_json, + Granted: user.Granted, + Shared: user.Shared, + Pubkey: pubkey, + }, "", " ") +} + +func LoadUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, error) { + var j UserJSON + err := json.Unmarshal(data, &j) + if err != nil { + return nil, err + } + + pub, err := x509.ParsePKIXPublicKey(j.Pubkey) + if err != nil { + return nil, err + } + + var pubkey *ecdsa.PublicKey + switch pub.(type) { + case *ecdsa.PublicKey: + pubkey = pub.(*ecdsa.PublicKey) + default: + return nil, fmt.Errorf("Invalid key type") + } + + user := NewUser(j.Name, j.Granted, pubkey, j.Shared) + nodes[id] = &user + + err = RestoreSimpleLockable(ctx, &user, j.SimpleLockableJSON, nodes) + if err != nil { + return nil, err + } + + return &user, nil +} + +func NewUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []byte) User { + id := KeyID(pubkey) + return User{ + SimpleLockable: NewSimpleLockable(id, name), + Granted: granted, + Pubkey: pubkey, + Shared: shared, + } +}