diff --git a/Makefile b/Makefile index 7e0626e..14ff28a 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ cmds: go build -o bin/ ./tools/cmd/fgen go build -o bin/ ./tools/cmd/qmgr go build -o bin/ ./tools/cmd/mailer - go test MY/webapp/tests -c -o bin/ + go test MY/webapp/tests/... -c -o bin/ site: go tool ssgod diff --git a/common/auth.go b/common/auth.go index 8816606..194ff9b 100644 --- a/common/auth.go +++ b/common/auth.go @@ -2,12 +2,10 @@ package common import ( "errors" - "golang.org/x/crypto/bcrypt" "log" "github.com/gofiber/fiber/v2" _ "github.com/mattn/go-sqlite3" - sq "github.com/Masterminds/squirrel" "github.com/gofiber/fiber/v2/middleware/session" "MY/webapp/data" @@ -39,37 +37,3 @@ func AuthCheck(c *fiber.Ctx, needs_admin bool) (*session.Session, error) { return sess, errors.New("Authentication, permission failure") } } - -func LogoutUser(c *fiber.Ctx) error { - sess, err := STORE.Get(c) - if err != nil { return err } - - err = sess.Destroy() - if err != nil { return err } - - err = sess.Save() - return err -} - -func LoginUser(result *data.User, login *data.Login) (bool, error) { - sql, args, err := sq.Select("username, password"). - From("user").Where("username=?", login.Username).ToSql() - - if err != nil { return false, err } - - err = data.DB.Get(result, sql, args...) - if err != nil { return false, err } - - pass_good := bcrypt.CompareHashAndPassword([]byte(result.Password), []byte(login.Password)) - if pass_good != nil { return false, pass_good } - - return login.Username == result.Username && pass_good == nil, nil -} - -func SetUserPassword(user *data.User) error { - hashed, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12) - if err != nil { return err } - - user.Password = string(hashed) - return nil -} diff --git a/auth/handlers.go b/features/auth/api.go similarity index 97% rename from auth/handlers.go rename to features/auth/api.go index bb07af6..9d0dbaf 100644 --- a/auth/handlers.go +++ b/features/auth/api.go @@ -1,4 +1,4 @@ -package auth +package features_auth import ( "github.com/gofiber/fiber/v2" @@ -68,8 +68,7 @@ func PostApiLogin(c *fiber.Ctx) error { } } - -func Setup(app *fiber.App) { +func SetupApi(app *fiber.App) { app.Get("/api/authcheck", GetApiAuthCheck) app.Get("/api/logout", GetApiLogout) app.Post("/api/register", PostApiRegister) diff --git a/features/auth/db.go b/features/auth/db.go new file mode 100644 index 0000000..12190e4 --- /dev/null +++ b/features/auth/db.go @@ -0,0 +1,46 @@ +package features_auth + +import ( + "golang.org/x/crypto/bcrypt" + + "github.com/gofiber/fiber/v2" + _ "github.com/mattn/go-sqlite3" + sq "github.com/Masterminds/squirrel" + + "MY/webapp/data" + "MY/webapp/common" +) + +func SetUserPassword(user *data.User) error { + hashed, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12) + if err != nil { return err } + + user.Password = string(hashed) + return nil +} + +func LogoutUser(c *fiber.Ctx) error { + sess, err := common.STORE.Get(c) + if err != nil { return err } + + err = sess.Destroy() + if err != nil { return err } + + err = sess.Save() + return err +} + +func LoginUser(result *data.User, login *data.Login) (bool, error) { + sql, args, err := sq.Select("username, password"). + From("user").Where("username=?", login.Username).ToSql() + + if err != nil { return false, err } + + err = data.DB.Get(result, sql, args...) + if err != nil { return false, err } + + pass_good := bcrypt.CompareHashAndPassword([]byte(result.Password), []byte(login.Password)) + if pass_good != nil { return false, pass_good } + + return login.Username == result.Username && pass_good == nil, nil +} diff --git a/features/auth/init.go b/features/auth/init.go new file mode 100644 index 0000000..cabe5d1 --- /dev/null +++ b/features/auth/init.go @@ -0,0 +1,10 @@ +package features_auth + +import ( + "github.com/gofiber/fiber/v2" +) + +func Setup(app *fiber.App) { + SetupApi(app) + SetupViews(app) +} diff --git a/features/auth/views.go b/features/auth/views.go new file mode 100644 index 0000000..d31fe36 --- /dev/null +++ b/features/auth/views.go @@ -0,0 +1,11 @@ +package features_auth + +import ( + "github.com/gofiber/fiber/v2" + . "MY/webapp/common" +) + +func SetupViews(app *fiber.App) { + err := ConfigViews(app, "views/auth") + if err != nil { panic(err) } +} diff --git a/features/init.go b/features/init.go index c166307..d7ee419 100644 --- a/features/init.go +++ b/features/init.go @@ -7,12 +7,14 @@ import ( "MY/webapp/features/shopping" "MY/webapp/features/fakepay" "MY/webapp/features/admin" + "MY/webapp/features/auth" ) func Setup(app *fiber.App) { + features_auth.Setup(app) + features_admin.Setup(app) features_email.Setup(app) features_paypal.Setup(app) features_shopping.Setup(app) features_fakepay.Setup(app) - features_admin.Setup(app) } diff --git a/main.go b/main.go index abd4d65..3e8b9e1 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,6 @@ import ( "MY/webapp/data" "MY/webapp/config" "MY/webapp/common" - "MY/webapp/auth" "MY/webapp/features" ) @@ -48,7 +47,6 @@ func main() { common.STORE = session.New() data.Setup(config.Settings.Database.Driver, config.Settings.Database.Url) - auth.Setup(app) features.Setup(app) // this sets up graceful shutdown diff --git a/tests/admin/admin_ui_test.go b/tests/admin/admin_ui_test.go index 197504d..d7fc022 100644 --- a/tests/admin/admin_ui_test.go +++ b/tests/admin/admin_ui_test.go @@ -6,6 +6,7 @@ import ( // "github.com/stretchr/testify/require" // "MY/webapp/data" // sq "github.com/Masterminds/squirrel" + . "MY/webapp/tests/tools" ) func TestTableListing(t *testing.T) { diff --git a/tests/base_test.go b/tests/auth/login_test.go similarity index 95% rename from tests/base_test.go rename to tests/auth/login_test.go index d0f279f..df0282b 100644 --- a/tests/base_test.go +++ b/tests/auth/login_test.go @@ -1,10 +1,11 @@ -package tests +package tests_auth import ( "testing" // "github.com/stretchr/testify/require" "MY/webapp/data" sq "github.com/Masterminds/squirrel" + . "MY/webapp/tests/tools" ) func deleteTestUser(username string) { diff --git a/tests/fakepay/example_test.go b/tests/fakepay/example_test.go index f7318b7..96f333d 100644 --- a/tests/fakepay/example_test.go +++ b/tests/fakepay/example_test.go @@ -2,11 +2,11 @@ package tests import ( "testing" - "github.com/stretchr/testify/require" + assert "github.com/stretchr/testify/require" ) func TestLogin(t *testing.T) { - assert.Equal(true, false) + assert.Equal(t, true, false) } func TestMain(m *testing.M) { diff --git a/tests/main.go b/tests/main.go new file mode 100644 index 0000000..a5c0af0 --- /dev/null +++ b/tests/main.go @@ -0,0 +1,15 @@ +package tests + +import ( + "testing" + // "github.com/stretchr/testify/require" + "MY/webapp/data" +) + +func TestMain(m *testing.M) { + data.Setup("sqlite3", "./db.sqlite3") + + m.Run() + + data.Shutdown() +} diff --git a/tests/paypal/example_test.go b/tests/paypal/example_test.go index f7318b7..96f333d 100644 --- a/tests/paypal/example_test.go +++ b/tests/paypal/example_test.go @@ -2,11 +2,11 @@ package tests import ( "testing" - "github.com/stretchr/testify/require" + assert "github.com/stretchr/testify/require" ) func TestLogin(t *testing.T) { - assert.Equal(true, false) + assert.Equal(t, true, false) } func TestMain(m *testing.M) { diff --git a/tests/shopping/example_test.go b/tests/shopping/example_test.go index f7318b7..96f333d 100644 --- a/tests/shopping/example_test.go +++ b/tests/shopping/example_test.go @@ -2,11 +2,11 @@ package tests import ( "testing" - "github.com/stretchr/testify/require" + assert "github.com/stretchr/testify/require" ) func TestLogin(t *testing.T) { - assert.Equal(true, false) + assert.Equal(t, true, false) } func TestMain(m *testing.M) { diff --git a/tests/tools.go b/tests/tools/helpers.go similarity index 100% rename from tests/tools.go rename to tests/tools/helpers.go diff --git a/tools/cmd/fgen/templates/tests/example_test.go b/tools/cmd/fgen/templates/tests/example_test.go index f7318b7..96f333d 100644 --- a/tools/cmd/fgen/templates/tests/example_test.go +++ b/tools/cmd/fgen/templates/tests/example_test.go @@ -2,11 +2,11 @@ package tests import ( "testing" - "github.com/stretchr/testify/require" + assert "github.com/stretchr/testify/require" ) func TestLogin(t *testing.T) { - assert.Equal(true, false) + assert.Equal(t, true, false) } func TestMain(m *testing.M) {