Skip to content

Commit

Permalink
Extract more generic versions of setting and checking configuration p…
Browse files Browse the repository at this point in the history
…asswords
  • Loading branch information
olabiniV2 committed Mar 26, 2020
1 parent a6dc451 commit dd9b553
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions cmd/grumble/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ func (server *Server) RootChannel() *Channel {
return root
}

// Set password as the new SuperUser password
func (server *Server) SetSuperUserPassword(password string) {
func (server *Server) setConfigPassword(key, password string) {
saltBytes := make([]byte, 24)
_, err := rand.Read(saltBytes)
if err != nil {
Expand All @@ -190,7 +189,6 @@ func (server *Server) SetSuperUserPassword(password string) {
digest := hex.EncodeToString(hasher.Sum(nil))

// Could be racy, but shouldn't really matter...
key := "SuperUserPassword"
val := "sha1$" + salt + "$" + digest
server.cfg.Set(key, val)

Expand All @@ -199,9 +197,13 @@ func (server *Server) SetSuperUserPassword(password string) {
}
}

// CheckSuperUserPassword checks whether password matches the set SuperUser password.
func (server *Server) CheckSuperUserPassword(password string) bool {
parts := strings.Split(server.cfg.StringValue("SuperUserPassword"), "$")
// Set password as the new SuperUser password
func (server *Server) SetSuperUserPassword(password string) {
server.setConfigPassword("SuperUserPassword", password)
}

func (server *Server) checkConfigPassword(key, password string) bool {
parts := strings.Split(server.cfg.StringValue(key), "$")
if len(parts) != 3 {
return false
}
Expand Down Expand Up @@ -239,6 +241,11 @@ func (server *Server) CheckSuperUserPassword(password string) bool {
return false
}

// CheckSuperUserPassword checks whether password matches the set SuperUser password.
func (server *Server) CheckSuperUserPassword(password string) bool {
return server.checkConfigPassword("SuperUserPassword", password)
}

// Called by the server to initiate a new client connection.
func (server *Server) handleIncomingClient(conn net.Conn) (err error) {
client := new(Client)
Expand Down

0 comments on commit dd9b553

Please sign in to comment.