| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"flag" |
| 20 |
"fmt" |
| 21 |
"io/ioutil" |
| 22 |
"log" |
| 23 |
"net/http" |
| 24 |
"net/url" |
| 25 |
"text/template" |
| 26 |
|
| 27 |
"github.com/gomodule/oauth1/examples/session" |
| 28 |
"github.com/gomodule/oauth1/oauth" |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
const ( |
| 33 |
tempCredKey = "tempCred" |
| 34 |
tokenCredKey = "tokenCred" |
| 35 |
companyKey = "company" |
| 36 |
) |
| 37 |
|
| 38 |
var oauthClient = oauth.Client{ |
| 39 |
TemporaryCredentialRequestURI: "https://oauth.intuit.com/oauth/v1/get_request_token", |
| 40 |
ResourceOwnerAuthorizationURI: "https://appcenter.intuit.com/Connect/Begin", |
| 41 |
TokenRequestURI: "https://oauth.intuit.com/oauth/v1/get_access_token", |
| 42 |
Header: http.Header{"Accept": {"application/json"}}, |
| 43 |
} |
| 44 |
|
| 45 |
var credPath = flag.String("config", "config.json", "Path to configuration file containing the application's credentials.") |
| 46 |
|
| 47 |
func readCredentials() error { |
| 48 |
b, err := ioutil.ReadFile(*credPath) |
| 49 |
if err != nil { |
| 50 |
return err |
| 51 |
} |
| 52 |
return json.Unmarshal(b, &oauthClient.Credentials) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func serveLogin(w http.ResponseWriter, r *http.Request) { |
| 58 |
callback := "http://" + r.Host + "/callback" |
| 59 |
tempCred, err := oauthClient.RequestTemporaryCredentials(nil, callback, nil) |
| 60 |
if err != nil { |
| 61 |
http.Error(w, "Error getting temp cred, "+err.Error(), 500) |
| 62 |
return |
| 63 |
} |
| 64 |
s := session.Get(r) |
| 65 |
s[tempCredKey] = tempCred |
| 66 |
if err := session.Save(w, r, s); err != nil { |
| 67 |
http.Error(w, "Error saving session , "+err.Error(), 500) |
| 68 |
return |
| 69 |
} |
| 70 |
http.Redirect(w, r, oauthClient.AuthorizationURL(tempCred, nil), 302) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func serveOAuthCallback(w http.ResponseWriter, r *http.Request) { |
| 75 |
s := session.Get(r) |
| 76 |
tempCred, _ := s[tempCredKey].(*oauth.Credentials) |
| 77 |
if tempCred == nil || tempCred.Token != r.FormValue("oauth_token") { |
| 78 |
http.Error(w, "Unknown oauth_token.", 500) |
| 79 |
return |
| 80 |
} |
| 81 |
tokenCred, _, err := oauthClient.RequestToken(nil, tempCred, r.FormValue("oauth_verifier")) |
| 82 |
if err != nil { |
| 83 |
http.Error(w, "Error getting request token, "+err.Error(), 500) |
| 84 |
return |
| 85 |
} |
| 86 |
delete(s, tempCredKey) |
| 87 |
s[tokenCredKey] = tokenCred |
| 88 |
s[companyKey] = r.FormValue("realmId") |
| 89 |
if err := session.Save(w, r, s); err != nil { |
| 90 |
http.Error(w, "Error saving session , "+err.Error(), 500) |
| 91 |
return |
| 92 |
} |
| 93 |
http.Redirect(w, r, "/", 302) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func serveLogout(w http.ResponseWriter, r *http.Request) { |
| 98 |
s := session.Get(r) |
| 99 |
delete(s, tokenCredKey) |
| 100 |
delete(s, companyKey) |
| 101 |
if err := session.Save(w, r, s); err != nil { |
| 102 |
http.Error(w, "Error saving session , "+err.Error(), 500) |
| 103 |
return |
| 104 |
} |
| 105 |
http.Redirect(w, r, "/", 302) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
type authHandler struct { |
| 110 |
handler func(w http.ResponseWriter, r *http.Request, c *oauth.Credentials, company string) |
| 111 |
optional bool |
| 112 |
} |
| 113 |
|
| 114 |
func (h *authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 115 |
s := session.Get(r) |
| 116 |
cred, _ := s[tokenCredKey].(*oauth.Credentials) |
| 117 |
if cred == nil && !h.optional { |
| 118 |
http.Error(w, "Not logged in.", 403) |
| 119 |
return |
| 120 |
} |
| 121 |
company, _ := s[companyKey].(string) |
| 122 |
h.handler(w, r, cred, company) |
| 123 |
} |
| 124 |
|
| 125 |
func callAPI(cred *oauth.Credentials, company string, endpoint string, form url.Values, data interface{}) error { |
| 126 |
resp, err := oauthClient.Get(nil, |
| 127 |
cred, |
| 128 |
fmt.Sprintf("https://qb.sbfinance.intuit.com/v3/company/%s/%s", company, endpoint), |
| 129 |
form) |
| 130 |
if err != nil { |
| 131 |
return err |
| 132 |
} |
| 133 |
defer resp.Body.Close() |
| 134 |
if resp.StatusCode != 200 { |
| 135 |
p, _ := ioutil.ReadAll(resp.Body) |
| 136 |
return fmt.Errorf("get %s returned status %d, %s", resp.Request.URL, resp.StatusCode, p) |
| 137 |
} |
| 138 |
return json.NewDecoder(resp.Body).Decode(data) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func respond(w http.ResponseWriter, t *template.Template, data interface{}) { |
| 143 |
w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 144 |
if err := t.Execute(w, data); err != nil { |
| 145 |
log.Print(err) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func serveHome(w http.ResponseWriter, r *http.Request, cred *oauth.Credentials, company string) { |
| 150 |
if r.URL.Path != "/" { |
| 151 |
http.NotFound(w, r) |
| 152 |
return |
| 153 |
} |
| 154 |
if cred == nil { |
| 155 |
data := struct{ Host string }{r.Host} |
| 156 |
respond(w, homeLoggedOutTmpl, &data) |
| 157 |
} else { |
| 158 |
respond(w, homeTmpl, nil) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func serveAccounts(w http.ResponseWriter, r *http.Request, cred *oauth.Credentials, company string) { |
| 163 |
var accounts map[string]interface{} |
| 164 |
if err := callAPI( |
| 165 |
cred, |
| 166 |
company, |
| 167 |
"query", |
| 168 |
url.Values{"query": {"select * from Account"}}, |
| 169 |
&accounts); err != nil { |
| 170 |
http.Error(w, "Error getting accounts, "+err.Error(), 500) |
| 171 |
return |
| 172 |
} |
| 173 |
respond(w, accountsTmpl, accounts) |
| 174 |
} |
| 175 |
|
| 176 |
var httpAddr = flag.String("addr", ":8080", "HTTP server address") |
| 177 |
|
| 178 |
func main() { |
| 179 |
flag.Parse() |
| 180 |
if err := readCredentials(); err != nil { |
| 181 |
log.Fatalf("Error reading configuration, %v", err) |
| 182 |
} |
| 183 |
|
| 184 |
http.Handle("/", &authHandler{handler: serveHome, optional: true}) |
| 185 |
http.Handle("/accounts", &authHandler{handler: serveAccounts}) |
| 186 |
http.HandleFunc("/login", serveLogin) |
| 187 |
http.HandleFunc("/logout", serveLogout) |
| 188 |
http.HandleFunc("/callback", serveOAuthCallback) |
| 189 |
if err := http.ListenAndServe(*httpAddr, nil); err != nil { |
| 190 |
log.Fatalf("Error listening, %v", err) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
var ( |
| 195 |
homeLoggedOutTmpl = template.Must(template.New("loggedout").Parse( |
| 196 |
`<html> |
| 197 |
<head> |
| 198 |
<script src="https://js.appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js" type="text/javascript"></script> |
| 199 |
<script>intuit.ipp.anywhere.setup({ menuProxy: '', grantUrl: 'http://{{.Host}}/login'}); </script> |
| 200 |
</head> |
| 201 |
<body> |
| 202 |
<ipp:connectToIntuit></ipp:connectToIntuit> |
| 203 |
</body> |
| 204 |
</html>`)) |
| 205 |
|
| 206 |
homeTmpl = template.Must(template.New("home").Parse( |
| 207 |
`<html> |
| 208 |
<head> |
| 209 |
</head> |
| 210 |
<body> |
| 211 |
<p><a href="/accounts">accounts</a> |
| 212 |
</body></html>`)) |
| 213 |
|
| 214 |
accountsTmpl = template.Must(template.New("accounts").Parse( |
| 215 |
`<html> |
| 216 |
<head> |
| 217 |
</head> |
| 218 |
<body> |
| 219 |
<p><a href="/">home</a> |
| 220 |
{{range .QueryResponse.Account}}<p>{{.Name}}{{end}} |
| 221 |
<{{.}} |
| 222 |
</body></html>`)) |
| 223 |
) |