-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
224 lines (187 loc) · 4.92 KB
/
web.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"html/template"
"net/http"
"gitern/db"
"gitern/pubkey"
"github.com/dpapathanasiou/go-recaptcha"
"github.com/joho/godotenv"
)
const GITERN_ACCOUNTS = "/gitern/accounts/"
func isDev() bool {
return os.Getenv("ENV") == "DEV"
}
var funcMap = template.FuncMap{
"isDev": isDev,
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("no .env found ... using default env")
}
schema, err := ioutil.ReadFile("schema.sql")
if err != nil {
log.Fatalln(err)
}
_, err = db.Conn.Exec(string(schema))
if err != nil {
log.Fatalln(err)
}
port, exists := os.LookupEnv("PORT")
if !exists {
port = "5000"
}
http.Handle("/public/", http.StripPrefix("/public/",
http.FileServer(http.Dir("./public"))))
http.HandleFunc("/", index)
http.HandleFunc("/join", join)
http.HandleFunc("/account", account)
stripeHTTP()
log.Printf("starting gitern server on port %s\n\n", port)
log.Fatalln(http.ListenAndServe(":"+port, nil))
}
func session(w http.ResponseWriter, r *http.Request) {
// check if the ssh based session exists
row := db.Conn.QueryRow(`SELECT name, fingerprint,
NOW() - created_at > interval '1 hours' AS expired
FROM sessions
WHERE id = $1`, strings.TrimPrefix(r.URL.Path, "/"))
var name, fp string
var expired bool
if err := row.Scan(&name, &fp, &expired); err != nil {
errHandlerMsg(w, http.StatusUnauthorized, "invalid session")
return
}
db.Conn.Exec("SELECT delete_sessions($1, $2)", name, fp)
if expired {
errHandlerMsg(w, http.StatusUnauthorized, "expired session")
return
}
err := createSession(w, name, fp)
if err != nil {
errHandler(w, http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/account", http.StatusSeeOther)
}
func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
session(w, r)
return
}
tmpl := template.Must(template.New("index.tmpl").Funcs(funcMap).
ParseFiles("views/index.tmpl"))
tmpl.Execute(w, nil)
}
type tArgs map[string]interface{}
func pubkeySplit(pubkey string) (string, string, string) {
parts := strings.Split(pubkey, " ")
switch len(parts) {
case 0:
return "", "", ""
case 1:
return parts[0], "", ""
case 2:
return parts[0], parts[1], ""
default:
return parts[0], parts[1], parts[2]
}
}
func account(w http.ResponseWriter, r *http.Request) {
accname, fp, err := getSession(r)
if err != nil {
errHandler(w, http.StatusUnauthorized)
return
}
stripeID, stripeStatus, err := stripeIDAndStatus(accname)
if err != nil {
errHandlerMsg(w, http.StatusInternalServerError, "retrieving stripe status")
return
}
keyCount, _ := pubkey.Count(accname)
tmpl := template.Must(template.ParseFiles("views/account.tmpl"))
tmpl.Execute(w, tArgs{
"accname": accname,
"fp": fp,
"pubkeys": keyCount,
"StripeID": stripeID,
"StripeStatus": stripeStatus,
"STRIPE_PUBLISHABLE_KEY": os.Getenv("STRIPE_PUBLISHABLE_KEY"),
"STRIPE_PRICE_ID": os.Getenv("STRIPE_PRICE_ID"),
})
}
func join(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
log.Printf("ParseForm() err: %v", err)
errHandler(w, http.StatusInternalServerError)
return
}
chaKey := os.Getenv("RECAPTCHA_SECKEY")
if chaKey == "" {
log.Println("no recaptcha secret key")
errHandler(w, http.StatusInternalServerError)
return
}
recaptcha.Init(chaKey)
chaResp := r.FormValue("g-recaptcha-response")
result, err := recaptcha.Confirm(r.RemoteAddr, chaResp)
if err != nil {
log.Println("recaptcha confirmation error", err)
errHandler(w, http.StatusInternalServerError)
return
}
if !result && !isDev() {
errHandler(w, http.StatusBadRequest)
return
}
accname := r.FormValue("accname")
err = db.DoTxn(func(tx *sql.Tx) error {
_, err := tx.Exec("INSERT INTO accounts (name) VALUES ($1)", accname)
if err != nil {
return err
}
err = pubkey.AddLineTx(r.FormValue("pubkey"), accname, tx)
if err != nil {
return err
}
err = os.Mkdir(path.Join(GITERN_ACCOUNTS, accname), 0755)
if err != nil && !isDev() {
return err
}
return nil
})
if err != nil {
log.Printf("sign up err: %v", err)
errHandler(w, http.StatusBadRequest)
return
}
row := db.Conn.QueryRow(`SELECT pubkey_fingerprint
FROM accounts_pubkeys
WHERE account_name = $1`, accname)
var fp string
if err := row.Scan(&fp); err != nil {
log.Printf("row.Scan() err: %v", err)
errHandler(w, http.StatusInternalServerError)
return
}
err = createSession(w, accname, fp)
if err != nil {
errHandler(w, http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/account", http.StatusSeeOther)
}
func errHandler(w http.ResponseWriter, status int) {
w.WriteHeader(status)
fmt.Fprintf(w, "Error %d\n", status)
}
func errHandlerMsg(w http.ResponseWriter, status int, msg string) {
errHandler(w, status)
fmt.Fprintf(w, msg)
}