imports, comments and formatting

This commit is contained in:
Jan Bader 2022-02-20 20:58:48 +00:00
parent 75a6ce1577
commit 91b8cc06b2
5 changed files with 39 additions and 40 deletions

View File

@ -26,5 +26,5 @@ func (bv *Verifier) Hash(password string) (string, error) {
return "", fmt.Errorf("hash password: %w", err) return "", fmt.Errorf("hash password: %w", err)
} }
return string(hash[:]), nil return string(hash), nil
} }

View File

@ -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"),

View File

@ -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 {

View File

@ -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

View File

@ -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)