-
Notifications
You must be signed in to change notification settings - Fork 2
/
login.go
76 lines (69 loc) · 1.77 KB
/
login.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package samtunnelhandler
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Create a struct that models the structure of a user, both in the request body, and in the DB
type Credentials struct {
Password string `json:"password"`
Username string `json:"username"`
}
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func GenerateRandomString(s int) (string, error) {
b, err := GenerateRandomBytes(s)
return base64.URLEncoding.EncodeToString(b), err
}
func (m *TunnelHandlerMux) Signin(w http.ResponseWriter, r *http.Request) {
var creds Credentials
err := json.NewDecoder(r.Body).Decode(&creds)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if creds.Username != m.user {
w.WriteHeader(http.StatusUnauthorized)
return
}
if creds.Password != m.password {
w.WriteHeader(http.StatusUnauthorized)
return
}
m.sessionToken, err = GenerateRandomString(32)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Value: m.sessionToken,
Expires: time.Now().Add(10 * time.Minute),
})
}
func (m *TunnelHandlerMux) CSS(w http.ResponseWriter, r *http.Request) {
if m.CheckCookie(w, r) == false {
return
}
w.Header().Add("Content-Type", "text/css")
w.WriteHeader(http.StatusOK)
//w.Write([]byte(fmt.Sprintf("%s\n", m.cssString)))
w.Write([]byte(fmt.Sprintf("%s\n", DefaultCSS())))
}
func (m *TunnelHandlerMux) JS(w http.ResponseWriter, r *http.Request) {
if m.CheckCookie(w, r) == false {
return
}
w.WriteHeader(http.StatusOK)
//w.Write([]byte(fmt.Sprintf("%s\n", m.jsString)))
w.Write([]byte(fmt.Sprintf("%s\n", DefaultJS())))
}