Moved user to user.go

graph-rework-2
noah metz 2023-07-20 22:08:28 -06:00
parent e50b550cd7
commit 99d4f18daf
3 changed files with 95 additions and 87 deletions

@ -24,7 +24,6 @@ import (
"crypto/sha512" "crypto/sha512"
"crypto/rand" "crypto/rand"
"crypto/x509" "crypto/x509"
"github.com/google/uuid"
) )
type AuthReqJSON struct { type AuthReqJSON struct {
@ -153,87 +152,6 @@ func ParseAuthRespJSON(resp AuthRespJSON, ecdsa_curve elliptic.Curve, ecdh_curve
return shared_secret, nil 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) { func AuthHandler(ctx *Context, server *GQLThread) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx.Log.Logf("gql", "GQL_AUTH_REQUEST: %s", r.RemoteAddr) ctx.Log.Logf("gql", "GQL_AUTH_REQUEST: %s", r.RemoteAddr)

@ -6,7 +6,9 @@ import (
badger "github.com/dgraph-io/badger/v3" badger "github.com/dgraph-io/badger/v3"
"fmt" "fmt"
"encoding/binary" "encoding/binary"
"crypto/sha256" "crypto/sha512"
"crypto/ecdsa"
"crypto/elliptic"
) )
// IDs are how nodes are uniquely identified, and can be serialized for the database // 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 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) // Types are how nodes are associated with structs at runtime(and from the DB)
type NodeType string type NodeType string
func (node_type NodeType) Hash() uint64 { func (node_type NodeType) Hash() uint64 {
hash := sha256.New() hash := sha512.Sum512([]byte(node_type))
hash.Write([]byte(node_type))
bytes := hash.Sum(nil)
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 // Generate a random NodeID

@ -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,
}
}