This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
290 lines (277 loc) · 9.11 KB
/
accounts.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"encoding/json"
"errors"
"get.2cloud.org/twocloud"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
type tokenRequest struct {
Access string `json:"access"`
Refresh string `json:"refresh,omitempty"`
Expires time.Time `json:"expires,omitempty"`
}
type Credentials [2]string
func parseCallback(callback string) (*url.URL, error) {
if callback == "" {
return nil, errors.New("Callback must be specified.")
}
_, err := url.QueryUnescape(callback)
if err != nil {
return nil, errors.New("Bad callback formatting.")
}
cbURL, err := url.Parse(callback)
if err != nil {
return nil, errors.New("Invalid callback URL")
}
return cbURL, nil
}
func oauthRedirect(w http.ResponseWriter, r *twocloud.RequestBundle) {
callback := r.Request.URL.Query().Get("callback")
_, err := parseCallback(callback)
if err != nil {
Respond(w, r, http.StatusBadRequest, err.Error(), []interface{}{})
return
}
url := r.GetOAuthAuthURL(r.Config.OAuth.ClientID, r.Config.OAuth.ClientSecret, r.Config.OAuth.CallbackURL, callback)
http.Redirect(w, r.Request, url, http.StatusFound)
return
}
func oauthCallback(w http.ResponseWriter, r *twocloud.RequestBundle) {
code := r.Request.URL.Query().Get("code")
if code == "" {
Respond(w, r, http.StatusBadRequest, "No auth code specified.", []interface{}{})
return
}
state := r.Request.URL.Query().Get("state")
callback, err := parseCallback(state)
if err != nil {
Respond(w, r, http.StatusBadRequest, err.Error(), []interface{}{})
return
}
access, refresh, exp, err := r.GetOAuthAccessToken(code)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
account, err := r.GetAccount(access, refresh, exp)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
values := callback.Query()
if account.UserID != 0 {
user, err := r.GetUser(account.UserID)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Error while logging you in. We're looking into it.", []interface{}{})
return
}
values.Set("user", user.Username)
values.Set("secret", user.Secret)
} else {
values.Set("id", strconv.FormatUint(account.ID, 10))
values.Set("email", account.Email)
values.Set("givenName", account.GivenName)
values.Set("familyName", account.FamilyName)
}
callback.RawQuery = values.Encode()
http.Redirect(w, r.Request, callback.String(), http.StatusFound)
return
}
func oauthToken(w http.ResponseWriter, r *twocloud.RequestBundle) {
var tokens tokenRequest
body, err := ioutil.ReadAll(r.Request.Body)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
err = json.Unmarshal(body, &tokens)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusBadRequest, "Error decoding request.", []interface{}{})
return
}
if tokens.Access == "" {
Respond(w, r, http.StatusBadRequest, "access token must be supplied.", []interface{}{})
return
}
account, err := r.GetAccount(tokens.Access, tokens.Refresh, tokens.Expires)
if err != nil {
r.Log.Error(err.Error())
if err == twocloud.OAuthAuthError {
Respond(w, r, http.StatusUnauthorized, err.Error(), []interface{}{})
return
} else if oauthError, ok := err.(twocloud.OAuthError); ok {
Respond(w, r, http.StatusUnauthorized, oauthError.Error(), []interface{}{})
return
}
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
if account.UserID != 0 {
user, err := r.GetUser(account.UserID)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Error while logging you in. We're looking into it.", []interface{}{})
return
}
setLastModified(w, user.LastActive)
Respond(w, r, http.StatusOK, "Successfully authenticated the user", []interface{}{user})
return
}
Respond(w, r, http.StatusCreated, "Successfully created a new account", []interface{}{account})
setLastModified(w, account.Added)
return
}
func updateAccountTokens(w http.ResponseWriter, r *twocloud.RequestBundle) {
var tokens tokenRequest
accountID := r.Request.URL.Query().Get(":account")
if accountID == "" {
Respond(w, r, http.StatusBadRequest, "Must specify an account ID.", []interface{}{})
return
}
id, err := strconv.ParseUint(accountID, 10, 64)
if err != nil {
Respond(w, r, http.StatusBadRequest, "Invalid account ID.", []interface{}{})
return
}
account, err := r.GetAccountByID(id)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
if account.UserID != r.AuthUser.ID && !r.AuthUser.IsAdmin {
Respond(w, r, http.StatusForbidden, "You don't have access to that account.", []interface{}{})
return
}
body, err := ioutil.ReadAll(r.Request.Body)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
err = json.Unmarshal(body, &tokens)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusBadRequest, "Error decoding request.", []interface{}{})
return
}
if tokens.Access == "" {
Respond(w, r, http.StatusBadRequest, "access token must be supplied.", []interface{}{})
return
}
err = r.UpdateAccountTokens(account, tokens.Access, tokens.Refresh, tokens.Expires)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
Respond(w, r, http.StatusOK, "Successfully updated the account tokens", []interface{}{account})
return
}
func removeAccount(w http.ResponseWriter, r *twocloud.RequestBundle) {
accountID := r.Request.URL.Query().Get(":account")
if accountID == "" {
Respond(w, r, http.StatusBadRequest, "Must specify an account ID.", []interface{}{})
return
}
id, err := strconv.ParseUint(accountID, 10, 64)
if err != nil {
Respond(w, r, http.StatusBadRequest, "Invalid account ID.", []interface{}{})
return
}
account, err := r.GetAccountByID(id)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
if account.UserID != r.AuthUser.ID && !r.AuthUser.IsAdmin {
Respond(w, r, http.StatusForbidden, "You don't have access to that account.", []interface{}{})
return
}
err = r.DeleteAccount(account)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
Respond(w, r, http.StatusOK, "Successfully deleted the account", []interface{}{account})
return
}
func refreshAccount(w http.ResponseWriter, r *twocloud.RequestBundle) {
accountID := r.Request.URL.Query().Get(":account")
if accountID == "" {
Respond(w, r, http.StatusBadRequest, "Must specify an account ID.", []interface{}{})
return
}
id, err := strconv.ParseUint(accountID, 10, 64)
if err != nil {
Respond(w, r, http.StatusBadRequest, "Invalid account ID.", []interface{}{})
return
}
account, err := r.GetAccountByID(id)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
if account.UserID != r.AuthUser.ID && !r.AuthUser.IsAdmin {
Respond(w, r, http.StatusForbidden, "You don't have access to that account.", []interface{}{})
return
}
account, err = r.UpdateAccountData(account)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
Respond(w, r, http.StatusOK, "Successfully updated the account", []interface{}{account})
return
}
func generateTmpCredentials(w http.ResponseWriter, r *twocloud.RequestBundle) {
strs, err := r.CreateTempCredentials(r.AuthUser)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error.", []interface{}{})
return
}
creds := Credentials(strs)
Respond(w, r, http.StatusCreated, "Generated temporary credentials", []interface{}{creds})
return
}
func authTmpCredentials(w http.ResponseWriter, r *twocloud.RequestBundle) {
cred1 := r.Request.URL.Query().Get("cred1")
cred2 := r.Request.URL.Query().Get("cred2")
if cred1 == "" || cred2 == "" {
Respond(w, r, http.StatusBadRequest, "Both temporary credentials must be supplied", []interface{}{})
return
}
id, err := r.CheckTempCredentials(cred1, cred2)
if err == twocloud.InvalidCredentialsError {
Respond(w, r, http.StatusUnauthorized, "Invalid credentials", []interface{}{})
return
} else if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
user, err := r.GetUser(id)
if err != nil {
r.Log.Error(err.Error())
Respond(w, r, http.StatusInternalServerError, "Internal server error", []interface{}{})
return
}
Respond(w, r, http.StatusOK, "Successfully authenticated the user", []interface{}{user})
return
}
func auditAccount(w http.ResponseWriter, r *twocloud.RequestBundle) {
}