From e557cfa25757b91943b677236ec8f1bb7005d28d Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Tue, 22 Dec 2020 12:44:16 +0100 Subject: [PATCH] refactoring --- src/login.go | 21 +++++++++++---------- src/main.go | 30 +++++------------------------- src/register.go | 34 ++++++++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/login.go b/src/login.go index 8a6f008..035d5ad 100644 --- a/src/login.go +++ b/src/login.go @@ -10,8 +10,8 @@ type loginStruct struct { } var sessions hashmap.HashMap const sessionName string = "session" +const sessionTimeout time.Duration = 10 * 24 * time.Hour func login(w http.ResponseWriter, r *http.Request) { - var err error loginStruct := loginStruct{} var login bool = false if loggedIn(r) { @@ -32,22 +32,18 @@ func login(w http.ResponseWriter, r *http.Request) { cookie := http.Cookie{ Name: sessionName, Value: key, - Expires: time.Now().Add(10 * 24 * time.Hour), + Expires: time.Now().Add(sessionTimeout), HttpOnly: true, Secure: true, } http.SetCookie(w, &cookie) sessions.Set(key, username) + go deleteSession(key) http.Redirect(w, r, "dash", http.StatusSeeOther) - } else { - w.Header().Set("Content-Type", "text/html") - err = loginTmpl.Execute(w, loginStruct) - log(err) } - } else { - w.Header().Set("Content-Type", "text/html") - loginTmpl.Execute(w, loginStruct) - log(err) + } + if r.Method == http.MethodGet || !login { + runTemplate(w, loginTmpl, loginStruct) } } @@ -59,3 +55,8 @@ func loggedIn(r *http.Request) bool { _, valid := sessions.GetStringKey(key.Value) return valid } + +func deleteSession(key string) { + time.Sleep(sessionTimeout) + sessions.Del(key) +} diff --git a/src/main.go b/src/main.go index 1369da9..176bf5b 100644 --- a/src/main.go +++ b/src/main.go @@ -24,31 +24,6 @@ var giteaClient *gitea.Client var registerTmpl *template.Template var submitTmpl *template.Template var loginTmpl *template.Template -type account struct { - email string - username string - password string - discordUsername string - discordTag string - discordId string -} -type WrongAccount struct { - User bool - Pass bool - Email bool - DiscordUser bool -} -type registertmpl struct { - Success bool - WrongAccount WrongAccount - AlreadyEsitsInDatabase struct{ - Username bool - DiscordUsername bool - } -} -type SubmitStruct struct { - Success bool -} type secrets_json struct { DiscordToken string `json:"discordToken"` MysqlIndentify string `json:"mysqlIndentify"` @@ -118,3 +93,8 @@ func log(err error) { func hashFunc(password []byte, salt []byte) []byte { return argon2.IDKey(password, salt, 1, 64*1024, 4, 32) } +func runTemplate(w http.ResponseWriter, template *template.Template, templateData interface{}) { + w.Header().Set("Content-Type", "text/html") + var err error = template.Execute(w, templateData) + log(err) +} diff --git a/src/register.go b/src/register.go index 8f06914..66803dd 100644 --- a/src/register.go +++ b/src/register.go @@ -12,12 +12,36 @@ import ( "crypto/rand" "github.com/cornelk/hashmap" ) +type account struct { + email string + username string + password string + discordUsername string + discordTag string + discordId string +} +type WrongAccount struct { + User bool + Pass bool + Email bool + DiscordUser bool +} +type registertmpl struct { + Success bool + WrongAccount WrongAccount + AlreadyEsitsInDatabase struct{ + Username bool + DiscordUsername bool + } +} +type SubmitStruct struct { + Success bool +} var cacheAccounts hashmap.HashMap var rusername *regexp.Regexp var remail *regexp2.Regexp var rpassword *regexp2.Regexp func register(w http.ResponseWriter, r *http.Request) { - var err error registerstruct := registertmpl{} if r.Method == http.MethodPost { var newAccount account @@ -60,8 +84,7 @@ func register(w http.ResponseWriter, r *http.Request) { discord.ChannelMessageSend(dmChannel.ID, "Bitte klicke auf den Link, um die Erstellung des Accounts abzuschließen.\nhttp://localhost:8080/submit?token=" + token) cacheAccounts.Set(token, newAccount) } - registerReturn: err = registerTmpl.Execute(w, registerstruct) - log(err) + registerReturn: runTemplate(w, registerTmpl, registerstruct) } func submit(w http.ResponseWriter, r *http.Request) { var err error @@ -76,7 +99,7 @@ func register(w http.ResponseWriter, r *http.Request) { var account account = accInter.(account) cacheAccounts.Del(token) salt := make([]byte, 32) - _, err := rand.Read(salt) + _, err = rand.Read(salt) log(err) hash := hashFunc([]byte(account.password), salt) // add user to the database @@ -103,8 +126,7 @@ func register(w http.ResponseWriter, r *http.Request) { } } - submitReturn: err = submitTmpl.Execute(w, submitStruct) - log(err) + submitReturn: runTemplate(w, submitTmpl, submitStruct) } func getRbuMember(user string, tag string) (*discordgo.Member, bool) { allUsers, err := discord.GuildMembers(secret.DiscordServerID, "0", 1000)