Skip to content

Commit

Permalink
Add simple CLI with auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
anothertobi committed Jun 19, 2023
1 parent 3084ee0 commit bac3411
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 20 deletions.
27 changes: 8 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ Little helper to get transactions from Viseca One and print them in CSV format.
## Usage

1. Log in to [one.viseca.ch](https://one.viseca.ch)
1. Open the developer tools of your browser and navigate to the network tab
1. Go to "Transaktionen" on [one.viseca.ch](https://one.viseca.ch)
1. Filter the URLs in the network tab of the developer tools for `transactions`
1. Save the card ID from the path (between `/v1/card/` and `/transactions`) to an env file (see examples)
1. Save the session cookie (`AL_SESS-S=AAAAAA...`) to an env file (see examples)
1. Save the card ID from the path (between `/v1/card/` and `/transactions`)

1. ```
source .env
go run main.go "$CARDID" "$COOKIE" > data/export.csv
export VISECA_CLI_USERNAME=<email>
export VISECA_CLI_PASSWORD=<password>
go run cmd/viseca-cli/main.go transactions <cardID>
```
### CLI Output
## Examples
### Env file
```
export CARDID=0000000AAAAA0000
export COOKIE=AAAAAAAAAA...
```csv
"TransactionID","Date","Merchant","Amount","PFMCategoryID","PFMCategoryName"
"AUTH8c919db2-1c23-43f1-8862-61c31336d9b6","2021-10-20T17:05:44","ALDI","50.550000","cv_groceries","Groceries"
```

## API
Expand Down Expand Up @@ -74,10 +70,3 @@ Large page sizes (e.g. 1000) lead to an error.
]
}
```

### CLI Output

```csv
"TransactionID","Date","Merchant","Amount","PFMCategoryID","PFMCategoryName"
"AUTH8c919db2-1c23-43f1-8862-61c31336d9b6","2021-10-20T17:05:44","ALDI","50.550000","cv_groceries","Groceries"
```
37 changes: 37 additions & 0 deletions cmd/viseca-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"log"
"os"

"github.com/anothertobi/viseca-exporter/internal/app"
"github.com/urfave/cli/v2"
)

func main() {
app := &cli.App{
Name: "viseca-cli",
Usage: "query Viseca one",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Usage: "username",
Required: true,
EnvVars: []string{"VISECA_CLI_USERNAME"},
},
&cli.StringFlag{
Name: "password",
Usage: "password",
Required: true,
EnvVars: []string{"VISECA_CLI_PASSWORD"},
},
},
Commands: []*cli.Command{
app.NewTransactionsCommand(),
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ module github.com/anothertobi/viseca-exporter
go 1.19

require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/stretchr/testify v1.8.3
github.com/urfave/cli/v2 v2.25.7
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/net v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
45 changes: 45 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand All @@ -11,6 +19,43 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
96 changes: 96 additions & 0 deletions internal/app/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package app

import (
"context"
"errors"
"net/http"
"net/http/cookiejar"
"net/url"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/anothertobi/viseca-exporter/pkg/viseca"
)

func login(ctx context.Context, username string, password string) (*viseca.Client, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
httpClient := &http.Client{
Jar: cookieJar,
// Don't follow redirects since the auth flow is built around redirects
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

res, err := httpClient.Get("https://one.viseca.ch/login/login")
if err != nil {
return nil, err
}
defer res.Body.Close()

doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}

var formToken string
var found bool
doc.Find(`input[name="FORM_TOKEN"]`).EachWithBreak(func(i int, s *goquery.Selection) bool {
formToken, found = s.Attr("value")
return !found
})

form := url.Values{}
form.Add("FORM_TOKEN", formToken)
form.Add("USERNAME", username)
form.Add("PASSWORD", password)

res, err = httpClient.PostForm("https://one.viseca.ch/login/login", form)
if err != nil {
return nil, err
}
defer res.Body.Close()

if res.StatusCode != 302 {
return nil, errors.New("login failed (no redirect response)")
}

err = awaitAppConfirmation(ctx, httpClient)
if err != nil {
return nil, err
}

visecaClient := viseca.NewClient(httpClient)

return visecaClient, nil
}

func awaitAppConfirmation(ctx context.Context, httpClient *http.Client) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://one.viseca.ch/login/app-confirmation", nil)
if err != nil {
return err
}

for {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return err
}
default:
res, err := httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()

if res.StatusCode == 302 {
return nil
}
}
time.Sleep(2 * time.Second)
}
}
80 changes: 80 additions & 0 deletions internal/app/transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package app

import (
"context"
"errors"
"fmt"
"time"

"github.com/anothertobi/viseca-exporter/internal/csv"
"github.com/anothertobi/viseca-exporter/pkg/viseca"
"github.com/urfave/cli/v2"
)

const flagDateFrom = "date-from"
const flagDateTo = "date-to"

// NewTransactionsCommand creates a new transactions CLI command
func NewTransactionsCommand() *cli.Command {
return &cli.Command{
Name: "transactions",
Usage: "list all transactions for given card id",
ArgsUsage: "cardID",
Flags: []cli.Flag{
&cli.TimestampFlag{
Name: flagDateFrom,
Usage: "from which date on transactions should be fetched (format: 2006-01-02)",
Layout: "2006-01-02",
Timezone: time.Local,
},
&cli.TimestampFlag{
Name: flagDateTo,
Usage: "to which date transactions should be fetched (format: 2006-01-02)",
Layout: "2006-01-02",
Timezone: time.Local,
},
},
Action: func(cCtx *cli.Context) error {
return transactions(cCtx)
},
}
}

func transactions(cCtx *cli.Context) error {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

if !cCtx.Args().Present() {
return errors.New("requires card id arg")
}
cardID := cCtx.Args().First()

username := cCtx.String("username")
password := cCtx.String("password")

visecaClient, err := login(ctx, username, password)
if err != nil {
return err
}

listOptions := viseca.NewDefaultListOptions()
dateFrom := cCtx.Timestamp(flagDateFrom)
if dateFrom != nil {
listOptions.DateFrom = *dateFrom
}
dateTo := cCtx.Timestamp(flagDateTo)
if dateTo != nil {
listOptions.DateFrom = *dateTo
}

// returns all transactions if date from
transactions, err := visecaClient.ListAllTransactionsOpts(ctx, cardID, listOptions)
if err != nil {
return err
}

out := csv.TransactionsString(transactions)
fmt.Print(out)

return nil
}

0 comments on commit bac3411

Please sign in to comment.