-
Notifications
You must be signed in to change notification settings - Fork 2
/
login.go
133 lines (109 loc) · 3.61 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package auth
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/vincentserpoul/mangosteam"
)
// doLogInResponse is used to store the body of the steam doLogin response
type doLogInResponse struct {
Success bool `json:"success"`
LoginComplete bool `json:"login_complete"`
CaptchaNeeded bool `json:"captcha_needed"`
CaptchaGID string `json:"captcha_gid"`
EmailSteamID uint64 `json:"emailsteamid,string"`
EmailAuthNeeded bool `json:"emailauth_needed"`
TwoFactorNeeded bool `json:"requires_twofactor"`
Message string `json:"message"`
OAuth OAuth `json:"transfer_parameters"`
}
// OAuth is what will be used in future requests to specify we are logged in
type OAuth struct {
SteamID mangosteam.SteamID `db:"osteam_id" json:"steamid,string"`
OAuthToken string `json:"auth"`
Token string `json:"token"`
TokenSecure string `json:"token_secure"`
LastSessionID string `json:"webcookie"`
}
const (
// DoLoginURI URL used for login
DoLoginURI string = "/login/dologin"
// IsLoggedInURI URL used to check if user is logged in
IsLoggedInURI string = "/actions/GetNotificationCounts"
)
// ErrTwoFactorNeeded is returned when the user needs to give a two factor code
var ErrTwoFactorNeeded = errors.New("Two factor auth needed")
// ErrEmailAuthNeeded is returned when the login requires an email code
var ErrEmailAuthNeeded = errors.New("Email auth needed")
// ErrCaptchaNeededNeeded is returned when the login requires a captcha
var ErrCaptchaNeededNeeded = errors.New("Captcha needed")
// DoLogin is used to log in the steam account after we got the encrypted password
func DoLogin(
client *http.Client,
username string,
encryptedPassword string,
rsatimestamp string,
emailauthKeyedIn string,
captchaGID string,
captchaKeyedIn string,
twoFactorCode string,
) (OAuth, error) {
var oAuth OAuth
baseURL, _ := url.Parse(mangosteam.BaseSteamWebURL + DoLoginURI)
// default value set to -1
if captchaGID == "" {
captchaGID = "-1"
}
// adding query params
params := url.Values{}
params.Add("password", encryptedPassword)
params.Add("username", username)
params.Add("twofactorcode", twoFactorCode)
params.Add("emailauth", emailauthKeyedIn)
params.Add("loginfriendlyname", "")
params.Add("captchagid", captchaGID)
params.Add("captcha_text", captchaKeyedIn)
params.Add("emailsteamid", "")
params.Add("rsatimestamp", rsatimestamp)
baseURL.RawQuery = params.Encode()
req, err := http.NewRequest("POST", baseURL.String(), nil)
resp, err := client.Do(req)
if err != nil {
return oAuth, fmt.Errorf("auth DoLogin(): %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return oAuth, fmt.Errorf("auth DoLogin(): bad request %v for %s, ",
resp.Status, username)
}
loginBody := new(doLogInResponse)
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(loginBody)
if err != nil {
return oAuth, fmt.Errorf("auth DoLogin(): %v", err)
}
if loginBody.EmailAuthNeeded {
return oAuth, ErrEmailAuthNeeded
}
if !loginBody.Success {
if loginBody.TwoFactorNeeded {
return oAuth, ErrTwoFactorNeeded
}
return oAuth, fmt.Errorf("auth DoLogin(): unknown error for %s", username)
}
return loginBody.OAuth, nil
}
// IsLoggedIn checks if a user is logged in or not
func IsLoggedIn(client *http.Client) (bool, error) {
resp, err := client.Get(mangosteam.BaseSteamWebURL + IsLoggedInURI)
if err != nil {
return false, fmt.Errorf("auth IsLoggedin(): %v", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return true, nil
}
return false, nil
}