From 6bde5f986a55d331b3da75b4c8087ed2fde030ab Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Wed, 12 Aug 2020 11:28:49 +0200 Subject: [PATCH] Store password in client structure. No need to carry password around. --- client.go | 7 +++++- disk.go | 4 ++-- group.go | 62 +++++++++++++++++++++++++--------------------------- webclient.go | 25 +++++++++++---------- webserver.go | 2 +- 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/client.go b/client.go index fc54c95..32954cd 100644 --- a/client.go +++ b/client.go @@ -1,9 +1,14 @@ package main +type clientCredentials struct { + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` +} + type client interface { Group() *group Id() string - Username() string + Credentials() clientCredentials pushConn(id string, conn upConnection, tracks []upTrack, label string) error pushClient(id, username string, add bool) error } diff --git a/disk.go b/disk.go index f003731..b33f0fa 100644 --- a/disk.go +++ b/disk.go @@ -32,8 +32,8 @@ func (client *diskClient) Id() string { return client.id } -func (client *diskClient) Username() string { - return "RECORDING" +func (client *diskClient) Credentials() clientCredentials { + return clientCredentials{"RECORDING", ""} } func (client *diskClient) pushClient(id, username string, add bool) error { diff --git a/group.go b/group.go index 8495d16..c98af49 100644 --- a/group.go +++ b/group.go @@ -180,13 +180,13 @@ func delGroupUnlocked(name string) bool { return true } -func addClient(name string, c client, pass string) (*group, error) { +func addClient(name string, c client) (*group, error) { g, err := addGroup(name, nil) if err != nil { return nil, err } - perms, err := getPermission(g.description, c.Username(), pass) + perms, err := getPermission(g.description, c.Credentials()) if err != nil { return nil, err } @@ -214,13 +214,15 @@ func addClient(name string, c client, pass string) (*group, error) { g.clients[c.Id()] = c go func(clients []client) { - c.pushClient(c.Id(), c.Username(), true) + u := c.Credentials().Username + c.pushClient(c.Id(), u, true) for _, cc := range clients { - err := c.pushClient(cc.Id(), cc.Username(), true) + uu := cc.Credentials().Username + err := c.pushClient(cc.Id(), uu, true) if err == ErrClientDead { return } - cc.pushClient(c.Id(), c.Username(), true) + cc.pushClient(c.Id(), u, true) } }(g.getClientsUnlocked(c)) @@ -240,7 +242,7 @@ func delClient(c client) { go func(clients []client) { for _, cc := range clients { - cc.pushClient(c.Id(), c.Username(), false) + cc.pushClient(c.Id(), c.Credentials().Username, false) } }(g.getClientsUnlocked(nil)) } @@ -311,35 +313,31 @@ func (g *group) getChatHistory() []chatHistoryEntry { return h } -type groupUser struct { - Username string `json:"username,omitempty"` - Password string `json:"password,omitempty"` -} - -func matchUser(user, pass string, users []groupUser) (bool, bool) { +func matchUser(user clientCredentials, users []clientCredentials) (bool, bool) { for _, u := range users { if u.Username == "" { - if u.Password == "" || u.Password == pass { + if u.Password == "" || u.Password == user.Password { return true, true } - } else if u.Username == user { - return true, (u.Password == "" || u.Password == pass) + } else if u.Username == user.Username { + return true, + (u.Password == "" || u.Password == user.Password) } } return false, false } type groupDescription struct { - loadTime time.Time `json:"-"` - modTime time.Time `json:"-"` - fileSize int64 `json:"-"` - Public bool `json:"public,omitempty"` - MaxClients int `json:"max-clients,omitempty"` - AllowAnonymous bool `json:"allow-anonymous,omitempty"` - AllowRecording bool `json:"allow-recording,omitempty"` - Op []groupUser `json:"op,omitempty"` - Presenter []groupUser `json:"presenter,omitempty"` - Other []groupUser `json:"other,omitempty"` + loadTime time.Time `json:"-"` + modTime time.Time `json:"-"` + fileSize int64 `json:"-"` + Public bool `json:"public,omitempty"` + MaxClients int `json:"max-clients,omitempty"` + AllowAnonymous bool `json:"allow-anonymous,omitempty"` + AllowRecording bool `json:"allow-recording,omitempty"` + Op []clientCredentials `json:"op,omitempty"` + Presenter []clientCredentials `json:"presenter,omitempty"` + Other []clientCredentials `json:"other,omitempty"` } func descriptionChanged(name string, old *groupDescription) (bool, error) { @@ -384,18 +382,18 @@ func getDescription(name string) (*groupDescription, error) { return &desc, nil } -type userPermission struct { +type clientPermission struct { Op bool `json:"op,omitempty"` Present bool `json:"present,omitempty"` Record bool `json:"record,omitempty"` } -func getPermission(desc *groupDescription, user, pass string) (userPermission, error) { - var p userPermission - if !desc.AllowAnonymous && user == "" { +func getPermission(desc *groupDescription, creds clientCredentials) (clientPermission, error) { + var p clientPermission + if !desc.AllowAnonymous && creds.Username == "" { return p, userError("anonymous users not allowed in this group, please choose a username") } - if found, good := matchUser(user, pass, desc.Op); found { + if found, good := matchUser(creds, desc.Op); found { if good { p.Op = true p.Present = true @@ -406,14 +404,14 @@ func getPermission(desc *groupDescription, user, pass string) (userPermission, e } return p, userError("not authorised") } - if found, good := matchUser(user, pass, desc.Presenter); found { + if found, good := matchUser(creds, desc.Presenter); found { if good { p.Present = true return p, nil } return p, userError("not authorised") } - if found, good := matchUser(user, pass, desc.Other); found { + if found, good := matchUser(creds, desc.Other); found { if good { return p, nil } diff --git a/webclient.go b/webclient.go index 8e58036..46054d2 100644 --- a/webclient.go +++ b/webclient.go @@ -86,8 +86,8 @@ func isWSNormalError(err error) bool { type webClient struct { group *group id string - username string - permissions userPermission + credentials clientCredentials + permissions clientPermission requested map[string]uint32 done chan struct{} writeCh chan interface{} @@ -107,8 +107,8 @@ func (c *webClient) Id() string { return c.id } -func (c *webClient) Username() string { - return c.username +func (c *webClient) Credentials() clientCredentials { + return c.credentials } func (c *webClient) pushClient(id, username string, add bool) error { @@ -172,7 +172,7 @@ type clientMessage struct { Id string `json:"id,omitempty"` Username string `json:"username,omitempty"` Password string `json:"password,omitempty"` - Permissions userPermission `json:"permissions,omitempty"` + Permissions clientPermission `json:"permissions,omitempty"` Group string `json:"group,omitempty"` Value string `json:"value,omitempty"` Me bool `json:"me,omitempty"` @@ -461,8 +461,8 @@ func gotOffer(c *webClient, id string, offer webrtc.SessionDescription, renegoti return err } - if c.username != "" { - up.label = c.username + if u := c.Credentials().Username; u != "" { + up.label = u } err = up.pc.SetRemoteDescription(offer) if err != nil { @@ -630,8 +630,11 @@ func startClient(conn *websocket.Conn) (err error) { } c := &webClient{ - id: m.Id, - username: m.Username, + id: m.Id, + credentials: clientCredentials{ + m.Username, + m.Password, + }, actionCh: make(chan interface{}, 10), done: make(chan struct{}), } @@ -662,7 +665,7 @@ func startClient(conn *websocket.Conn) (err error) { c.writerDone = make(chan struct{}) go clientWriter(conn, c.writeCh, c.writerDone) - g, err := addClient(m.Group, c, m.Password) + g, err := addClient(m.Group, c) if err != nil { return } @@ -1015,7 +1018,7 @@ func handleClientMessage(c *webClient, m clientMessage) error { group: c.group, id: "recording", } - _, err := addClient(c.group.name, disk, "") + _, err := addClient(c.group.name, disk) if err != nil { disk.Close() return c.error(err) diff --git a/webserver.go b/webserver.go index c281db9..7926121 100644 --- a/webserver.go +++ b/webserver.go @@ -334,7 +334,7 @@ func checkGroupPermissions(w http.ResponseWriter, r *http.Request, group string) return false } - p, err := getPermission(desc, user, pass) + p, err := getPermission(desc, clientCredentials{user, pass}) if err != nil || !p.Record { return false }