-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhome_handler.go
More file actions
151 lines (128 loc) · 3.9 KB
/
Copy pathhome_handler.go
File metadata and controls
151 lines (128 loc) · 3.9 KB
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
package main
import (
"crypto/subtle"
"database/sql"
"errors"
"fmt"
"golang.org/x/crypto/ssh"
"html/template"
"net"
"net/http"
"path"
"time"
)
var (
ErrNotSignedIn = errors.New("not signed in")
ErrInvalidSession = errors.New("invalid session token")
)
type UserSession struct {
userId int
sessionId string
sessionSecret string
csrfToken string
}
type HomeHandler struct {
db *sql.DB
sshAdvertise string
assetsDir string
hostPubkey ssh.PublicKey
}
type HomeContext struct {
IntroPage bool
SigninPage bool
ThrowawayPage bool
FingerprintPage bool
SignedIn bool
UserId int
Fingerprint string
CSRFToken string
SSHHost string
SSHPort string
SSHPortNonStandard bool
HostFingerprint1 string
HostFingerprint2 string
}
func (hd *HomeHandler) ServeHTTP(resp http.ResponseWriter, request *http.Request) {
db := hd.db
t, err := template.ParseFiles(path.Join(hd.assetsDir, "index.html"))
if err != nil {
fmt.Println("parsing HTML template:", err)
http.Error(resp, "Internal server error", http.StatusInternalServerError)
return
}
session, err := sessionFromRequest(request, db)
if err == ErrInvalidSession {
fmt.Println(err)
clearSessionCookie(resp)
}
signedIn := err == nil
var pubkey []byte
var fingerprint string
if signedIn {
err = db.QueryRow("select pubkey from users where user_id = ?", session.userId).Scan(&pubkey)
if err != nil {
fmt.Println("retrieving pubkey:", err)
http.Error(resp, "Internal server error", http.StatusInternalServerError)
return
}
fingerprint = pubkeyFingerprintMD5(pubkey)
keepSessionAlive(resp, db, session)
}
hostFingerprint := pubkeyFingerprintMD5(hd.hostPubkey.Marshal())
var hostFingerprint1, hostFingerprint2 string
if len(hostFingerprint) == 47 {
hostFingerprint1 = hostFingerprint[0:23]
hostFingerprint2 = hostFingerprint[24:47]
}
sshHost, sshPort, err := net.SplitHostPort(hd.sshAdvertise)
if err != nil {
fmt.Println("splitting SSH host and port:", err)
http.Error(resp, "Internal server error", http.StatusInternalServerError)
return
}
path := request.URL.Path
context := HomeContext{
IntroPage: path == "/" && !signedIn,
SigninPage: path == "/signin",
ThrowawayPage: path == "/throwaway",
FingerprintPage: path == "/fingerprint",
SignedIn: signedIn,
UserId: session.userId,
Fingerprint: fingerprint,
CSRFToken: session.csrfToken,
SSHHost: sshHost,
SSHPort: sshPort,
SSHPortNonStandard: sshPort != "22",
HostFingerprint1: hostFingerprint1,
HostFingerprint2: hostFingerprint2,
}
t.Execute(resp, context)
}
func sessionFromRequest(request *http.Request, db *sql.DB) (UserSession, error) {
session := UserSession{}
cookie, err := request.Cookie("session")
if err == http.ErrNoCookie {
return session, ErrNotSignedIn
} else if err != nil {
return session, ErrInvalidSession
}
sessionToken := cookie.Value
if len(sessionToken) != sessionIdLength+sessionSecretLength {
return session, ErrInvalidSession
}
session.sessionId = sessionToken[:sessionIdLength]
providedSessionSecret := sessionToken[sessionIdLength:]
err = db.QueryRow("select user_id, session_secret, csrf_token from sessions where session_id = ?", session.sessionId).Scan(&session.userId, &session.sessionSecret, &session.csrfToken)
if err != nil {
return session, ErrInvalidSession
}
if subtle.ConstantTimeCompare([]byte(providedSessionSecret), []byte(session.sessionSecret)) != 1 {
return session, ErrInvalidSession
}
return session, nil
}
func keepSessionAlive(resp http.ResponseWriter, db *sql.DB, session UserSession) {
timestamp := time.Now().Unix()
db.Exec("update sessions set last_active = ? where session_id = ?", timestamp, session.sessionId)
setSessionCookie(resp, session.sessionId, session.sessionSecret)
}