Skip to content

Commit

Permalink
v9.20.6: bcrypt salt cost from env var support added
Browse files Browse the repository at this point in the history
  • Loading branch information
AlperRehaYAZGAN committed Feb 6, 2024
1 parent 0273e08 commit ee583b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export CGO_ENABLED=0
exprot LOGS_DATABASE="postgresql://user:pass@localhost/logs?sslmode=disable"
export DATABASE="postgresql://user:pass@localhost/postgres?sslmode=disable"

# optional ENV_VARS
export BCRYPT_COST=10 # default is 12

# export is success you can run the project ✅
go run -tags pq ./examples/base serve

Expand Down Expand Up @@ -134,6 +137,9 @@ export CGO_ENABLED=0
export LOGS_DATABASE="postgresql://user:pass@localhost/logs?sslmode=disable"
export DATABASE="postgresql://user:pass@localhost/postgres?sslmode=disable"

# optional ENV_VARS
export BCRYPT_COST=10 # default is 12

# run the application
go run -tags pq main.go serve --http=0.0.0.0:8090
```
16 changes: 15 additions & 1 deletion models/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package models

import (
"errors"
"os"
"strconv"

"github.com/AlperRehaYAZGAN/postgresbase/tools/security"
"github.com/AlperRehaYAZGAN/postgresbase/tools/types"
Expand Down Expand Up @@ -47,8 +49,20 @@ func (m *Admin) SetPassword(password string) error {
return errors.New("The provided plain password is empty")
}

// !CHANGED: bcrypt salt amount is increased from 10 to 12 (old ersion: 10). Get it from env var
// get cost from env
cost := 12
costArg := os.Getenv("BCRYPT_COST")
if costArg != "" {
costAi, err := strconv.Atoi(costArg)
if err != nil {
return errors.New("The provided BCRYPT_COST is not a valid number")
}
cost = costAi
}

// hash the password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 12)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), cost)
if err != nil {
return err
}
Expand Down
15 changes: 14 additions & 1 deletion models/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strconv"
"time"
Expand Down Expand Up @@ -930,8 +931,20 @@ func (m *Record) SetPassword(password string) error {
return errors.New("The provided plain password is empty")
}

// !CHANGED: bcrypt salt amount is increased from 10 to 12 (old ersion: 10). Get it from env var
// get cost from env
cost := 12
costArg := os.Getenv("BCRYPT_COST")
if costArg != "" {
costAi, err := strconv.Atoi(costArg)
if err != nil {
return errors.New("The provided BCRYPT_COST is not a valid number")
}
cost = costAi
}

// hash the password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 12)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), cost)
if err != nil {
return err
}
Expand Down

0 comments on commit ee583b7

Please sign in to comment.