From 91b8cc06b244be0ed82ba7a41d46836d4b62d9c3 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Sun, 20 Feb 2022 20:58:48 +0000 Subject: [PATCH] imports, comments and formatting --- bcrypt/verifier.go | 60 +++++++++++++++++++++++----------------------- config/config.go | 4 ++-- jwt/login.go | 8 +++---- postgres/conn.go | 3 +-- token.go | 4 ++-- 5 files changed, 39 insertions(+), 40 deletions(-) diff --git a/bcrypt/verifier.go b/bcrypt/verifier.go index 22dc5bc..8df284f 100644 --- a/bcrypt/verifier.go +++ b/bcrypt/verifier.go @@ -1,30 +1,30 @@ -package bcrypt - -import ( - "fmt" - - "golang.org/x/crypto/bcrypt" -) - -// Verifier verifys passwords using Bcrypt. -type Verifier struct{} - -// Verify verifys a Password. -func (bv *Verifier) Verify(password string, hashOnDB string) error { - err := bcrypt.CompareHashAndPassword([]byte(hashOnDB), []byte(password)) - if err != nil { - return fmt.Errorf("verify password: %w", err) - } - - return nil -} - -// Hash calculates a hash to be stored on the database. -func (bv *Verifier) Hash(password string) (string, error) { - hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) - if err != nil { - return "", fmt.Errorf("hash password: %w", err) - } - - return string(hash[:]), nil -} +package bcrypt + +import ( + "fmt" + + "golang.org/x/crypto/bcrypt" +) + +// Verifier verifys passwords using Bcrypt. +type Verifier struct{} + +// Verify verifys a Password. +func (bv *Verifier) Verify(password string, hashOnDB string) error { + err := bcrypt.CompareHashAndPassword([]byte(hashOnDB), []byte(password)) + if err != nil { + return fmt.Errorf("verify password: %w", err) + } + + return nil +} + +// Hash calculates a hash to be stored on the database. +func (bv *Verifier) Hash(password string) (string, error) { + hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + return "", fmt.Errorf("hash password: %w", err) + } + + return string(hash), nil +} diff --git a/config/config.go b/config/config.go index 8c0ede6..7c3e324 100644 --- a/config/config.go +++ b/config/config.go @@ -4,12 +4,12 @@ import ( "os" ) -// Config contains all needed configurations +// Config contains all needed configurations. type Config struct { DatabaseConnection string } -// LoadConfig from path +// LoadConfig from path. func LoadConfig() (*Config, error) { configuration := Config{ DatabaseConnection: os.Getenv("BUDGETEER_DB"), diff --git a/jwt/login.go b/jwt/login.go index b0d5144..34fbf98 100644 --- a/jwt/login.go +++ b/jwt/login.go @@ -10,10 +10,10 @@ import ( "github.com/google/uuid" ) -// TokenVerifier verifies Tokens +// TokenVerifier verifies Tokens. type TokenVerifier struct{} -// Token contains everything to authenticate a user +// Token contains everything to authenticate a user. type Token struct { username string name string @@ -26,7 +26,7 @@ const ( secret = "uditapbzuditagscwxuqdflgzpbu´ßiaefnlmzeßtrubiadern" ) -// CreateToken creates a new token from username and name +// CreateToken creates a new token from username and name. func (tv *TokenVerifier) CreateToken(user *postgres.User) (string, error) { token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "usr": user.Email, @@ -50,7 +50,7 @@ var ( ErrTokenExpired = fmt.Errorf("token has expired") ) -// VerifyToken verifys a given string-token +// VerifyToken verifys a given string-token. func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error) { token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { diff --git a/postgres/conn.go b/postgres/conn.go index 86f6de2..6a5c814 100644 --- a/postgres/conn.go +++ b/postgres/conn.go @@ -5,9 +5,8 @@ import ( "embed" "fmt" - "github.com/pressly/goose/v3" - _ "github.com/jackc/pgx/v4/stdlib" // needed for pg connection + "github.com/pressly/goose/v3" ) //go:embed schema/*.sql diff --git a/token.go b/token.go index 1a9cff3..944a25a 100644 --- a/token.go +++ b/token.go @@ -5,7 +5,7 @@ import ( "github.com/google/uuid" ) -// Token contains data that authenticates a user +// Token contains data that authenticates a user. type Token interface { GetUsername() string GetName() string @@ -13,7 +13,7 @@ type Token interface { GetID() uuid.UUID } -// TokenVerifier verifies a Token +// TokenVerifier verifies a Token. type TokenVerifier interface { VerifyToken(string) (Token, error) CreateToken(*postgres.User) (string, error)