diff options
| author | Teddy Wing | 2015-06-06 19:25:17 -0400 |
|---|---|---|
| committer | Teddy Wing | 2015-06-06 19:25:17 -0400 |
| commit | 1135764727f3dc525d0f674c784edbc81cc786f3 (patch) | |
| tree | 5c402c88d3a07a3cde84237ead5829bda28f3e25 /vendor/_nuts/github.com/fabioberger/coinbase-go/client_oauth_authentication.go | |
| download | New-House-on-the-Block-1135764727f3dc525d0f674c784edbc81cc786f3.tar.bz2 | |
Initial commit. Install coinbase-go.
* Use nut for vendored dependencies
* Install coinbase-go to interact with the Coinbase API
Diffstat (limited to 'vendor/_nuts/github.com/fabioberger/coinbase-go/client_oauth_authentication.go')
| -rw-r--r-- | vendor/_nuts/github.com/fabioberger/coinbase-go/client_oauth_authentication.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/_nuts/github.com/fabioberger/coinbase-go/client_oauth_authentication.go b/vendor/_nuts/github.com/fabioberger/coinbase-go/client_oauth_authentication.go new file mode 100644 index 0000000..b8f924c --- /dev/null +++ b/vendor/_nuts/github.com/fabioberger/coinbase-go/client_oauth_authentication.go @@ -0,0 +1,60 @@ +package coinbase + +import ( + "errors" + "net/http" + "time" +) + +// ClientOAuthAuthentication Struct implements the Authentication interface +// and takes care of authenticating OAuth RPC requests on behalf of a client +// (i.e GetBalance()) +type clientOAuthAuthentication struct { + Tokens *oauthTokens + BaseUrl string + Client http.Client +} + +// ClientOAuth instantiates ClientOAuthAuthentication with the client OAuth tokens +func clientOAuth(tokens *oauthTokens) *clientOAuthAuthentication { + return clientOAuthWithEnv(tokens, false) +} + +// ClientOAuthWithEnv instantiates ClientOAuthAuthentication with the client OAuth tokens and the specified environment +func clientOAuthWithEnv(tokens *oauthTokens, sandbox bool) *clientOAuthAuthentication { + baseUrl := "https://api.coinbase.com/v1/" // Live Url + + // Check if should use sandbox + if sandbox { + baseUrl = "https://api.sandbox.coinbase.com/v1/" // Sandbox Url + } + a := clientOAuthAuthentication{ + Tokens: tokens, + BaseUrl: baseUrl, + Client: http.Client{ + Transport: &http.Transport{ + Dial: dialTimeout, + }, + }, + } + return &a +} + +// Client OAuth authentication requires us to attach an unexpired OAuth token to +// the request header +func (a clientOAuthAuthentication) authenticate(req *http.Request, endpoint string, params []byte) error { + // Ensure tokens havent expired + if time.Now().UTC().Unix() > a.Tokens.ExpireTime { + return errors.New("The OAuth tokens are expired. Use refreshTokens to refresh them") + } + req.Header.Set("Authorization", "Bearer "+a.Tokens.AccessToken) + return nil +} + +func (a clientOAuthAuthentication) getBaseUrl() string { + return a.BaseUrl +} + +func (a clientOAuthAuthentication) getClient() *http.Client { + return &a.Client +} |
