refactoring
This commit is contained in:
21
src/login.go
21
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)
|
||||
}
|
||||
|
||||
30
src/main.go
30
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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user