2017-01-07 05:20:45 +00:00
|
|
|
package roblox
|
|
|
|
|
|
|
|
import (
|
2018-10-10 00:57:50 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2017-01-07 05:20:45 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Session struct for roblox login session data and members
|
|
|
|
type Session struct {
|
2017-09-28 23:27:53 +00:00
|
|
|
ID int
|
2017-01-07 08:48:57 +00:00
|
|
|
Username string
|
2017-01-07 05:20:45 +00:00
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// New create a new session and logs in with provided data
|
2017-09-28 23:27:53 +00:00
|
|
|
func New(username, password string) (*Session, error) {
|
2017-01-07 05:20:45 +00:00
|
|
|
cookieJar, _ := cookiejar.New(nil)
|
|
|
|
client := &http.Client{
|
|
|
|
Jar: cookieJar,
|
|
|
|
}
|
|
|
|
|
2017-09-28 23:27:53 +00:00
|
|
|
session := Session{0, username, client}
|
2017-01-07 05:20:45 +00:00
|
|
|
|
2018-10-10 00:57:50 +00:00
|
|
|
err := session.Login(username, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to login: %s", err)
|
2017-01-07 05:20:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 23:27:53 +00:00
|
|
|
info, err := session.GetUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to retrieve user information: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
session.ID = info.UserID
|
|
|
|
|
2017-01-07 05:20:45 +00:00
|
|
|
return &session, err
|
|
|
|
}
|
2018-10-10 00:57:50 +00:00
|
|
|
|
|
|
|
func (s *Session) Login(username, password string) error {
|
|
|
|
details := struct {
|
|
|
|
Ctype string `json:"ctype"`
|
|
|
|
Cvalue string `json:"cvalue"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}{
|
|
|
|
"Username",
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
}
|
|
|
|
payload, err := json.Marshal(&details)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Post("https://auth.roblox.com/v1/login", "application/json", bytes.NewBuffer(payload))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode == 403 {
|
|
|
|
req, err := http.NewRequest("POST", "https://auth.roblox.com/v1/login", bytes.NewBuffer(payload))
|
|
|
|
req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0])
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := s.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("Status %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|