Added NodeWithUsers interface to encompass any nodes that have a Users map

graph-rework-2
noah metz 2023-07-25 00:27:39 -06:00
parent a2395189a8
commit a72124e659
5 changed files with 25 additions and 16 deletions

@ -197,7 +197,7 @@ func AuthHandler(ctx *Context, server *GQLThread) func(http.ResponseWriter, *htt
key_id := KeyID(remote_id) key_id := KeyID(remote_id)
_, exists := server.Users[key_id] _, exists := server.UserMap[key_id]
if exists { if exists {
ctx.Log.Logf("gql", "REFRESHING AUTH FOR %s", key_id) ctx.Log.Logf("gql", "REFRESHING AUTH FOR %s", key_id)
} else { } else {
@ -215,7 +215,7 @@ func AuthHandler(ctx *Context, server *GQLThread) func(http.ResponseWriter, *htt
Resources: nil, Resources: nil,
}, },
}), func(context *StateContext) error { }), func(context *StateContext) error {
server.Users[key_id] = &new_user server.UserMap[key_id] = &new_user
return nil return nil
}) })
if err != nil { if err != nil {
@ -378,7 +378,7 @@ func NewResolveContext(ctx *Context, server *GQLThread, r *http.Request) (*Resol
return nil, fmt.Errorf("GQL_REQUEST_ERR: failed to parse ID from auth username: %s", username) return nil, fmt.Errorf("GQL_REQUEST_ERR: failed to parse ID from auth username: %s", username)
} }
user, exists := server.Users[auth_id] user, exists := server.UserMap[auth_id]
if exists == false { if exists == false {
return nil, fmt.Errorf("GQL_REQUEST_ERR: no existing authorization for client %s", auth_id) return nil, fmt.Errorf("GQL_REQUEST_ERR: no existing authorization for client %s", auth_id)
} }
@ -636,7 +636,7 @@ type GQLThread struct {
tls_key []byte tls_key []byte
tls_cert []byte tls_cert []byte
Listen string Listen string
Users map[NodeID]*User UserMap map[NodeID]*User
Key *ecdsa.PrivateKey Key *ecdsa.PrivateKey
ECDH ecdh.Curve ECDH ecdh.Curve
SubscribeLock sync.Mutex SubscribeLock sync.Mutex
@ -681,6 +681,10 @@ func (thread * GQLThread) Serialize() ([]byte, error) {
return json.MarshalIndent(&thread_json, "", " ") return json.MarshalIndent(&thread_json, "", " ")
} }
func (thread * GQLThread) Users() map[NodeID]*User {
return thread.UserMap
}
type GQLThreadJSON struct { type GQLThreadJSON struct {
ThreadJSON ThreadJSON
Listen string `json:"listen"` Listen string `json:"listen"`
@ -715,9 +719,9 @@ func NewGQLThreadJSON(thread *GQLThread) GQLThreadJSON {
panic(err) panic(err)
} }
users := make([]string, len(thread.Users)) users := make([]string, len(thread.UserMap))
i := 0 i := 0
for id, _ := range(thread.Users) { for id, _ := range(thread.UserMap) {
users[i] = id.String() users[i] = id.String()
i += 1 i += 1
} }
@ -747,7 +751,7 @@ var LoadGQLThread = LoadJSONNode(func(id NodeID, j GQLThreadJSON) (Node, error)
thread := NewGQLThread(id, j.Name, j.StateName, j.Listen, ecdh_curve, key, j.TLSCert, j.TLSKey) thread := NewGQLThread(id, j.Name, j.StateName, j.Listen, ecdh_curve, key, j.TLSCert, j.TLSKey)
return &thread, nil return &thread, nil
}, func(ctx *Context, thread *GQLThread, j GQLThreadJSON, nodes NodeMap) error { }, func(ctx *Context, thread *GQLThread, j GQLThreadJSON, nodes NodeMap) error {
thread.Users = map[NodeID]*User{} thread.UserMap = map[NodeID]*User{}
for _, id_str := range(j.Users) { for _, id_str := range(j.Users) {
ctx.Log.Logf("db", "THREAD_LOAD_USER: %s", id_str) ctx.Log.Logf("db", "THREAD_LOAD_USER: %s", id_str)
user_id, err := ParseID(id_str) user_id, err := ParseID(id_str)
@ -758,7 +762,7 @@ var LoadGQLThread = LoadJSONNode(func(id NodeID, j GQLThreadJSON) (Node, error)
if err != nil { if err != nil {
return err return err
} }
thread.Users[user_id] = user.(*User) thread.UserMap[user_id] = user.(*User)
} }
return RestoreThread(ctx, thread, j.ThreadJSON, nodes) return RestoreThread(ctx, thread, j.ThreadJSON, nodes)
@ -808,7 +812,7 @@ func NewGQLThread(id NodeID, name string, state_name string, listen string, ecdh
Thread: NewThread(id, name, state_name, []InfoType{"parent"}, gql_actions, gql_handlers), Thread: NewThread(id, name, state_name, []InfoType{"parent"}, gql_actions, gql_handlers),
Listen: listen, Listen: listen,
SubscribeListeners: []chan GraphSignal{}, SubscribeListeners: []chan GraphSignal{},
Users: map[NodeID]*User{}, UserMap: map[NodeID]*User{},
http_done: &sync.WaitGroup{}, http_done: &sync.WaitGroup{},
Key: key, Key: key,
ECDH: ecdh_curve, ECDH: ecdh_curve,

@ -289,13 +289,13 @@ func GQLLockableOwner(p graphql.ResolveParams) (interface{}, error) {
return owner, nil return owner, nil
} }
func GQLThreadUsers(p graphql.ResolveParams) (interface{}, error) { func GQLUserNodeUsers(p graphql.ResolveParams) (interface{}, error) {
ctx, err := PrepResolve(p) ctx, err := PrepResolve(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
node, ok := p.Source.(*GQLThread) node, ok := p.Source.(NodeWithUsers)
if ok == false || node == nil { if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to GQLThread") return nil, fmt.Errorf("Failed to cast source to GQLThread")
} }
@ -303,9 +303,9 @@ func GQLThreadUsers(p graphql.ResolveParams) (interface{}, error) {
var users []*User var users []*User
context := NewReadContext(ctx.Context) context := NewReadContext(ctx.Context)
err = UseStates(context, ctx.User, NewLockInfo(node, []string{"users"}), func(context *StateContext) error { err = UseStates(context, ctx.User, NewLockInfo(node, []string{"users"}), func(context *StateContext) error {
users = make([]*User, len(node.Users)) users = make([]*User, len(node.Users()))
i := 0 i := 0
for _, user := range(node.Users) { for _, user := range(node.Users()) {
users[i] = user users[i] = user
i += 1 i += 1
} }

@ -72,7 +72,7 @@ func TestGQLDBLoad(t * testing.T) {
err = UpdateStates(context, &gql, NewLockMap( err = UpdateStates(context, &gql, NewLockMap(
NewLockInfo(&gql, []string{"users"}), NewLockInfo(&gql, []string{"users"}),
), func(context *StateContext) error { ), func(context *StateContext) error {
gql.Users[u1.ID()] = &u1 gql.UserMap[u1.ID()] = &u1
err := LinkThreads(context, &gql, &gql, ChildInfo{&t1, map[InfoType]interface{}{ err := LinkThreads(context, &gql, &gql, ChildInfo{&t1, map[InfoType]interface{}{
"parent": &info, "parent": &info,
@ -116,7 +116,7 @@ func TestGQLDBLoad(t * testing.T) {
ctx.Log.Logf("test", "\n%s\n\n", ser) ctx.Log.Logf("test", "\n%s\n\n", ser)
dependency := gql_loaded.(*GQLThread).Thread.Dependencies[l1.ID()].(*Listener) dependency := gql_loaded.(*GQLThread).Thread.Dependencies[l1.ID()].(*Listener)
l1_loaded = dependency l1_loaded = dependency
u_loaded := gql_loaded.(*GQLThread).Users[u1.ID()] u_loaded := gql_loaded.(*GQLThread).UserMap[u1.ID()]
err = UseStates(context, gql_loaded, NewLockInfo(u_loaded, nil), func(context *StateContext) error { err = UseStates(context, gql_loaded, NewLockInfo(u_loaded, nil), func(context *StateContext) error {
ser, err := u_loaded.Serialize() ser, err := u_loaded.Serialize()
ctx.Log.Logf("test", "\n%s\n\n", ser) ctx.Log.Logf("test", "\n%s\n\n", ser)

@ -92,7 +92,7 @@ var GQLTypeGQLThread = NewSingleton(func() *graphql.Object {
gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{ gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{
Type: GQLTypeUser.List, Type: GQLTypeUser.List,
Resolve: GQLThreadUsers, Resolve: GQLUserNodeUsers,
}) })
gql_type_gql_thread.AddFieldConfig("Listen", &graphql.Field{ gql_type_gql_thread.AddFieldConfig("Listen", &graphql.Field{

@ -8,6 +8,11 @@ import (
"crypto/x509" "crypto/x509"
) )
type NodeWithUsers interface {
Node
Users() map[NodeID]*User
}
type User struct { type User struct {
Lockable Lockable