User
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Kekskurse 2021-11-23 20:21:43 +01:00
parent 16292a3b74
commit 82441fc9d6
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4
4 changed files with 42 additions and 17 deletions

View file

@ -30,8 +30,10 @@ func main() {
d := gomail.NewDialer("localhost", 1025, "from@gmail.com", "email_password") d := gomail.NewDialer("localhost", 1025, "from@gmail.com", "email_password")
d.TLSConfig = &tls.Config{InsecureSkipVerify: true} d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
userconfig := user.NewUserConfig()
sample.Register(router) sample.Register(router)
user.Register(router, tokenAuth, uc, d) user.Register(router, tokenAuth, uc, d, userconfig)
}, },
} }

View file

@ -1,5 +1,5 @@
Hallo {{ .username }},<br><br> Hallo {{ .username }},<br><br>
um deine E-Mail addresse zu bestätigen klicke auf folgenden link: {{ .url }}<br/><br/> um deine E-Mail addresse zu bestätigen klicke auf folgenden link: <a href="{{ .url }}">LINK</a><br/><br/>
Kthxbye,<br/> Kthxbye,<br/>
system system

View file

@ -25,8 +25,31 @@ var tokenAuth *jwtauth.JWTAuth
var d *gomail.Dialer var d *gomail.Dialer
func Register(router chi.Router, token *jwtauth.JWTAuth, userClient UserClient, dialer *gomail.Dialer) { type UserConfig struct {
RegisterMail *gomail.Message
URLValidationToken string
}
var userconfig UserConfig
func NewUserConfig() UserConfig {
uc := UserConfig{}
m := gomail.NewMessage()
m.SetHeader("From", "test@keks.cloud")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Activate your Account")
uc.RegisterMail = m
uc.URLValidationToken = "http://localhost/validate/%s"
return uc
}
func Register(router chi.Router, token *jwtauth.JWTAuth, userClient UserClient, dialer *gomail.Dialer, config UserConfig) {
uc = userClient uc = userClient
userconfig = config
ren = render.New(render.Options{ ren = render.New(render.Options{
//Layout: "layout", //Layout: "layout",
FileSystem: &render.EmbedFileSystem{ FileSystem: &render.EmbedFileSystem{
@ -111,23 +134,21 @@ func register(w http.ResponseWriter, r *http.Request) {
buf: &bytes.Buffer{}, buf: &bytes.Buffer{},
} }
ren.HTML(rw, http.StatusOK, "token", map[string]string{"username": r.FormValue("username"), "url": "http://www.google.de"}) token, err := uc.getMailValidationToken(r.FormValue("email"), true)
if err != nil {
panic(err)
}
ren.HTML(rw, http.StatusOK, "token", map[string]string{"username": r.FormValue("username"), "url": fmt.Sprintf(userconfig.URLValidationToken, token)})
content, err := io.ReadAll(rw.buf) content, err := io.ReadAll(rw.buf)
if err != nil { if err != nil {
panic(err) panic(err)
} }
m := gomail.NewMessage() mail := *userconfig.RegisterMail
m.SetHeader("From", "test@keks.cloud") mail.SetBody("text/html", string(content))
m.SetHeader("To", "to@example.com") if err := d.DialAndSend(&mail); err != nil {
m.SetHeader("Subject", "Gomail test subject")
m.SetBody("text/plain", "This is Gomail test body")
m.SetBody("text/html", string(content))
if err := d.DialAndSend(m); err != nil {
fmt.Println(err) fmt.Println(err)
panic(err) panic(err)
} }

View file

@ -1,6 +1,7 @@
package user package user
import ( import (
"encoding/base64"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
@ -9,7 +10,7 @@ import (
type UserClient interface { type UserClient interface {
register(username, password, email string) (bool, error) register(username, password, email string) (bool, error)
login(username, password string, requiredMailValidation bool) (bool, error) login(username, password string, requiredMailValidation bool) (bool, error)
getMailValidationToken(forceRecreate bool) (string, error) getMailValidationToken(email string, forceRecreate bool,) (string, error)
} }
type UserClientMemory struct { type UserClientMemory struct {
@ -54,8 +55,9 @@ func (uc UserClientMemory) login(username, password string, requiredMailValidati
return false, nil return false, nil
} }
func (uc UserClientMemory) getMailValidationToken(forceRecreate bool) (string, error) { func (uc UserClientMemory) getMailValidationToken(email string, forceRecreate bool) (string, error) {
token := randomString(5) token := randomString(35)
token = fmt.Sprintf("%v/%v", base64.StdEncoding.EncodeToString([]byte(email)), token)
return token, nil return token, nil
} }