Skip to content

Commit

Permalink
chore: temporary commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and RobertCraigie committed Mar 27, 2024
1 parent da24dc4 commit 46f8947
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 292 deletions.
33 changes: 0 additions & 33 deletions accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ package finchgo

import (
"context"
"errors"
"net/http"

"github.com/Finch-API/finch-api-go/internal/apijson"
"github.com/Finch-API/finch-api-go/internal/param"
"github.com/Finch-API/finch-api-go/internal/requestconfig"
"github.com/Finch-API/finch-api-go/option"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

// AccessTokenService contains methods and other services that help with
Expand All @@ -35,39 +32,9 @@ func NewAccessTokenService(opts ...option.RequestOption) (r *AccessTokenService)
// Exchange the authorization code for an access token
func (r *AccessTokenService) New(ctx context.Context, body AccessTokenNewParams, opts ...option.RequestOption) (res *CreateAccessTokenResponse, err error) {
opts = append(r.Options[:], opts...)

opts = append(opts[:], func(rc *requestconfig.RequestConfig) error {
bodyClientID := gjson.Get(string(rc.Buffer), "client_id")
if !bodyClientID.Exists() {
if rc.ClientID == "" {
return errors.New("client_id must be provided as an argument or with the FINCH_CLIENT_ID environment variable")
}
updatedBody, err := sjson.Set(string(rc.Buffer), "client_id", rc.ClientID)
if err != nil {
return err
}
rc.Buffer = []byte(updatedBody)
}

bodyClientSecret := gjson.Get(string(rc.Buffer), "client_secret")
if !bodyClientSecret.Exists() {
if rc.ClientSecret == "" {
return errors.New("client_secret must be provided as an argument or with the FINCH_CLIENT_SECRET environment variable")
}
updatedBody, err := sjson.Set(string(rc.Buffer), "client_secret", rc.ClientSecret)
if err != nil {
return err
}
rc.Buffer = []byte(updatedBody)
}

return nil
})

path := "auth/token"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return

}

type CreateAccessTokenResponse struct {
Expand Down
12 changes: 0 additions & 12 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
- <a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go/internal/shared">shared</a>.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go/internal/shared#OperationSupportMatrix">OperationSupportMatrix</a>
- <a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go/internal/shared">shared</a>.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go/internal/shared#Paging">Paging</a>

# finchgo

Methods:

- <code>client.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go#FinchgoService.GetAuthURL">GetAuthURL</a>(products string, redirectUri string, sandbox bool, opts ...option.RequestOption) (string, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code>client.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go#FinchgoService.WithAccessToken">WithAccessToken</a>(accessToken string) (Client, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>

# AccessTokens

Response Types:
Expand Down Expand Up @@ -183,11 +176,6 @@ Response Types:
- <a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go">finchgo</a>.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go#PaymentEvent">PaymentEvent</a>
- <a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go">finchgo</a>.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go#WebhookEvent">WebhookEvent</a>

Methods:

- <code>client.Webhooks.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go#WebhookService.Unwrap">Unwrap</a>(payload []byte, headers http.Header, secret string, now time.Time) (WebhookEvent, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code>client.Webhooks.<a href="https://pkg.go.dev/github.com/Finch-API/finch-api-go#WebhookService.VerifySignature">VerifySignature</a>(payload []byte, headers http.Header, secret string, now time.Time) <a href="https://pkg.go.dev/builtin#error">error</a></code>

# RequestForwarding

Response Types:
Expand Down
86 changes: 0 additions & 86 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@
package finchgo

import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"os"
"strconv"

"github.com/Finch-API/finch-api-go/internal/requestconfig"
"github.com/Finch-API/finch-api-go/option"
)

Expand Down Expand Up @@ -67,82 +60,3 @@ func NewClient(opts ...option.RequestOption) (r *Client) {

return
}

// DEPRECATED: use client.accessTokens().create instead.
func (r *Client) GetAccessToken(ctx context.Context, code string, redirectUri string, opts ...option.RequestOption) (res string, err error) {
opts = append(r.Options[:], opts...)
opts = append(opts[:], option.WithHeaderDel("authorization"))

path := "/auth/token"

var result map[string]string
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodPost, path, nil, &result, opts...)
if err != nil {
return "", err
}
if cfg.ClientID == "" {
return "", errors.New("expected ClientID to be set in order to call GetAccessToken")
}
if cfg.ClientSecret == "" {
return "", errors.New("expected ClientSecret to be set in order to call GetAccessToken")
}

body := struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Code string `json:"code"`
RedirectURI string `json:"redirect_uri"`
}{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Code: code,
RedirectURI: redirectUri,
}
cfg.Apply(func(rc *requestconfig.RequestConfig) (err error) {
rc.Buffer, err = json.Marshal(body)
rc.Request.Header.Set("Content-Type", "application/json")
return err
})

