go-roblox/session.go
2020-09-07 22:45:43 -04:00

90 lines
2.1 KiB
Go

package roblox
import (
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
)
// Session struct for roblox login session data and members
type Session struct {
ID int
Username string
client *http.Client
}
// New create a new session and logs in with provided data
func New(cookie string) (*Session, error) {
cookieJar, _ := cookiejar.New(nil)
rbxCookie := []*http.Cookie{&http.Cookie{
Name: ".ROBLOSECURITY",
Value: cookie,
}}
// url.Parse("http://www.roblox.com") // http://api.roblox.com
cookieJar.SetCookies(&url.URL{Scheme: "http", Host: "www.roblox.com"}, rbxCookie)
cookieJar.SetCookies(&url.URL{Scheme: "http", Host: "api.roblox.com"}, rbxCookie)
cookieJar.SetCookies(&url.URL{Scheme: "https", Host: "economy.roblox.com"}, rbxCookie)
cookieJar.SetCookies(&url.URL{Scheme: "https", Host: "data.roblox.com"}, rbxCookie)
client := &http.Client{
Jar: cookieJar,
}
session := Session{0, "", client}
/*err := session.Login(username, password)
if err != nil {
return nil, fmt.Errorf("Failed to login: %s", err)
}*/
info, err := session.GetUserInfo()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve user information: %s", err)
}
session.ID = info.UserID
session.Username = info.UserName
return &session, err
}
/*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/v2/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/v2/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
}*/