Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
sha256 hash password. (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o authored Nov 19, 2023
1 parent 9d1ea87 commit d9bcde3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions internal/config/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (Member) Init(cmd *cobra.Command) error {
return err
}

cmd.PersistentFlags().Bool("member.file.hash", true, "member file provider: whether to hash passwords using sha256 (recommended)")
if err := viper.BindPFlag("member.file.hash", cmd.PersistentFlags().Lookup("member.file.hash")); err != nil {
return err
}

// object provider
cmd.PersistentFlags().String("member.object.users", "[]", "member object provider: users in JSON format")
if err := viper.BindPFlag("member.object.users", cmd.PersistentFlags().Lookup("member.object.users")); err != nil {
Expand Down Expand Up @@ -68,6 +73,7 @@ func (s *Member) Set() {

// file provider
s.File.Path = viper.GetString("member.file.path")
s.File.Hash = viper.GetBool("member.file.hash")

// object provider
if err := viper.UnmarshalKey("member.object.users", &s.Object.Users, viper.DecodeHook(
Expand Down
21 changes: 15 additions & 6 deletions internal/member/file/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"crypto/sha256"
"encoding/json"
"io"
"os"
Expand All @@ -18,6 +19,17 @@ type MemberProviderCtx struct {
config Config
}

func (provider *MemberProviderCtx) hash(password string) string {
// if hash is disabled, return password as plain text
if !provider.config.Hash {
return password
}

sha256 := sha256.New()
sha256.Write([]byte(password))
return string(sha256.Sum(nil))
}

func (provider *MemberProviderCtx) Connect() error {
return nil
}
Expand All @@ -35,8 +47,7 @@ func (provider *MemberProviderCtx) Authenticate(username string, password string
return "", types.MemberProfile{}, err
}

// TODO: Use hash function.
if entry.Password != password {
if entry.Password != provider.hash(password) {
return "", types.MemberProfile{}, types.ErrMemberInvalidPassword
}

Expand All @@ -58,8 +69,7 @@ func (provider *MemberProviderCtx) Insert(username string, password string, prof
}

entries[id] = MemberEntry{
// TODO: Use hash function.
Password: password,
Password: provider.hash(password),
Profile: profile,
}

Expand Down Expand Up @@ -94,8 +104,7 @@ func (provider *MemberProviderCtx) UpdatePassword(id string, password string) er
return types.ErrMemberDoesNotExist
}

// TODO: Use hash function.
entry.Password = password
entry.Password = provider.hash(password)
entries[id] = entry

return provider.serialize(entries)
Expand Down
1 change: 1 addition & 0 deletions internal/member/file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type MemberEntry struct {

type Config struct {
Path string
Hash bool
}

0 comments on commit d9bcde3

Please sign in to comment.