-
Notifications
You must be signed in to change notification settings - Fork 20
/
account.go
213 lines (185 loc) · 5.71 KB
/
account.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
// Copyright Banrai LLC. All rights reserved. Use of this source code is
// governed by the license that can be found in the LICENSE file.
// Package ui provides http request handlers for the Pi client WebApp
package ui
import (
"encoding/json"
"github.com/Banrai/PiScan/client/database"
"github.com/Banrai/PiScan/server/api"
"github.com/Banrai/PiScan/server/digest"
"html/template"
"net/http"
"net/url"
"strconv"
"strings"
)
var (
ACCOUNT_EDIT_TEMPLATE_FILES = []string{"account.html", "head.html", "navigation_tabs.html", "modal.html", "scripts.html"}
ACCOUNT_EDIT_TEMPLATES *template.Template
)
type AccountForm struct {
Title string
ActiveTab *ActiveTab
Account *database.Account
CancelUrl string
FormError string
Unregistered bool
}
/* HTML Response Functions (via templates) */
func renderAccountEditTemplate(w http.ResponseWriter, a *AccountForm) {
if TEMPLATES_INITIALIZED {
ACCOUNT_EDIT_TEMPLATES.Execute(w, a)
}
}
// EditAccount presents the form for editing Account information (in
// response to a GET request) and handles to add/updates (in response to
// a POST request)
func EditAccount(w http.ResponseWriter, r *http.Request, dbCoords database.ConnCoordinates, opts ...interface{}) {
// attempt to connect to the db
db, err := database.InitializeDB(dbCoords)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer db.Close()
// get the Account for this request
acc, accErr := database.GetDesignatedAccount(db)
if accErr != nil {
http.Error(w, accErr.Error(), http.StatusInternalServerError)
return
}
// get the api server + port from the optional parameters
apiHost, apiHostOk := opts[0].(string)
if !apiHostOk {
http.Error(w, BAD_REQUEST, http.StatusInternalServerError)
return
}
// prepare the html page response
regStatus := (acc.Email == database.ANONYMOUS_EMAIL)
cancelUrl := HOME_URL
if !regStatus {
cancelUrl = ACCOUNT_URL
}
form := &AccountForm{Title: "My Account",
ActiveTab: &ActiveTab{Scanned: false, Favorites: false, Account: true, ShowTabs: true},
Account: acc,
CancelUrl: cancelUrl,
Unregistered: regStatus}
if "POST" == r.Method {
form.FormError = BAD_POST // in event of problems
// get the item id from the posted data
r.ParseForm()
accVal, accExists := r.PostForm["account"]
emailVal, emailExists := r.PostForm["accountEmail"]
if accExists && emailExists {
// make sure the hidden account id value matches the Account
accId, accIdErr := strconv.ParseInt(accVal[0], 10, 64)
if accIdErr != nil {
form.FormError = accIdErr.Error()
} else {
if acc.Id == accId {
// update the account email address in the local client db
updateErr := acc.Update(db, emailVal[0], acc.APICode)
if updateErr != nil {
form.FormError = updateErr.Error()
} else {
// ping the server with the api code and email for verification
ping := func() {
v := url.Values{}
v.Set("email", emailVal[0])
v.Set("api", acc.APICode)
// use the email address as the digest key
hmac := digest.GenerateDigest(emailVal[0], v.Encode())
v.Set("hmac", hmac)
res, err := http.Get(strings.Join([]string{apiHost, "/register?", v.Encode()}, ""))
if err == nil {
res.Body.Close()
}
}
go ping() // do not wait for the server to reply
// return success
http.Redirect(w, r, ACCOUNT_URL, http.StatusFound)
return
}
}
}
}
}
renderAccountEditTemplate(w, form)
}
// ConfirmServerAccount responds to the ajax request from the client to
// lookup and return the status of the given account
func ConfirmServerAccount(r *http.Request, dbCoords database.ConnCoordinates, opts ...interface{}) string {
// prepare the ajax reply object
ack := AjaxAck{Message: "", Error: ""}
// attempt to connect to the db
db, err := database.InitializeDB(dbCoords)
if err != nil {
ack.Error = err.Error()
}
defer db.Close()
// get the api server + port from the optional parameters
apiHost, apiHostOk := opts[0].(string)
if !apiHostOk {
ack.Error = BAD_REQUEST
}
if ack.Error == "" {
// get the Account for this request
acc, accErr := database.GetDesignatedAccount(db)
if accErr != nil {
ack.Error = accErr.Error()
}
// get the account from the POST values
if "POST" == r.Method {
r.ParseForm()
if accVal, exists := r.PostForm["account"]; exists {
if len(accVal) > 0 {
id, idErr := strconv.ParseInt(accVal[0], 10, 64)
if idErr != nil {
ack.Error = idErr.Error()
} else {
if acc.Id != id {
ack.Error = BAD_REQUEST
} else {
// prepare the API Server request
v := url.Values{}
v.Set("email", acc.Email)
// use the email address as the digest key
hmac := digest.GenerateDigest(acc.Email, v.Encode())
v.Set("hmac", hmac)
// ping the API Server for the status of this account
res, resErr := http.Get(strings.Join([]string{apiHost, "/status?", v.Encode()}, ""))
defer res.Body.Close()
if resErr != nil {
ack.Error = resErr.Error()
} else {
// read and parse the json message from the API Server
m := new(api.SimpleMessage)
dec := json.NewDecoder(res.Body)
dec.Decode(&m)
// assign the json ack accordingly
if m.Err != nil {
ack.Error = m.Err.Error()
} else {
ack.Message = m.Ack
}
}
}
}
} else {
ack.Error = "Missing account id"
}
} else {
ack.Error = BAD_POST
}
} else {
ack.Error = BAD_REQUEST
}
}
// convert the ajax reply object to json
ackObj, ackObjErr := json.Marshal(ack)
if ackObjErr != nil {
return ackObjErr.Error()
}
return string(ackObj)
}