-
Notifications
You must be signed in to change notification settings - Fork 67
/
authentication.go
53 lines (42 loc) · 1.08 KB
/
authentication.go
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
package uiserver
import (
"net/http"
)
const (
sessioncookiename = "postee-session-cookie"
)
func (srv *uiServer) login(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
session, err := srv.store.Get(r, sessioncookiename)
if err != nil {
session, err = srv.store.New(r, sessioncookiename)
}
if session.Values["user"] == nil {
frmusr := r.FormValue("username")
frmpwd := r.FormValue("password")
if frmusr == srv.admusr && frmpwd == srv.admpwd {
session.Values["user"] = frmusr
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
} else {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}
}
}
func (srv *uiServer) logout(w http.ResponseWriter, r *http.Request) {
session, err := srv.store.Get(r, sessioncookiename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values["user"] = ""
session.Options.MaxAge = -1
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}