imports, comments and formatting
This commit is contained in:
parent
75a6ce1577
commit
91b8cc06b2
@ -1,30 +1,30 @@
|
|||||||
package bcrypt
|
package bcrypt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Verifier verifys passwords using Bcrypt.
|
// Verifier verifys passwords using Bcrypt.
|
||||||
type Verifier struct{}
|
type Verifier struct{}
|
||||||
|
|
||||||
// Verify verifys a Password.
|
// Verify verifys a Password.
|
||||||
func (bv *Verifier) Verify(password string, hashOnDB string) error {
|
func (bv *Verifier) Verify(password string, hashOnDB string) error {
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hashOnDB), []byte(password))
|
err := bcrypt.CompareHashAndPassword([]byte(hashOnDB), []byte(password))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("verify password: %w", err)
|
return fmt.Errorf("verify password: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash calculates a hash to be stored on the database.
|
// Hash calculates a hash to be stored on the database.
|
||||||
func (bv *Verifier) Hash(password string) (string, error) {
|
func (bv *Verifier) Hash(password string) (string, error) {
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("hash password: %w", err)
|
return "", fmt.Errorf("hash password: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(hash[:]), nil
|
return string(hash), nil
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config contains all needed configurations
|
// Config contains all needed configurations.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DatabaseConnection string
|
DatabaseConnection string
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig from path
|
// LoadConfig from path.
|
||||||
func LoadConfig() (*Config, error) {
|
func LoadConfig() (*Config, error) {
|
||||||
configuration := Config{
|
configuration := Config{
|
||||||
DatabaseConnection: os.Getenv("BUDGETEER_DB"),
|
DatabaseConnection: os.Getenv("BUDGETEER_DB"),
|
||||||
|
@ -10,10 +10,10 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TokenVerifier verifies Tokens
|
// TokenVerifier verifies Tokens.
|
||||||
type TokenVerifier struct{}
|
type TokenVerifier struct{}
|
||||||
|
|
||||||
// Token contains everything to authenticate a user
|
// Token contains everything to authenticate a user.
|
||||||
type Token struct {
|
type Token struct {
|
||||||
username string
|
username string
|
||||||
name string
|
name string
|
||||||
@ -26,7 +26,7 @@ const (
|
|||||||
secret = "uditapbzuditagscwxuqdflgzpbu´ßiaefnlmzeßtrubiadern"
|
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) {
|
func (tv *TokenVerifier) CreateToken(user *postgres.User) (string, error) {
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"usr": user.Email,
|
"usr": user.Email,
|
||||||
@ -50,7 +50,7 @@ var (
|
|||||||
ErrTokenExpired = fmt.Errorf("token has expired")
|
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) {
|
func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error) {
|
||||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
|
@ -5,9 +5,8 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/pressly/goose/v3"
|
|
||||||
|
|
||||||
_ "github.com/jackc/pgx/v4/stdlib" // needed for pg connection
|
_ "github.com/jackc/pgx/v4/stdlib" // needed for pg connection
|
||||||
|
"github.com/pressly/goose/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed schema/*.sql
|
//go:embed schema/*.sql
|
||||||
|
4
token.go
4
token.go
@ -5,7 +5,7 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Token contains data that authenticates a user
|
// Token contains data that authenticates a user.
|
||||||
type Token interface {
|
type Token interface {
|
||||||
GetUsername() string
|
GetUsername() string
|
||||||
GetName() string
|
GetName() string
|
||||||
@ -13,7 +13,7 @@ type Token interface {
|
|||||||
GetID() uuid.UUID
|
GetID() uuid.UUID
|
||||||
}
|
}
|
||||||
|
|
||||||
// TokenVerifier verifies a Token
|
// TokenVerifier verifies a Token.
|
||||||
type TokenVerifier interface {
|
type TokenVerifier interface {
|
||||||
VerifyToken(string) (Token, error)
|
VerifyToken(string) (Token, error)
|
||||||
CreateToken(*postgres.User) (string, error)
|
CreateToken(*postgres.User) (string, error)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user