diff --git a/.vscode/launch.json b/.vscode/launch.json index 0f8103e..797be4e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,8 @@ "type": "go", "request": "launch", "mode": "auto", - "program": "${workspaceFolder}" + "program": "${workspaceFolder}", + "args": ["-c", "./config_example/config.yaml", "-o", "./log.txt"] } ] } \ No newline at end of file diff --git a/db_init.sql b/db_init.sql index d17d1cd..6811da1 100644 --- a/db_init.sql +++ b/db_init.sql @@ -1,6 +1,6 @@ create table users ( id int generated always as identity, - login text unique not null, + email text unique not null, secret text not null, name text not null, diff --git a/go.mod b/go.mod index ef9cc86..39a6425 100644 --- a/go.mod +++ b/go.mod @@ -49,5 +49,7 @@ require ( golang.org/x/sys v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect ) diff --git a/go.sum b/go.sum index 8826889..2dbc55c 100644 --- a/go.sum +++ b/go.sum @@ -122,11 +122,15 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index e7a4205..1ed5c95 100644 --- a/main.go +++ b/main.go @@ -67,7 +67,7 @@ func main() { logger.Fatal().Err(err).Msg("failed parsing postgres connection string") } - sqlDb := stdlib.OpenDB(connConf) + sqlDb = stdlib.OpenDB(connConf) if err := sqlDb.Ping(); err != nil { logger.Fatal().Err(err).Msg("failed pinging postgres db") } @@ -77,13 +77,17 @@ func main() { passwordUtil := utils.NewPasswordUtil() userRepo := repo.NewUserRepo(sqlDb) userCache := repo.NewCacheInmem[string, models.UserDTO](60 * 60) + emailRepo := repo.NewEmailRepo() + actionTokenRepo := repo.NewActionTokenRepo(sqlDb) userService := services.NewUserService( services.UserServiceDeps{ - Jwt: jwtUtil, - Password: passwordUtil, - UserRepo: userRepo, - UserCache: userCache, + Jwt: jwtUtil, + Password: passwordUtil, + UserRepo: userRepo, + UserCache: userCache, + EmailRepo: emailRepo, + ActionTokenRepo: actionTokenRepo, }, ) diff --git a/src/repo/email_repo.go b/src/repo/email_repo.go index 3b8c256..9dd6733 100644 --- a/src/repo/email_repo.go +++ b/src/repo/email_repo.go @@ -1,5 +1,48 @@ package repo +import ( + "strings" + + "gopkg.in/gomail.v2" +) + +const MSG_TEXT = ` + +
+ + +This message was sent because you forgot a password
+ + + +` + type EmailRepo interface { SendEmailForgotPassword(email, token string) } + +func NewEmailRepo() EmailRepo { + return &emailRepo{} +} + +type emailRepo struct { + // mail *gomail.Dialer +} + +func (e *emailRepo) SendEmailForgotPassword(email, token string) { + link := "https://nucrea.ru?token=" + token + msgText := strings.ReplaceAll(MSG_TEXT, "{{Link}}", link) + + m := gomail.NewMessage() + m.SetHeader("From", "email") + m.SetHeader("To", email) + m.SetHeader("Subject", "Hello!") + m.SetBody("text/html", msgText) + + d := gomail.NewDialer("smtp.yandex.ru", 587, "login", "password") + + // Send the email to Bob, Cora and Dan. + if err := d.DialAndSend(m); err != nil { + panic(err) + } +} diff --git a/src/services/user_service.go b/src/services/user_service.go index d733844..f8e8433 100644 --- a/src/services/user_service.go +++ b/src/services/user_service.go @@ -107,7 +107,7 @@ func (u *userService) AuthenticateUser(ctx context.Context, email, password stri } func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) error { - user, err := u.deps.UserRepo.GetUserById(ctx, userId) + user, err := u.getUserById(ctx, userId) if err != nil { return err } @@ -129,7 +129,7 @@ func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) err } func (u *userService) ChangePasswordForgot(ctx context.Context, userId, newPassword, accessCode string) error { - user, err := u.deps.UserRepo.GetUserById(ctx, userId) + user, err := u.getUserById(ctx, userId) if err != nil { return err }