mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-06 23:16:53 +00:00
context: Add authenticated HTTP credentials (#892)
* gRPC: context overide * exchanges: continue update * exchange: Update context handling *Add setter methods for API credentials *Shift credentials functionality to its own file in exchanges package *Add tests *Refactor function DeployCredentialsToContext for library usage *Add function to process credential metadata from API boundary to internal use context value. *Add OTP rpc handling * exchanges: reverts to old style in GetFeeByType, reverts some code I accidently deleted. Plus things and other. XD * template: update * exchanges: fix linter issues * REMOVE THAT AWESOME NEW LINE! * gct: fix some tests * I cant spell :( * exchanges/gctscript: fix more tests * coinnut: fix tests * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * glorious: nits * exchanges/gctcli: stop applying empty credentials * fix linters * exchanges: add test * rpceserver: actually check error for errors * rpcserver: fix up tests * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * exchanges/creds: move tests to corresponding files, add protection and segration for Credentials struct & ptr values * exchanges/creds: allow subaccount to override default credentials via gRPC * exchanges/credentials: don't return nil in GetCredentials * creds: spelling * exchanges: fix glorious NITS! * credentials: Add in test and refactor IsEmpty method. * credentials: change type positioning (glorious) * exchange_template: Fix template changes * DOCS: Refresh * docs: fix spelling * DOCS: fix alignment and add package * DOCS: ALIGN! Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
@@ -361,8 +361,9 @@ func (g *Gemini) SendHTTPRequest(ctx context.Context, ep exchange.URL, path stri
|
||||
// SendAuthenticatedHTTPRequest sends an authenticated HTTP request to the
|
||||
// exchange and returns an error
|
||||
func (g *Gemini) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.URL, method, path string, params map[string]interface{}, result interface{}) (err error) {
|
||||
if !g.AllowAuthenticatedRequest() {
|
||||
return fmt.Errorf("%s %w", g.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
|
||||
creds, err := g.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
endpoint, err := g.API.Endpoints.GetURL(ep)
|
||||
@@ -387,7 +388,7 @@ func (g *Gemini) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.U
|
||||
PayloadBase64 := crypto.Base64Encode(PayloadJSON)
|
||||
hmac, err := crypto.GetHMAC(crypto.HashSHA512_384,
|
||||
[]byte(PayloadBase64),
|
||||
[]byte(g.API.Credentials.Secret))
|
||||
[]byte(creds.Secret))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -395,7 +396,7 @@ func (g *Gemini) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.U
|
||||
headers := make(map[string]string)
|
||||
headers["Content-Length"] = "0"
|
||||
headers["Content-Type"] = "text/plain"
|
||||
headers["X-GEMINI-APIKEY"] = g.API.Credentials.Key
|
||||
headers["X-GEMINI-APIKEY"] = creds.Key
|
||||
headers["X-GEMINI-PAYLOAD"] = PayloadBase64
|
||||
headers["X-GEMINI-SIGNATURE"] = crypto.HexEncodeToString(hmac)
|
||||
headers["Cache-Control"] = "no-cache"
|
||||
|
||||
@@ -403,7 +403,7 @@ func TestGetOrderHistory(t *testing.T) {
|
||||
// Any tests below this line have the ability to impact your orders on the exchange. Enable canManipulateRealOrders to run them
|
||||
// ----------------------------------------------------------------------------------------------------------------------------
|
||||
func areTestAPIKeysSet() bool {
|
||||
return g.ValidateAPICredentials()
|
||||
return g.ValidateAPICredentials(g.GetDefaultCredentials()) == nil
|
||||
}
|
||||
|
||||
func TestSubmitOrder(t *testing.T) {
|
||||
@@ -580,7 +580,7 @@ func TestWsAuth(t *testing.T) {
|
||||
}
|
||||
var dialer websocket.Dialer
|
||||
go g.wsReadData()
|
||||
err = g.WsAuth(&dialer)
|
||||
err = g.WsAuth(context.Background(), &dialer)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -52,7 +53,7 @@ func (g *Gemini) WsConnect() error {
|
||||
go g.wsFunnelConnectionData(g.Websocket.Conn)
|
||||
|
||||
if g.Websocket.CanUseAuthenticatedEndpoints() {
|
||||
err := g.WsAuth(&dialer)
|
||||
err := g.WsAuth(context.TODO(), &dialer)
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%v - websocket authentication failed: %v\n", g.Name, err)
|
||||
g.Websocket.SetCanUseAuthenticatedEndpoints(false)
|
||||
@@ -176,10 +177,14 @@ func (g *Gemini) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
|
||||
}
|
||||
|
||||
// WsAuth will connect to Gemini's secure endpoint
|
||||
func (g *Gemini) WsAuth(dialer *websocket.Dialer) error {
|
||||
func (g *Gemini) WsAuth(ctx context.Context, dialer *websocket.Dialer) error {
|
||||
if !g.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
|
||||
return fmt.Errorf("%v AuthenticatedWebsocketAPISupport not enabled", g.Name)
|
||||
}
|
||||
creds, err := g.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload := WsRequestPayload{
|
||||
Request: "/v1/" + geminiWsOrderEvents,
|
||||
Nonce: time.Now().UnixNano(),
|
||||
@@ -196,7 +201,7 @@ func (g *Gemini) WsAuth(dialer *websocket.Dialer) error {
|
||||
PayloadBase64 := crypto.Base64Encode(PayloadJSON)
|
||||
hmac, err := crypto.GetHMAC(crypto.HashSHA512_384,
|
||||
[]byte(PayloadBase64),
|
||||
[]byte(g.API.Credentials.Secret))
|
||||
[]byte(creds.Secret))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -205,7 +210,7 @@ func (g *Gemini) WsAuth(dialer *websocket.Dialer) error {
|
||||
headers.Add("Content-Length", "0")
|
||||
headers.Add("Content-Type", "text/plain")
|
||||
headers.Add("X-GEMINI-PAYLOAD", PayloadBase64)
|
||||
headers.Add("X-GEMINI-APIKEY", g.API.Credentials.Key)
|
||||
headers.Add("X-GEMINI-APIKEY", creds.Key)
|
||||
headers.Add("X-GEMINI-SIGNATURE", crypto.HexEncodeToString(hmac))
|
||||
headers.Add("Cache-Control", "no-cache")
|
||||
|
||||
|
||||
@@ -664,7 +664,7 @@ func (g *Gemini) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuild
|
||||
if feeBuilder == nil {
|
||||
return 0, fmt.Errorf("%T %w", feeBuilder, common.ErrNilPointer)
|
||||
}
|
||||
if (!g.AllowAuthenticatedRequest() || g.SkipAuthCheck) && // Todo check connection status
|
||||
if (!g.AreCredentialsValid(ctx) || g.SkipAuthCheck) && // Todo check connection status
|
||||
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
||||
feeBuilder.FeeType = exchange.OfflineTradeFee
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user