aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 10815a598feb38621e37d13dcbff7224a0aa484e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main

import (
	"crypto/subtle"
	"flag"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

type App struct {
	Store
	Template *template.Template
	Domain   string
	Header   string
}

func logMux(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		t := time.Now()
		handler.ServeHTTP(w, r)
		log.Println(r.Method, r.URL.Path, time.Since(t))
	})
}

func (app *App) validate(r *http.Request) (*Session, bool) {
	c, err := r.Cookie("id")
	if err != nil {
		return nil, false
	} else if s, ok := app.GetSession(c.Value); ok {
		return s, true
	} else {
		return nil, false
	}
}

func (app *App) validateHandler(w http.ResponseWriter, r *http.Request) {
	if s, ok := app.validate(r); ok {
		w.Header().Set(app.Header, strconv.FormatInt(s.UserId, 10))
		w.WriteHeader(http.StatusOK)
	} else {
		w.WriteHeader(http.StatusUnauthorized)
	}
}

func (app *App) rootHandler(w http.ResponseWriter, r *http.Request) {
	if s, ok := app.validate(r); ok {
		app.Template.ExecuteTemplate(w, "index.tmpl", s)
	} else {
		http.Redirect(w, r, "/login", http.StatusSeeOther)
	}
}

func (app *App) loginHandler(w http.ResponseWriter, r *http.Request) {
	if _, ok := app.validate(r); ok {
		http.Redirect(w, r, "/", http.StatusSeeOther)
	} else if r.Method == http.MethodPost {
		username := r.FormValue("username")
		hash := md5hex([]byte(r.FormValue("password")))
		next := r.FormValue("next")
		u, ok := app.GetUser(username)
		if ok && subtle.ConstantTimeCompare(u.Password, hash) == 1 {
			s := app.NewSession(u.Id)
			c := http.Cookie{
				Name:   "id",
				Value:  s.Id,
				Domain: "." + app.Domain,
			}
			http.SetCookie(w, &c)
			http.Redirect(w, r, next, http.StatusSeeOther)
		} else {
			var flash string
			if !ok {
				flash = "Utilisateur non enregistré"
			} else if subtle.ConstantTimeCompare(u.Password, hash) != 1 {
				flash = "Mot de passe incorrect"
			}
			app.Template.ExecuteTemplate(w, "login.tmpl", struct {
				Next  string
				Flash string
			}{next, flash})
		}
	} else if r.Method == http.MethodGet {
		next := r.FormValue("next")
		app.Template.ExecuteTemplate(w, "login.tmpl", struct {
			Next  string
			Flash string
		}{next, ""})
	}
}

type Flash struct {
	Type  string
	Value string
}

func (app *App) passwordHandler(w http.ResponseWriter, r *http.Request) {
	if s, ok := app.validate(r); !ok {
		http.Redirect(w, r, "/login", http.StatusSeeOther)
		return
	} else if r.Method == http.MethodGet {
		app.Template.ExecuteTemplate(w, "password.tmpl", Flash{})
	} else if r.Method == http.MethodPost {
		password := r.FormValue("password")
		confirm := r.FormValue("confirm")
		if password != "" && password == confirm {
			hash := md5hex([]byte(password))
			app.ChangePassword(s.UserId, hash)
			app.Template.ExecuteTemplate(w, "password.tmpl", Flash{
				"success",
				"Mot de passe enregistré",
			})
		} else {
			var bad string
			if password != confirm {
				bad = "Les deux mots de passe ne coïncident pas"
			} else if password == "" {
				bad = "Le mot de passe est vide"
			}
			app.Template.ExecuteTemplate(w, "password.tmpl", Flash{
				"danger",
				bad,
			})
		}
	}
}

func (app *App) logoutHandler(w http.ResponseWriter, r *http.Request) {
	if s, ok := app.validate(r); ok {
		// should we save old sessions in another table?
		app.DeleteSession(s.Id)
		c := http.Cookie{
			Name:   "id",
			Value:  "",
			Domain: "." + app.Domain,
			MaxAge: -1,
		}
		http.SetCookie(w, &c)
	}
	http.Redirect(w, r, "/login", http.StatusSeeOther)
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(
			flag.CommandLine.Output(),
			"Usage: %s [OPTIONS] [DATABASE] [DOMAIN]\n",
			os.Args[0],
		)
		flag.PrintDefaults()
	}
	address := flag.String("a", ":8080", "bind the server to `address`")
	templateDir := flag.String("t", "templates", "template `directory`")
	header := flag.String(
		"header",
		"X-Remote-User",
		"http `header` containing the user id",
	)
	flag.Parse()
	if flag.NArg() < 2 {
		fmt.Print("Argument missing. ")
		flag.Usage()
		os.Exit(1)
	}

	store := NewPgStore(flag.Args()[0])
	template := template.Must(
		template.New("").ParseGlob(filepath.Join(*templateDir, "*.tmpl")),
	)
	app := &App{store, template, flag.Args()[1], *header}
	http.HandleFunc("/validate", app.validateHandler)
	http.HandleFunc("/login", app.loginHandler)
	http.HandleFunc("/logout", app.logoutHandler)
	http.HandleFunc("/", app.rootHandler)
	http.HandleFunc("/password", app.passwordHandler)
	if err := http.ListenAndServe(*address, logMux(http.DefaultServeMux)); err != nil {
		panic(err)
	}
}