Skip to content

Commit

Permalink
storing passwords as hash
Browse files Browse the repository at this point in the history
  • Loading branch information
itishrishikesh committed Dec 20, 2023
1 parent 51d4a28 commit 9e6675d
Show file tree
Hide file tree
Showing 13 changed files with 865 additions and 6 deletions.
13 changes: 10 additions & 3 deletions controller/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@ import (
"github.com/itishrishikesh/gofeed/internal/database"
"github.com/itishrishikesh/gofeed/models"
"github.com/itishrishikesh/gofeed/utils"
"golang.org/x/crypto/bcrypt"
)

func (apiCfg *ApiConfig) CreateUserHandler(writer http.ResponseWriter, request *http.Request) {
type parameters struct {
Username string `json:"username"`
Password string `json:"password"`
}

params := parameters{}
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&params)
if err != nil {
utils.RespondWithError(writer, constants.HTTP_BAD_REQUEST, "Error parsing JSON")
log.Println("D#1ORHXO - User passed incorrect JSON")
log.Println("E#1PDKJ2 - User passed incorrect JSON")
return
}

hash, err := bcrypt.GenerateFromPassword([]byte(params.Password), bcrypt.MinCost)
if err != nil {
utils.RespondWithError(writer, constants.HTTP_BAD_REQUEST, "Error parsing password")
log.Println("E#1PDKIV - Please check your password string")
return
}

Expand All @@ -34,7 +41,7 @@ func (apiCfg *ApiConfig) CreateUserHandler(writer http.ResponseWriter, request *
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Name: params.Username,
Password: params.Password,
Password: string(hash),
})
if err != nil {
utils.RespondWithError(writer, constants.HTTP_ERROR, "Couldn't create user")
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ require (
)

require github.com/golang-jwt/jwt v3.2.2+incompatible

require golang.org/x/crypto v0.17.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
2 changes: 1 addition & 1 deletion internal/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions internal/database/users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/golang.org/x/crypto/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/crypto/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions vendor/golang.org/x/crypto/bcrypt/base64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9e6675d

Please sign in to comment.