7f8a5176ce
I created a server to manage my access and refresh tokens. This server exposes a larger API than it needs to at the moment, but that should change. The goal is to expose a GET at /token to retrieve a valid access token. The server should take care of refreshing tokens before they expire and getting entirely new tokens, should they become so stale that I need to re-authorize my application. A lot of my development of this project has been clumsy. I'm new to Go; I didn't understand OAuth2.0; I'm learning concurrent programming (outside of the context of comfortable Elixir/Erlang). My habits for writing programs in compiled languages feels amateurish. I find myself dropping log.Println's all over the source code when I should be using proper debugging tools like Delve and properly logging with things like httputil.Dump{Request,Response}. The application right now is in a transitional state. There is still plenty of code in main.go that belongs in tokens.go. For instance, the client authorization code belongs in the tokens server. Another question I haven't answered is where is the monzo client that I can use to make function calls like `monzo.Transactions` or `monzo.Accounts`? The benefit of having a tokens server is that it allows me to maintain state of the tokens while I'm developing. This way, I can stop and start main.go without disturbing the state of the access tokens. Of course this isn't the primary benefit, which is to abstract over the OAuth details and expose an API that gives me an access token whenever I request one. The first benefit that I listed could and perhaps should be solved by introducing some simple persistence. I'd like to write the access tokens to disk when I shutdown the tokens server and read them from disk when I start the tokens server. This will come. I could have done this before introducing the tokens server, and it would have saved me a few hours I think. Where has my time gone? Mostly I've been re-authorizing my client unnecessarily. This process is expensive because it opens a web browser, asks me to enter my email address, sends me an email, I then click the link in that email. Overall this takes maybe 1-3 minutes in total. Before my tokens server existed, however, I was doing this about 10-20 times per hour. It's a little disappointing that I didn't rectify this earlier. I'd like to remain vigilant and avoid making similar workflow mistakes as I move ahead.
170 lines
5.4 KiB
Go
170 lines
5.4 KiB
Go
// Exporting Monzo transactions to my YouNeedABudget.com (i.e. YNAB)
|
|
// account. YNAB unfortunately doesn't currently offer an Monzo integration. As
|
|
// a workaround and a practical excuse to learn Go, I decided to write one
|
|
// myself.
|
|
//
|
|
// This job is going to run N times per 24 hours. Monzo offers webhooks for
|
|
// reacting to certain types of events. I don't expect I'll need realtime data
|
|
// for my YNAB integration. That may change, however, so it's worth noting.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"strings"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// Constants
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
var (
|
|
accountId = os.Getenv("monzo_account_id")
|
|
clientId = os.Getenv("monzo_client_id")
|
|
clientSecret = os.Getenv("monzo_client_secret")
|
|
)
|
|
|
|
const (
|
|
redirectURI = "http://localhost:8080/authorization-code"
|
|
// TODO(wpcarro): Consider generating a random string for the state when the
|
|
// application starts instead of hardcoding it here.
|
|
state = "xyz123"
|
|
)
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// Business Logic
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// This is the response returned from Monzo when we exchange our authorization
|
|
// code for an access token. While Monzo returns additional fields, I'm only
|
|
// interested in AccessToken and RefreshToken.
|
|
type accessTokenResponse struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
ExpiresIn int `json:"expires_in"`
|
|
}
|
|
|
|
type setTokensRequest struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
ExpiresIn int `json:"expires_in"`
|
|
}
|
|
|
|
type Tokens struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
ExpiresIn int `json:"expires_in"`
|
|
}
|
|
|
|
// TODO(wpcarro): Replace http.PostForm and other similar calls with
|
|
// client.postForm. The default http.Get and other methods doesn't timeout, so
|
|
// it's better to create a configured client with a value for the timeout.
|
|
|
|
// Returns the access token and refresh tokens for the Monzo API.
|
|
func getTokens(code string) *Tokens {
|
|
res, err := http.PostForm("https://api.monzo.com/oauth2/token", url.Values{
|
|
"grant_type": {"authorization_code"},
|
|
"client_id": {clientId},
|
|
"client_secret": {clientSecret},
|
|
"redirect_uri": {redirectURI},
|
|
"code": {code},
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer res.Body.Close()
|
|
payload := &accessTokenResponse{}
|
|
json.NewDecoder(res.Body).Decode(payload)
|
|
|
|
return &Tokens{payload.AccessToken, payload.RefreshToken, payload.ExpiresIn}
|
|
}
|
|
|
|
// TODO(wpcarro): Prefer using an environment variable for the web browser
|
|
// instead of assuming it will be google-chrome.
|
|
// Open a web browser to allow the user to authorize this application. Return
|
|
// the authorization code sent from Monzo.
|
|
func getAuthCode() string {
|
|
url := fmt.Sprintf("https://auth.monzo.com/?client_id=%s&redirect_uri=%s&response_type=code&state=%s", clientId, redirectURI, state)
|
|
exec.Command("google-chrome", url).Start()
|
|
|
|
authCode := make(chan string)
|
|
go func() {
|
|
log.Fatal(http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
// 1. Get authorization code from Monzo.
|
|
if req.URL.Path == "/authorization-code" {
|
|
params := req.URL.Query()
|
|
reqState := params["state"][0]
|
|
code := params["code"][0]
|
|
|
|
if reqState != state {
|
|
log.Fatalf("Value for state returned by Monzo does not equal our state. %s != %s", reqState, state)
|
|
}
|
|
authCode <- code
|
|
|
|
fmt.Fprintf(w, "Authorized!")
|
|
} else {
|
|
log.Printf("Unhandled request: %v\n", *req)
|
|
}
|
|
})))
|
|
}()
|
|
result := <-authCode
|
|
return result
|
|
}
|
|
|
|
// TODO(wpcarro): Move this logic out of here and into the tokens server.
|
|
func authorize() {
|
|
authCode := getAuthCode()
|
|
tokens := getTokens(authCode)
|
|
client := &http.Client{}
|
|
|
|
payload, _ := json.Marshal(setTokensRequest{
|
|
tokens.AccessToken,
|
|
tokens.RefreshToken,
|
|
tokens.ExpiresIn})
|
|
log.Printf("Access token: %s\n", tokens.AccessToken)
|
|
log.Printf("Refresh token: %s\n", tokens.RefreshToken)
|
|
log.Printf("Expires: %s\n", tokens.ExpiresIn)
|
|
req, _ := http.NewRequest("POST", "http://localhost:4242/set-tokens", bytes.NewBuffer(payload))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
_, err := client.Do(req)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Retrieves the access token from the tokens server.
|
|
func getAccessToken() string {
|
|
return simpleGet("http://localhost:4242/token")
|
|
}
|
|
|
|
func main() {
|
|
accessToken := getAccessToken()
|
|
// authHeaders := map[string]string{
|
|
// "Authorization": fmt.Sprintf("Bearer %s", accessToken),
|
|
// }
|
|
|
|
client := &http.Client{}
|
|
form := url.Values{"account_id": {accountId}}
|
|
req, _ := http.NewRequest("GET", "https://api.monzo.com/transactions", strings.NewReader(form.Encode()))
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken))
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
bytes, _ := httputil.DumpRequest(req, true)
|
|
fmt.Println(string(bytes))
|
|
res, _ := client.Do(req)
|
|
bytes, _ = httputil.DumpResponse(res, true)
|
|
fmt.Println(string(bytes))
|
|
|
|
// res := simpleGet("https://api.monzo.com/accounts", authHeaders, true)
|
|
// fmt.Println(res)
|
|
|
|
os.Exit(0)
|
|
}
|