-
Notifications
You must be signed in to change notification settings - Fork 39
/
client_oauth_authentication.go
51 lines (44 loc) · 1.31 KB
/
client_oauth_authentication.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package coinbase
import (
"errors"
"net/http"
"time"
"github.com/fabioberger/coinbase-go/config"
)
// 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 {
a := clientOAuthAuthentication{
Tokens: tokens,
BaseUrl: config.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
}