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
|
var sessions hashmap.HashMap
|
||||||
const sessionName string = "session"
|
const sessionName string = "session"
|
||||||
|
const sessionTimeout time.Duration = 10 * 24 * time.Hour
|
||||||
func login(w http.ResponseWriter, r *http.Request) {
|
func login(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
|
||||||
loginStruct := loginStruct{}
|
loginStruct := loginStruct{}
|
||||||
var login bool = false
|
var login bool = false
|
||||||
if loggedIn(r) {
|
if loggedIn(r) {
|
||||||
@ -32,22 +32,18 @@ func login(w http.ResponseWriter, r *http.Request) {
|
|||||||
cookie := http.Cookie{
|
cookie := http.Cookie{
|
||||||
Name: sessionName,
|
Name: sessionName,
|
||||||
Value: key,
|
Value: key,
|
||||||
Expires: time.Now().Add(10 * 24 * time.Hour),
|
Expires: time.Now().Add(sessionTimeout),
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
Secure: true,
|
Secure: true,
|
||||||
}
|
}
|
||||||
http.SetCookie(w, &cookie)
|
http.SetCookie(w, &cookie)
|
||||||
sessions.Set(key, username)
|
sessions.Set(key, username)
|
||||||
|
go deleteSession(key)
|
||||||
http.Redirect(w, r, "dash", http.StatusSeeOther)
|
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")
|
if r.Method == http.MethodGet || !login {
|
||||||
loginTmpl.Execute(w, loginStruct)
|
runTemplate(w, loginTmpl, loginStruct)
|
||||||
log(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,3 +55,8 @@ func loggedIn(r *http.Request) bool {
|
|||||||
_, valid := sessions.GetStringKey(key.Value)
|
_, valid := sessions.GetStringKey(key.Value)
|
||||||
return valid
|
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 registerTmpl *template.Template
|
||||||
var submitTmpl *template.Template
|
var submitTmpl *template.Template
|
||||||
var loginTmpl *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 {
|
type secrets_json struct {
|
||||||
DiscordToken string `json:"discordToken"`
|
DiscordToken string `json:"discordToken"`
|
||||||
MysqlIndentify string `json:"mysqlIndentify"`
|
MysqlIndentify string `json:"mysqlIndentify"`
|
||||||
@ -118,3 +93,8 @@ func log(err error) {
|
|||||||
func hashFunc(password []byte, salt []byte) []byte {
|
func hashFunc(password []byte, salt []byte) []byte {
|
||||||
return argon2.IDKey(password, salt, 1, 64*1024, 4, 32)
|
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"
|
"crypto/rand"
|
||||||
"github.com/cornelk/hashmap"
|
"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 cacheAccounts hashmap.HashMap
|
||||||
var rusername *regexp.Regexp
|
var rusername *regexp.Regexp
|
||||||
var remail *regexp2.Regexp
|
var remail *regexp2.Regexp
|
||||||
var rpassword *regexp2.Regexp
|
var rpassword *regexp2.Regexp
|
||||||
func register(w http.ResponseWriter, r *http.Request) {
|
func register(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
|
||||||
registerstruct := registertmpl{}
|
registerstruct := registertmpl{}
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
var newAccount account
|
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)
|
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)
|
cacheAccounts.Set(token, newAccount)
|
||||||
}
|
}
|
||||||
registerReturn: err = registerTmpl.Execute(w, registerstruct)
|
registerReturn: runTemplate(w, registerTmpl, registerstruct)
|
||||||
log(err)
|
|
||||||
}
|
}
|
||||||
func submit(w http.ResponseWriter, r *http.Request) {
|
func submit(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
@ -76,7 +99,7 @@ func register(w http.ResponseWriter, r *http.Request) {
|
|||||||
var account account = accInter.(account)
|
var account account = accInter.(account)
|
||||||
cacheAccounts.Del(token)
|
cacheAccounts.Del(token)
|
||||||
salt := make([]byte, 32)
|
salt := make([]byte, 32)
|
||||||
_, err := rand.Read(salt)
|
_, err = rand.Read(salt)
|
||||||
log(err)
|
log(err)
|
||||||
hash := hashFunc([]byte(account.password), salt)
|
hash := hashFunc([]byte(account.password), salt)
|
||||||
// add user to the database
|
// add user to the database
|
||||||
@ -103,8 +126,7 @@ func register(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
submitReturn: err = submitTmpl.Execute(w, submitStruct)
|
submitReturn: runTemplate(w, submitTmpl, submitStruct)
|
||||||
log(err)
|
|
||||||
}
|
}
|
||||||
func getRbuMember(user string, tag string) (*discordgo.Member, bool) {
|
func getRbuMember(user string, tag string) (*discordgo.Member, bool) {
|
||||||
allUsers, err := discord.GuildMembers(secret.DiscordServerID, "0", 1000)
|
allUsers, err := discord.GuildMembers(secret.DiscordServerID, "0", 1000)
|
||||||
|
|||||||
Reference in New Issue
Block a user