예제 #1
0
//Login logs into steam service with given credentials
func (sess *Session) Login(credentials Credentials) (*LoginResponse, error) {
	key, err := sess.getRSA(credentials.Username)
	if err != nil {
		return nil, err
	}
	encryptedPassword, err := sess.encryptPassword(credentials.Password, key)
	if err != nil {
		return nil, err
	}
	credentials.Password = encryptedPassword
	credentials.RSATimeStamp = strconv.FormatUint(key.Timestamp, 10)
	credentials.DoNotCache = strconv.FormatInt(time.Now().Unix(), 10)
	vals, err := query.Values(&credentials)
	if err != nil {
		return nil, err
	}
	req := sess.NewRequest("POST", "https://steamcommunity.com/login/dologin/", strings.NewReader(vals.Encode()))
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	if credentials.Token != "" {
		//Workaround for steamguard
		req.AddCookie(&http.Cookie{
			Name:   fmt.Sprintf("steamMachineAuth%s", credentials.SteamID),
			Value:  credentials.Token,
			Path:   "/",
			Domain: ".steamcommunity.com",
			Secure: true,
		})
	}
	resp, err := utils.RetryRequest(3, sess.HTTPClient, req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, SteamCommunityUnvavailable
	}
	loginr := new(LoginResponse)
	buf := new(bytes.Buffer)
	if _, err = buf.ReadFrom(resp.Body); err != nil {
		return nil, err
	}
	err = json.Unmarshal(buf.Bytes(), loginr)
	return loginr, err
}
예제 #2
0
func (sess *Session) getRSA(username string) (*steamPublicKey, error) {
	reqParams := make(url.Values)
	reqParams.Add("username", username)
	reqParams.Add("donotcache", strconv.FormatUint(uint64(time.Now().Unix()), 10))
	req := sess.NewRequest("POST", "https://steamcommunity.com/login/getrsakey/", strings.NewReader(reqParams.Encode()))
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := utils.RetryRequest(3, sess.HTTPClient, req)
	if err != nil {
		return nil, fmt.Errorf(errorGettingKey, err.Error())
	}
	defer resp.Body.Close()
	buf := new(bytes.Buffer)
	if _, err = buf.ReadFrom(resp.Body); err != nil {
		return nil, fmt.Errorf(errorGettingKey, err.Error())
	}
	spk := new(steamPublicKey)
	if json.Unmarshal(buf.Bytes(), spk) != nil {
		return nil, fmt.Errorf(errorGettingKey, err.Error())
	}
	if !spk.Success {
		return nil, errors.New(errorGettingKey)
	}
	return spk, nil
}