Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/user/login.go (83 lines, 1.7 KiB) -rw-r--r-- file download

ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
0
// Copyright (C) 2023, Jakob Wakeling
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
1
// All rights reserved.
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
2
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
3
package user
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
4
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
5
import (
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
6
	"bytes"
b804701 Jakob Wakeling 2023-11-27 23:52:28
7
	"html/template"
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
8
	"log"
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
9
	"net/http"
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
10
	"time"
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
11
945fd71 Jakob Wakeling 2023-11-05 15:39:31
12
	"github.com/Jamozed/Goit/src/goit"
b804701 Jakob Wakeling 2023-11-27 23:52:28
13
	"github.com/gorilla/csrf"
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
14
)
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
15
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
16
func HandleLogin(w http.ResponseWriter, r *http.Request) {
473d02c Jakob Wakeling 2023-11-28 00:01:06
17
	auth, _, err := goit.Auth(w, r, true)
473d02c Jakob Wakeling 2023-11-28 00:01:06
18
	if err != nil {
473d02c Jakob Wakeling 2023-11-28 00:01:06
19
		log.Println("[admin]", err.Error())
473d02c Jakob Wakeling 2023-11-28 00:01:06
20
		goit.HttpError(w, http.StatusInternalServerError)
473d02c Jakob Wakeling 2023-11-28 00:01:06
21
	}
473d02c Jakob Wakeling 2023-11-28 00:01:06
22
473d02c Jakob Wakeling 2023-11-28 00:01:06
23
	if auth {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
24
		http.Redirect(w, r, "/", http.StatusFound)
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
25
	}
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
26
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
27
	data := struct {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
28
		Title, Message, Name string
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
29
		FocusPw              bool
b804701 Jakob Wakeling 2023-11-27 23:52:28
30
b804701 Jakob Wakeling 2023-11-27 23:52:28
31
		CsrfField template.HTML
b804701 Jakob Wakeling 2023-11-27 23:52:28
32
	}{
b804701 Jakob Wakeling 2023-11-27 23:52:28
33
		Title: "Login",
b804701 Jakob Wakeling 2023-11-27 23:52:28
34
b804701 Jakob Wakeling 2023-11-27 23:52:28
35
		CsrfField: csrf.TemplateField(r),
b804701 Jakob Wakeling 2023-11-27 23:52:28
36
	}
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
37
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
38
	if r.Method == http.MethodPost {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
39
		data.Name = r.FormValue("username")
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
40
		password := r.FormValue("password")
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
41
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
42
		if data.Name == "" {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
43
			data.Message = "Username cannot be empty"
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
44
			goto execute
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
45
		}
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
46
e530f2c Jakob Wakeling 2023-12-17 22:28:16
47
		ip := goit.Ip(r)
4cb95cb Jakob Wakeling 2023-12-17 20:17:40
48
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
49
		user, err := goit.GetUserByName(data.Name)
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
50
		if err != nil {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
51
			log.Println("[/user/login]", err.Error())
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
52
			goit.HttpError(w, http.StatusInternalServerError)
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
53
			return
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
54
		} else if user == nil || !bytes.Equal(goit.Hash(password, user.Salt), user.Pass) {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
55
			data.Message = "Invalid credentials"
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
56
			data.FocusPw = true
4cb95cb Jakob Wakeling 2023-12-17 20:17:40
57
4cb95cb Jakob Wakeling 2023-12-17 20:17:40
58
			log.Println("[login] login attempt with", data.Name, "from", ip)
4cb95cb Jakob Wakeling 2023-12-17 20:17:40
59
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
60
			goto execute
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
61
		}
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
62
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
63
		sess, err := goit.NewSession(user.Id, ip, time.Now().Add(2*24*time.Hour))
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
64
		if err != nil {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
65
			log.Println("[/user/login]", err.Error())
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
66
			goit.HttpError(w, http.StatusInternalServerError)
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
67
			return
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
68
		}
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
69
570144e Jakob Wakeling 2023-12-15 23:28:06
70
		log.Println("[login]", user.Name, "logged in from", ip)
570144e Jakob Wakeling 2023-12-15 23:28:06
71
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
72
		goit.SetSessionCookie(w, user.Id, sess)
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
73
		http.Redirect(w, r, "/", http.StatusFound)
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
74
		return
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
75
	}
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
76
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
77
execute:
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
78
	if err := goit.Tmpl.ExecuteTemplate(w, "user/login", data); err != nil {
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
79
		log.Println("[/user/login]", err.Error())
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
80
	}
ff9ec25 Jakob Wakeling 2023-08-08 22:43:43
81
}
82