err = cfg.Execute()
if err != nil {
return "", err
}
accessToken, ok := result["access_token"]
if !ok {
return "", errors.New("access_token not found in response")
}

return accessToken, nil
}

// Returns the authorization URL which can be visited in order to obtain an
// authorization code from Finch. The authorization code can then be exchanged for
// an access token for the Finch API by calling getAccessToken().
func (r *Client) GetAuthURL(products string, redirectUri string, sandbox bool, opts ...option.RequestOption) (res string, err error) {
opts = append(r.Options[:], opts...)
cfg := requestconfig.RequestConfig{}
cfg.Apply(opts...)

if cfg.ClientID == "" {
return "", errors.New("expected the ClientID to be set in order to call GetAuthUrl")
}
u, err := url.Parse("https://connect.tryfinch.com/authorize")
if err != nil {
return "", err
}
q := u.Query()
q.Set("client_id", cfg.ClientID)
q.Set("products", products)
q.Set("redirect_uri", redirectUri)
q.Set("sandbox", strconv.FormatBool(sandbox))
u.RawQuery = q.Encode()
return u.String(), nil
}

// Returns a copy of the current Finch client with the given access token for
// authentication.
func (r *Client) WithAccessToken(accessToken string) (res Client, err error) {
opts := append(r.Options[:], option.WithAccessToken(accessToken))
return Client{Options: opts}, nil
}
29 changes: 0 additions & 29 deletions examples/auth/main.go

This file was deleted.

12 changes: 0 additions & 12 deletions finchgo.go

This file was deleted.

92 changes: 0 additions & 92 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@
package finchgo

import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"time"

"github.com/Finch-API/finch-api-go/internal/apijson"
"github.com/Finch-API/finch-api-go/internal/shared"
Expand All @@ -37,83 +28,6 @@ func NewWebhookService(opts ...option.RequestOption) (r *WebhookService) {
return
}

// Validates that the given payload was sent by Finch and parses the payload.
func (r *WebhookService) Unwrap(payload []byte, headers http.Header, secret string, now time.Time) (res WebhookEvent, err error) {
err = r.VerifySignature(payload, headers, secret, now)
if err != nil {
return nil, err
}

event := WebhookEvent(nil)
err = apijson.UnmarshalRoot(payload, &event)
if err != nil {
return nil, err
}
return event, nil
}

// Validates whether or not the webhook payload was sent by Finch.
//
// An error will be raised if the webhook payload was not sent by Finch.
func (r *WebhookService) VerifySignature(payload []byte, headers http.Header, secret string, now time.Time) (err error) {
parsedSecret, err := base64.StdEncoding.DecodeString(secret)
if err != nil {
return fmt.Errorf("invalid webhook secret: %s", err)
}

id := headers.Get("finch-event-id")
if len(id) == 0 {
return errors.New("could not find finch-event-id header")
}
sign := headers.Values("finch-signature")
if len(sign) == 0 {
return errors.New("could not find finch-signature header")
}
unixtime := headers.Get("finch-timestamp")
if len(unixtime) == 0 {
return errors.New("could not find finch-timestamp header")
}

timestamp, err := strconv.ParseInt(unixtime, 10, 64)
if err != nil {
return fmt.Errorf("invalid timestamp header: %s, %s", unixtime, err)
}

if timestamp < now.Unix()-300 {
return errors.New("webhook timestamp too old")
}
if timestamp > now.Unix()+300 {
return errors.New("webhook timestamp too new")
}

mac := hmac.New(sha256.New, parsedSecret)
mac.Write([]byte(id))
mac.Write([]byte("."))
mac.Write([]byte(unixtime))
mac.Write([]byte("."))
mac.Write(payload)
expected := mac.Sum(nil)

for _, part := range sign {
parts := strings.Split(part, ",")
if len(parts) != 2 {
continue
}
if parts[0] != "v1" {
continue
}
signature, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
continue
}
if hmac.Equal(signature, expected) {
return nil
}
}

return errors.New("None of the given webhook signatures match the expected signature")
}

type AccountUpdateEvent struct {
Data AccountUpdateEventData `json:"data"`
EventType AccountUpdateEventEventType `json:"event_type"`
Expand Down Expand Up @@ -1716,9 +1630,3 @@ func init() {
},
)
}

type WebhookUnwrapParams struct {
}

type WebhookVerifySignatureParams struct {
}
28 changes: 0 additions & 28 deletions webhook_test.go

This file was deleted.

0 comments on commit 46f8947

Please sign in to comment.