Renamed GQLUser to User

graph-rework-2
noah metz 2023-07-20 22:03:25 -06:00
parent 4dc42a813e
commit e50b550cd7
4 changed files with 29 additions and 30 deletions

@ -184,8 +184,7 @@ func NewContext(db * badger.DB, log Logger) * Context {
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = ctx.RegisterNodeType(NewNodeDef((*User)(nil), LoadUser, GQLTypeUser()))
err = ctx.RegisterNodeType(NewNodeDef((*GQLUser)(nil), LoadGQLUser, GQLTypeGQLUser()))
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -153,7 +153,7 @@ func ParseAuthRespJSON(resp AuthRespJSON, ecdsa_curve elliptic.Curve, ecdh_curve
return shared_secret, nil return shared_secret, nil
} }
type GQLUser struct { type User struct {
SimpleLockable SimpleLockable
Granted time.Time Granted time.Time
@ -161,7 +161,7 @@ type GQLUser struct {
Shared []byte Shared []byte
} }
type GQLUserJSON struct { type UserJSON struct {
SimpleLockableJSON SimpleLockableJSON
Granted time.Time `json:"granted"` Granted time.Time `json:"granted"`
Pubkey []byte `json:"pubkey"` Pubkey []byte `json:"pubkey"`
@ -174,18 +174,18 @@ func KeyID(pub *ecdsa.PublicKey) NodeID {
return NodeID(str) return NodeID(str)
} }
func (user *GQLUser) Type() NodeType { func (user *User) Type() NodeType {
return NodeType("gql_user") return NodeType("gql_user")
} }
func (user *GQLUser) Serialize() ([]byte, error) { func (user *User) Serialize() ([]byte, error) {
lockable_json := NewSimpleLockableJSON(&user.SimpleLockable) lockable_json := NewSimpleLockableJSON(&user.SimpleLockable)
pubkey, err := x509.MarshalPKIXPublicKey(user.Pubkey) pubkey, err := x509.MarshalPKIXPublicKey(user.Pubkey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return json.MarshalIndent(&GQLUserJSON{ return json.MarshalIndent(&UserJSON{
SimpleLockableJSON: lockable_json, SimpleLockableJSON: lockable_json,
Granted: user.Granted, Granted: user.Granted,
Shared: user.Shared, Shared: user.Shared,
@ -193,8 +193,8 @@ func (user *GQLUser) Serialize() ([]byte, error) {
}, "", " ") }, "", " ")
} }
func LoadGQLUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, error) { func LoadUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, error) {
var j GQLUserJSON var j UserJSON
err := json.Unmarshal(data, &j) err := json.Unmarshal(data, &j)
if err != nil { if err != nil {
return nil, err return nil, err
@ -213,7 +213,7 @@ func LoadGQLUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, err
return nil, fmt.Errorf("Invalid key type") return nil, fmt.Errorf("Invalid key type")
} }
user := NewGQLUser(j.Name, j.Granted, pubkey, j.Shared) user := NewUser(j.Name, j.Granted, pubkey, j.Shared)
nodes[id] = &user nodes[id] = &user
err = RestoreSimpleLockable(ctx, &user, j.SimpleLockableJSON, nodes) err = RestoreSimpleLockable(ctx, &user, j.SimpleLockableJSON, nodes)
@ -224,9 +224,9 @@ func LoadGQLUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, err
return &user, nil return &user, nil
} }
func NewGQLUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []byte) GQLUser { func NewUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []byte) User {
id := KeyID(pubkey) id := KeyID(pubkey)
return GQLUser{ return User{
SimpleLockable: NewSimpleLockable(id, name), SimpleLockable: NewSimpleLockable(id, name),
Granted: granted, Granted: granted,
Pubkey: pubkey, Pubkey: pubkey,
@ -281,7 +281,7 @@ func AuthHandler(ctx *Context, server *GQLThread) func(http.ResponseWriter, *htt
} else { } else {
ctx.Log.Logf("gql", "AUTHORIZING NEW USER %s - %s", key_id, shared) ctx.Log.Logf("gql", "AUTHORIZING NEW USER %s - %s", key_id, shared)
new_user := NewGQLUser(fmt.Sprintf("GQL_USER %s", key_id.String()), time.Now(), remote_id, shared) new_user := NewUser(fmt.Sprintf("GQL_USER %s", key_id.String()), time.Now(), remote_id, shared)
err := UpdateStates(ctx, []Node{server}, func(nodes NodeMap) error { err := UpdateStates(ctx, []Node{server}, func(nodes NodeMap) error {
server.Users[key_id] = &new_user server.Users[key_id] = &new_user
return nil return nil
@ -429,7 +429,7 @@ func checkForAuthHeader(header http.Header) (string, bool) {
return "", false return "", false
} }
func CheckAuth(server *GQLThread, r *http.Request) (*GQLUser, error) { func CheckAuth(server *GQLThread, r *http.Request) (*User, error) {
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if ok == false { if ok == false {
return nil, fmt.Errorf("GQL_REQUEST_ERR: no auth header included in request header") return nil, fmt.Errorf("GQL_REQUEST_ERR: no auth header included in request header")
@ -691,7 +691,7 @@ type GQLThread struct {
http_server *http.Server http_server *http.Server
http_done *sync.WaitGroup http_done *sync.WaitGroup
Listen string Listen string
Users map[NodeID]*GQLUser Users map[NodeID]*User
Key *ecdsa.PrivateKey Key *ecdsa.PrivateKey
ECDH ecdh.Curve ECDH ecdh.Curve
} }
@ -780,7 +780,7 @@ func LoadGQLThread(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, e
} }
thread := NewGQLThread(id, j.Name, j.StateName, j.Listen, ecdh_curve, key) thread := NewGQLThread(id, j.Name, j.StateName, j.Listen, ecdh_curve, key)
thread.Users = map[NodeID]*GQLUser{} thread.Users = map[NodeID]*User{}
for _, id_str := range(j.Users) { for _, id_str := range(j.Users) {
id, err := ParseID(id_str) id, err := ParseID(id_str)
if err != nil { if err != nil {
@ -790,7 +790,7 @@ func LoadGQLThread(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, e
if err != nil { if err != nil {
return nil, err return nil, err
} }
thread.Users[id] = user.(*GQLUser) thread.Users[id] = user.(*User)
} }
nodes[id] = &thread nodes[id] = &thread
@ -806,7 +806,7 @@ func NewGQLThread(id NodeID, name string, state_name string, listen string, ecdh
return GQLThread{ return GQLThread{
SimpleThread: NewSimpleThread(id, name, state_name, reflect.TypeOf((*ParentThreadInfo)(nil)), gql_actions, gql_handlers), SimpleThread: NewSimpleThread(id, name, state_name, reflect.TypeOf((*ParentThreadInfo)(nil)), gql_actions, gql_handlers),
Listen: listen, Listen: listen,
Users: map[NodeID]*GQLUser{}, Users: map[NodeID]*User{},
http_done: &sync.WaitGroup{}, http_done: &sync.WaitGroup{},
Key: key, Key: key,
ECDH: ecdh_curve, ECDH: ecdh_curve,

@ -385,11 +385,11 @@ func GQLLockableOwner(p graphql.ResolveParams) (interface{}, error) {
return owner, nil return owner, nil
} }
var gql_type_gql_user *graphql.Object = nil var gql_type_user *graphql.Object = nil
func GQLTypeGQLUser() * graphql.Object { func GQLTypeUser() * graphql.Object {
if gql_type_gql_user == nil { if gql_type_user == nil {
gql_type_gql_user = graphql.NewObject(graphql.ObjectConfig{ gql_type_user = graphql.NewObject(graphql.ObjectConfig{
Name: "GQLUser", Name: "User",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceNode(), GQLInterfaceNode(),
GQLInterfaceLockable(), GQLInterfaceLockable(),
@ -412,32 +412,32 @@ func GQLTypeGQLUser() * graphql.Object {
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
gql_type_gql_user.AddFieldConfig("ID", &graphql.Field{ gql_type_user.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String, Type: graphql.String,
Resolve: GQLNodeID, Resolve: GQLNodeID,
}) })
gql_type_gql_user.AddFieldConfig("Name", &graphql.Field{ gql_type_user.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String, Type: graphql.String,
Resolve: GQLLockableName, Resolve: GQLLockableName,
}) })
gql_type_gql_user.AddFieldConfig("Requirements", &graphql.Field{ gql_type_user.AddFieldConfig("Requirements", &graphql.Field{
Type: GQLListLockable(), Type: GQLListLockable(),
Resolve: GQLLockableRequirements, Resolve: GQLLockableRequirements,
}) })
gql_type_gql_user.AddFieldConfig("Owner", &graphql.Field{ gql_type_user.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceLockable(), Type: GQLInterfaceLockable(),
Resolve: GQLLockableOwner, Resolve: GQLLockableOwner,
}) })
gql_type_gql_user.AddFieldConfig("Dependencies", &graphql.Field{ gql_type_user.AddFieldConfig("Dependencies", &graphql.Field{
Type: GQLListLockable(), Type: GQLListLockable(),
Resolve: GQLLockableDependencies, Resolve: GQLLockableDependencies,
}) })
} }
return gql_type_gql_user return gql_type_user
} }
var gql_type_gql_thread *graphql.Object = nil var gql_type_gql_thread *graphql.Object = nil

@ -67,7 +67,7 @@ func TestGQLDBLoad(t * testing.T) {
u1_shared := []byte{0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x23, 0x45, 0x67} u1_shared := []byte{0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x23, 0x45, 0x67}
u1_r := NewGQLUser("Test User", time.Now(), &u1_key.PublicKey, u1_shared) u1_r := NewUser("Test User", time.Now(), &u1_key.PublicKey, u1_shared)
u1 := &u1_r u1 := &u1_r
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)