Skip to content

Commit

Permalink
Use signature in request (#6)
Browse files Browse the repository at this point in the history
* Checkin

* Add pkg version

* Bump to 1.0.0

* Fmt

* Update test url

* Use lowercase

* Add changelog
  • Loading branch information
hoanhan101 authored Sep 1, 2021
1 parent b11b486 commit 83459c0
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 41 deletions.
105 changes: 105 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Changelog

## [v0.1.13](https://github.com/veryfi/veryfi-go/tree/v0.1.13) (2021-09-01)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.12...v0.1.13)

**Closed issues:**

- Setup Github actions [\#5](https://github.com/veryfi/veryfi-go/issues/5)

**Merged pull requests:**

- Use base64 encoded file instead of multipart upload [\#4](https://github.com/veryfi/veryfi-go/pull/4) ([hoanhan101](https://github.com/hoanhan101))

## [v0.1.12](https://github.com/veryfi/veryfi-go/tree/v0.1.12) (2021-08-11)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.11...v0.1.12)

## [v0.1.11](https://github.com/veryfi/veryfi-go/tree/v0.1.11) (2021-07-12)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.10...v0.1.11)

## [v0.1.10](https://github.com/veryfi/veryfi-go/tree/v0.1.10) (2021-07-06)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.9...v0.1.10)

## [v0.1.9](https://github.com/veryfi/veryfi-go/tree/v0.1.9) (2021-07-06)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.8...v0.1.9)

## [v0.1.8](https://github.com/veryfi/veryfi-go/tree/v0.1.8) (2021-07-06)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.8...v0.1.8)

## [0.1.8](https://github.com/veryfi/veryfi-go/tree/0.1.8) (2021-07-06)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.7...0.1.8)

## [v0.1.7](https://github.com/veryfi/veryfi-go/tree/v0.1.7) (2021-07-05)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.6...v0.1.7)

**Implemented enhancements:**

- Update default timeout to 120 seconds instead of 3 seconds [\#2](https://github.com/veryfi/veryfi-go/issues/2)

**Merged pull requests:**

- Update default max wait time [\#3](https://github.com/veryfi/veryfi-go/pull/3) ([hoanhan101](https://github.com/hoanhan101))

## [0.1.6](https://github.com/veryfi/veryfi-go/tree/0.1.6) (2021-05-10)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.6...0.1.6)

## [v0.1.6](https://github.com/veryfi/veryfi-go/tree/v0.1.6) (2021-05-10)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.5...v0.1.6)

## [v0.1.5](https://github.com/veryfi/veryfi-go/tree/v0.1.5) (2021-05-10)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.5...v0.1.5)

## [0.1.5](https://github.com/veryfi/veryfi-go/tree/0.1.5) (2021-05-10)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.4...0.1.5)

## [0.1.4](https://github.com/veryfi/veryfi-go/tree/0.1.4) (2021-05-04)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.4...0.1.4)

## [v0.1.4](https://github.com/veryfi/veryfi-go/tree/v0.1.4) (2021-05-04)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.3...v0.1.4)

## [0.1.3](https://github.com/veryfi/veryfi-go/tree/0.1.3) (2021-05-03)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.2...0.1.3)

## [v0.1.2](https://github.com/veryfi/veryfi-go/tree/v0.1.2) (2021-05-03)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.2...v0.1.2)

## [0.1.2](https://github.com/veryfi/veryfi-go/tree/0.1.2) (2021-05-03)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.1...0.1.2)

## [v0.1.1](https://github.com/veryfi/veryfi-go/tree/v0.1.1) (2021-05-03)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/v0.1.0...v0.1.1)

## [v0.1.0](https://github.com/veryfi/veryfi-go/tree/v0.1.0) (2021-05-03)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.1...v0.1.0)

## [0.1.1](https://github.com/veryfi/veryfi-go/tree/0.1.1) (2021-05-03)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/0.1.0...0.1.1)

## [0.1.0](https://github.com/veryfi/veryfi-go/tree/0.1.0) (2021-05-03)

[Full Changelog](https://github.com/veryfi/veryfi-go/compare/809a57cb245260c615da7b2ba38206296f9af8ca...0.1.0)



\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PKG_VERSION := v0.1.13
PKG_VERSION := v1.0.0
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2> /dev/null || true)
BUILD_DATE := $(shell date -u +%Y-%m-%dT%T 2> /dev/null)

Expand All @@ -13,6 +13,10 @@ clean: ## Remove temporary files and build artifacts
cover: test-unit ## Run unit tests and open the coverage report
go tool cover -html=coverage.out

.PHONY: changelog
changelog: ## Generate changelog from commits via Docker
docker run -it --rm -v "$(pwd)":/usr/local/src/your-app githubchangeloggenerator/github-changelog-generator -u veryfi -p veryfi-go -t FIXME_GITHUB_ACCESS_TOKEN

.PHONY: fmt
fmt: ## Run gofmt on all files
gofmt -s -w .
Expand All @@ -23,7 +27,7 @@ github-tag: ## Create and push a tag with the current client version
git push -u origin ${PKG_VERSION}

.PHONY: lint
lint: ## Lint project source files
lint: ## Lint project source files via Docker
docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:v1.39.0 golangci-lint run

.PHONY: test-unit
Expand All @@ -32,7 +36,7 @@ test-unit: ## Run unit tests

.PHONY: test-integration
test-integration: ## Run integration tests
CLIENT_ID=FIXME USERNAME=FIXME API_KEY=FIXME go test -race -cover -run Integration -coverprofile=coverage.out -covermode=atomic ./...
CLIENT_ID=FIXME CLIENT_SECRET=FIXME USERNAME=FIXME API_KEY=FIXME go test -race -cover -run Integration -coverprofile=coverage.out -covermode=atomic ./...

.PHONY: version
version: ## Print the version
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ To run unit tests:
make test-unit
```

To run integration tests, supply your `CLIENT_ID`, `USERNAME`, and `API_KEY` environment variables in [Makefile](Makefile) and run `make test-integration`:
To run integration tests, supply your `CLIENT_ID`, `CLIENT_SECRET`, `USERNAME`, and `API_KEY` environment variables in [Makefile](Makefile) and run `make test-integration`:
```
.PHONY: test-integration
test-integration: ## Run integration tests
CLIENT_ID=FIXME USERNAME=FIXME API_KEY=FIXME go test -race -cover -run Integration -coverprofile=coverage.out -covermode=atomic ./...
CLIENT_ID=FIXME CLIENT_SECRET=FIXME USERNAME=FIXME API_KEY=FIXME go test -race -cover -run Integration -coverprofile=coverage.out -covermode=atomic ./...
```


Expand All @@ -181,5 +181,3 @@ Below is a introduction to the Go SDK. We're gonna walkthrough a problem and sol
[Link to blog post →](https://www.veryfi.com/go/)

If you prefer a video format, here is the link to our Youtube channel →

[![Watch 'Code with Matt' Video](https://img.youtube.com/vi/HK-7lvY5J9E/0.jpg)](https://www.youtube.com/watch?v=HK-7lvY5J9E)
44 changes: 35 additions & 9 deletions veryfi/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package veryfi

import (
"crypto/hmac"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"

"github.com/pkg/errors"

Expand All @@ -21,6 +27,9 @@ type Client struct {
// apiVersion is the current API version of Veryfi that we are
// communicating with.
apiVersion string

// pkgVersion is the current SDK version.
pkgVersion string
}

// NewClientV7 returns a new instance of a client for v7 API.
Expand All @@ -34,6 +43,7 @@ func NewClientV7(opts *Options) (*Client, error) {
options: opts,
client: c,
apiVersion: "v7",
pkgVersion: "1.0.0",
}, nil
}

Expand Down Expand Up @@ -235,13 +245,17 @@ func (c *Client) DeleteGlobalTag(tagID string) error {
}

// request returns an authorized request to Veryfi API.
func (c *Client) request(okScheme interface{}, errScheme interface{}) *resty.Request {
func (c *Client) request(payload interface{}, okScheme interface{}, errScheme interface{}) *resty.Request {
timestamp := int(time.Now().Unix())
return c.setBaseURL().R().
SetHeaders(map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
"CLIENT-ID": c.options.ClientID,
"AUTHORIZATION": fmt.Sprintf("apikey %s:%s", c.options.Username, c.options.APIKey),
"User-Agent": fmt.Sprintf("Go Veryfi-Go/%s", c.pkgVersion),
"Content-Type": "application/json",
"Accept": "application/json",
"Client-Id": c.options.ClientID,
"Authorization": fmt.Sprintf("apikey %s:%s", c.options.Username, c.options.APIKey),
"X-Veryfi-Request-Timestamp": strconv.Itoa(timestamp),
"X-Veryfi-Request-Signature": c.generateSignature(payload, timestamp),
}).
SetResult(okScheme).
SetError(errScheme)
Expand All @@ -255,7 +269,7 @@ func (c *Client) setBaseURL() *resty.Client {
// post performs a POST request against Veryfi API.
func (c *Client) post(uri string, body interface{}, okScheme interface{}) error {
errScheme := new(scheme.Error)
request := c.request(okScheme, errScheme).SetBody(body)
request := c.request(body, okScheme, errScheme).SetBody(body)

_, err := request.Post(uri)

Expand All @@ -265,7 +279,7 @@ func (c *Client) post(uri string, body interface{}, okScheme interface{}) error
// put performs a PUT request against Veryfi API.
func (c *Client) put(uri string, body interface{}, okScheme interface{}) error {
errScheme := new(scheme.Error)
request := c.request(okScheme, errScheme).SetBody(body)
request := c.request(body, okScheme, errScheme).SetBody(body)
_, err := request.Put(uri)

return check(err, errScheme)
Expand All @@ -274,7 +288,7 @@ func (c *Client) put(uri string, body interface{}, okScheme interface{}) error {
// get performs a GET request against Veryfi API.
func (c *Client) get(uri string, queryParams interface{}, okScheme interface{}) error {
errScheme := new(scheme.Error)
request := c.request(okScheme, errScheme)
request := c.request(queryParams, okScheme, errScheme)
if queryParams != nil {
request.SetQueryParams(structToMap(queryParams))
}
Expand All @@ -287,12 +301,24 @@ func (c *Client) get(uri string, queryParams interface{}, okScheme interface{})
// rdelete performs a DELETE request against Veryfi API.
func (c *Client) rdelete(uri string) error {
errScheme := new(scheme.Error)
request := c.request(map[string]string{}, errScheme)
request := c.request(struct{}{}, map[string]string{}, errScheme)
_, err := request.Delete(uri)

return check(err, errScheme)
}

// generateSignature for a given request.
func (c *Client) generateSignature(s interface{}, timestamp int) string {
p := []string{fmt.Sprintf("timestamp:%v", timestamp)}
for k, v := range structToMap(s) {
p = append(p, fmt.Sprintf("%v:%v", k, v))
}

h := hmac.New(sha256.New, []byte(c.options.ClientSecret))
h.Write([]byte(strings.Join(p, ",")))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

// check validates returned response from Veryfi.
func check(err error, errResp *scheme.Error) error {
if err != nil {
Expand Down
51 changes: 29 additions & 22 deletions veryfi/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (

func cleanUp(t *testing.T, documentID int) {
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
})
assert.NotNil(t, client)
assert.NoError(t, err)
Expand All @@ -37,9 +38,10 @@ func TestIntegrationFailNoAuth(t *testing.T) {

func TestIntegrationFailInvalidClientID(t *testing.T) {
client, err := NewClientV7(&Options{
ClientID: "foo",
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
ClientID: "foo",
ClientSecret: "bar",
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
})
assert.NotNil(t, client)
assert.NoError(t, err)
Expand All @@ -53,9 +55,10 @@ func TestIntegrationFailInvalidClientID(t *testing.T) {

func TestIntegrationFailInvalidUsername(t *testing.T) {
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: "foo",
APIKey: os.Getenv("API_KEY"),
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Username: "foo",
APIKey: os.Getenv("API_KEY"),
})
assert.NoError(t, err)
assert.NotNil(t, client)
Expand All @@ -69,9 +72,10 @@ func TestIntegrationFailInvalidUsername(t *testing.T) {

func TestIntegrationFailInvalidAPIKey(t *testing.T) {
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: "foo",
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Username: os.Getenv("USERNAME"),
APIKey: "foo",
})
assert.NoError(t, err)
assert.NotNil(t, client)
Expand All @@ -85,9 +89,10 @@ func TestIntegrationFailInvalidAPIKey(t *testing.T) {

func TestIntegrationFailInvalidDocument(t *testing.T) {
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
})
assert.NotNil(t, client)
assert.NoError(t, err)
Expand All @@ -101,15 +106,16 @@ func TestIntegrationFailInvalidDocument(t *testing.T) {

func TestIntegrationSuccessProcessDocumentURL(t *testing.T) {
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
})
assert.NotNil(t, client)
assert.NoError(t, err)

resp, err := client.ProcessDocumentURL(scheme.DocumentURLOptions{
FileURL: "https://templates.invoicehome.com/invoice-template-us-neat-750px.png",
FileURL: "http://cdn-dev.veryfi.com/testing/veryfi-python/receipt_public.jpg",
DocumentSharedOptions: scheme.DocumentSharedOptions{
Tags: []string{"integration", "test", "url"},
},
Expand All @@ -121,9 +127,10 @@ func TestIntegrationSuccessProcessDocumentURL(t *testing.T) {

func TestIntegrationSuccessProcessDocumentUpload(t *testing.T) {
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
})
assert.NotNil(t, client)
assert.NoError(t, err)
Expand Down
Loading

0 comments on commit 83459c0

Please sign in to comment.