2024-05-14 21:50:57 -04:00
|
|
|
package tastytrade
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2025-05-07 23:50:46 -04:00
|
|
|
"time"
|
2024-05-14 21:50:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
ExternalID string `json:"external-id"`
|
|
|
|
|
IsConfirmed bool `json:"is-confirmed"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AuthData struct {
|
2025-05-07 23:50:46 -04:00
|
|
|
User User `json:"user"`
|
|
|
|
|
SessionToken string `json:"session-token"`
|
|
|
|
|
SessionExpire time.Time `json:"session-expiration"`
|
2025-05-07 01:19:21 -04:00
|
|
|
RememberToken string `json:"remember-token"`
|
2024-05-14 21:50:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AuthResponse struct {
|
|
|
|
|
Data AuthData `json:"data"`
|
|
|
|
|
Context string `json:"context"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Authenticate authenticates the client with the Tastytrade API
|
2025-05-07 01:19:21 -04:00
|
|
|
func (api *TastytradeAPI) Authenticate2(username, password string, remember bool) error {
|
|
|
|
|
var remStr string
|
|
|
|
|
if remember {
|
|
|
|
|
remStr = "true"
|
|
|
|
|
} else {
|
|
|
|
|
remStr = "false"
|
|
|
|
|
}
|
2024-05-14 21:50:57 -04:00
|
|
|
authURL := fmt.Sprintf("%s/sessions", api.host)
|
|
|
|
|
authData := map[string]string{
|
|
|
|
|
"login": username,
|
|
|
|
|
"password": password,
|
2025-05-07 01:19:21 -04:00
|
|
|
"remember-me": remStr,
|
2024-05-14 21:50:57 -04:00
|
|
|
}
|
|
|
|
|
authBody, err := json.Marshal(authData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := api.httpClient.Post(authURL, "application/json", bytes.NewReader(authBody))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-05-08 00:36:03 -04:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2024-05-14 21:50:57 -04:00
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
|
|
|
|
return fmt.Errorf("authentication failed: status code %d", resp.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
authResponse := AuthResponse{}
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&authResponse); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 23:50:46 -04:00
|
|
|
api.authToken = authResponse.Data.SessionToken
|
|
|
|
|
api.authExpire = authResponse.Data.SessionExpire
|
|
|
|
|
if (remember) {
|
|
|
|
|
api.remToken = authResponse.Data.RememberToken
|
2025-05-07 01:19:21 -04:00
|
|
|
}
|
2024-05-14 21:50:57 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
2025-05-07 01:19:21 -04:00
|
|
|
|
|
|
|
|
func (api *TastytradeAPI) Authenticate(username, password string) error {
|
|
|
|
|
return api.Authenticate2(username, password, false)
|
|
|
|
|
}
|
2025-05-07 23:50:46 -04:00
|
|
|
|
|
|
|
|
// Redeem a remember token to get a session
|
|
|
|
|
|
|
|
|
|
func (api *TastytradeAPI) AuthRemember(remember bool) error {
|
|
|
|
|
var remStr string
|
|
|
|
|
if remember {
|
|
|
|
|
remStr = "true"
|
|
|
|
|
} else {
|
|
|
|
|
remStr = "false"
|
|
|
|
|
}
|
|
|
|
|
authURL := fmt.Sprintf("%s/sessions", api.host)
|
|
|
|
|
authData := map[string]string{
|
|
|
|
|
"login": api.user,
|
|
|
|
|
"remember-token": api.remToken,
|
|
|
|
|
"remember-me": remStr,
|
|
|
|
|
}
|
|
|
|
|
authBody, err := json.Marshal(authData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := api.httpClient.Post(authURL, "application/json", bytes.NewReader(authBody))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-05-08 00:36:03 -04:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2025-05-07 23:50:46 -04:00
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
|
|
|
|
return fmt.Errorf("authentication failed: status code %d", resp.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
authResponse := AuthResponse{}
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&authResponse); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api.authToken = authResponse.Data.SessionToken
|
|
|
|
|
if (remember) {
|
|
|
|
|
api.remToken = authResponse.Data.RememberToken
|
|
|
|
|
} else {
|
|
|
|
|
api.remToken = ""
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|