-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
221 lines (193 loc) · 5.39 KB
/
main.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
"github.com/milescrabill/mirror-server/config"
"github.com/plaid/plaid-go/plaid"
"github.com/urfave/negroni"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
)
var (
conf config.Config
pc *plaid.Client
s *securecookie.SecureCookie
)
func makeSecureCookie(unencrypted map[string]string) *http.Cookie {
encoded, err := s.Encode("plaidToken", unencrypted)
if err != nil {
log.Printf("[error] plaid: couldn't encode cookie %s", err)
return nil
}
return &http.Cookie{
Name: "plaidToken",
Value: encoded,
// lasts a year, arbitrary
Expires: time.Now().Add(time.Hour * 24 * 365),
}
}
func readSecureCookie(r *http.Request, name string) map[string]string {
if cookie, err := r.Cookie(name); err == nil {
value := make(map[string]string)
if err = s.Decode(name, cookie.Value, &value); err == nil {
return value
} else {
log.Printf("readSecureCookie Decode error: %s", err.Error())
}
}
return nil
}
func getPlaidData(token string) ([]byte, error) {
postRes, err := pc.ExchangeToken(token)
if err != nil {
log.Printf("[error] plaid ExchangeToken: %s", err)
return []byte{}, err
}
// Use the returned Plaid API access_token to retrieve
// account information.
oneWeekAgo := time.Now().AddDate(0, 0, -7).Format("01-02-2006")
connectRes, mfaRes, err := pc.ConnectGet(postRes.AccessToken, &plaid.ConnectGetOptions{
GTE: oneWeekAgo,
})
if err != nil {
log.Printf("[error] plaid ConnectGet: %s", err.Error())
return []byte{}, err
}
if mfaRes != nil {
log.Printf("[info] plaid MFA response: %v", mfaRes)
}
categories := make(map[string]int)
for _, transaction := range connectRes.Transactions {
// for _, category := range transaction.Category {
// categories[category]++
// }
// one category per transaction
if len(transaction.Category) > 0 {
categories[transaction.Category[0]]++
}
}
js, err := json.Marshal(struct {
Categories map[string]int
Transactions []plaid.Transaction
Authorized bool
}{
categories,
connectRes.Transactions,
true,
})
if err != nil {
log.Printf("[error] plaid json.Marshal: fail!")
return []byte{}, err
}
return js, nil
}
func PlaidHandler(w http.ResponseWriter, req *http.Request) {
tok := struct {
Token string
}{}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("[error] plaid ReadAll: couldn't read request body: %s", err)
return
}
if string(body) == "" {
log.Printf("[warning] plaid: request body is empty")
return
}
err = json.Unmarshal(body, &tok)
if err != nil {
log.Printf("[error] plaid Unmarshal: couldn't decode json request body %s: %s", body, err)
w.WriteHeader(http.StatusBadRequest)
return
}
val := map[string]string{
"plaidToken": tok.Token,
}
// nil cookie
if cookie := makeSecureCookie(val); cookie == nil {
log.Printf("[error] could not make secure cookie")
w.WriteHeader(http.StatusInternalServerError)
} else {
http.SetCookie(w, cookie)
}
js, err := getPlaidData(tok.Token)
if err != nil {
log.Printf("[error] could not get plaid data")
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func BudgetHandler(w http.ResponseWriter, req *http.Request) {
// for _, cookie := range req.Cookies() {
// log.Printf("%s: %s", cookie.Name, cookie.Value)
// }
cookie := readSecureCookie(req, "plaidToken")
if cookie != nil {
if _, ok := cookie["plaidToken"]; ok {
js, err := getPlaidData(cookie["plaidToken"])
if err != nil {
log.Printf("[error] could not get plaid data")
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
return
}
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"auth": "none"}`))
}
func WeatherHandler(w http.ResponseWriter, req *http.Request) {
log.Printf("[info] weather: got request to %q", req.RequestURI)
resp, err := http.Get("https://api.forecast.io/forecast/" + conf.ForecastToken + "/45.4641,-122.6462")
if err != nil {
log.Fatal(err.Error())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err.Error())
}
w.Write(body)
}
func NewsHandler(w http.ResponseWriter, req *http.Request) {
log.Printf("[info] news: got request to %q", req.RequestURI)
resp, err := http.Get("https://newsapi.org/v1/articles?source=associated-press&sortBy=top&apiKey=" + conf.NewsToken)
if err != nil {
log.Fatal(err.Error())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err.Error())
}
w.Write(body)
}
func main() {
conf = config.GetConfig()
if conf.Port == 0 {
conf.Port = 8000
}
s = securecookie.New(
[]byte(conf.SecureCookieHashKey),
[]byte(conf.SecureCookieBlockKey),
)
pc = plaid.NewClient(conf.PlaidClientID, conf.PlaidSecret, plaid.Tartan)
r := mux.NewRouter()
r.HandleFunc("/weather", WeatherHandler)
r.HandleFunc("/news", NewsHandler)
r.HandleFunc("/budget", BudgetHandler)
r.HandleFunc("/plaid_auth", PlaidHandler).Methods("POST")
n := negroni.Classic() // Includes some default middlewares
n.UseHandler(handlers.CORS(
handlers.AllowCredentials(),
handlers.AllowedHeaders([]string{"Content-Type"}),
handlers.AllowedMethods([]string{"POST", "GET", "HEAD", "OPTIONS"}),
handlers.AllowedOrigins([]string{"http://localhost:4000"}),
)(r))
n.Run(":" + strconv.Itoa(conf.Port))
}