127 lines
3.3 KiB
Go
127 lines
3.3 KiB
Go
package core
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
|
)
|
|
|
|
type SteamSDKAuthInfo struct {
|
|
UserSteamId string `json:"steamid"`
|
|
UserAuthToken string `json:"authtoken"`
|
|
}
|
|
|
|
func (mg *Maingate) platform_steamsdk_authorize(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
brinfo, err := mg.GetUserBrowserInfo(r)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
logger.Error(err)
|
|
return
|
|
}
|
|
|
|
var authinfo SteamSDKAuthInfo
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&authinfo)
|
|
if err != nil {
|
|
logger.Println("authinfo decoding fail:", err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if !*noauth {
|
|
err = authenticateSteamUser(config.SteamPublisherAuthKey, config.SteamAppId, authinfo.UserSteamId, authinfo.UserAuthToken)
|
|
}
|
|
|
|
if err == nil {
|
|
acceestoken_expire_time := time.Date(2999, 1, int(time.January), 0, 0, 0, 0, time.UTC).Unix()
|
|
|
|
var info usertokeninfo
|
|
info.platform = AuthPlatformSteamSDK
|
|
info.userid = authinfo.UserSteamId
|
|
info.token = authinfo.UserAuthToken
|
|
info.brinfo = brinfo
|
|
//info.accesstoken = respReferesh.AccessToken
|
|
info.accesstoken_expire_time = acceestoken_expire_time
|
|
mg.setUserToken(info)
|
|
|
|
params := url.Values{}
|
|
params.Add("id", authinfo.UserSteamId)
|
|
params.Add("authtype", AuthPlatformSteamSDK)
|
|
w.Write([]byte("?" + params.Encode()))
|
|
//http.Redirect(w, r, "actionsquare://login?"+Result, http.StatusSeeOther)
|
|
} else {
|
|
logger.Println(err)
|
|
http.Redirect(w, r, "actionsquare://error", http.StatusSeeOther)
|
|
}
|
|
|
|
}
|
|
|
|
func authenticateSteamUser(pubkey, appid, playerid, ticket string) error {
|
|
// Returns: The user's 64-bit SteamID if the user's ticket is valid
|
|
url := fmt.Sprintf("https://partner.steam-api.com/ISteamUserAuth/AuthenticateUserTicket/v1/?key=%s&appid=%s&ticket=%s", pubkey, appid, ticket)
|
|
resp, e := http.Get(url)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
defer func() {
|
|
io.Copy(io.Discard, resp.Body)
|
|
resp.Body.Close()
|
|
}()
|
|
|
|
body, e := ioutil.ReadAll(resp.Body)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
// fmt.Println(url)
|
|
// fmt.Println(string(body))
|
|
|
|
var doc map[string]interface{}
|
|
if err := json.Unmarshal(body, &doc); err != nil {
|
|
return err
|
|
}
|
|
|
|
if v, ok := doc["response"]; ok {
|
|
response := v.(map[string]interface{})
|
|
if v, ok = response["params"]; ok {
|
|
paramsnode := v.(map[string]interface{})
|
|
if v, ok = paramsnode["result"]; ok {
|
|
if v.(string) == "OK" {
|
|
if v, ok = paramsnode["steamid"]; ok {
|
|
// playerid에는 빌드 구성 suffix가 붙어있는 상태이므로 == 비교가 아니라 HasPrefix로 비교
|
|
if strings.HasPrefix(playerid, v.(string)) {
|
|
return nil
|
|
}
|
|
}
|
|
} else if v.(string) == "Invalid ticket" {
|
|
return errors.New("steam: invalid ticket")
|
|
}
|
|
}
|
|
} else if errdocraw, ok := response["error"]; ok {
|
|
errdoc := errdocraw.(map[string]interface{})
|
|
desc := errdoc["errordesc"].(string)
|
|
return errors.New(desc)
|
|
}
|
|
}
|
|
return errors.New("steam: response is not expected")
|
|
}
|
|
|
|
func (mg *Maingate) platform_steamsdk_getuserinfo(info usertokeninfo) (bool, string, string) {
|
|
|
|
// Steam은 이메일 정보를 받을수 없기 때문에 dummy임시 주소 할당하여 리턴한다.
|
|
dummyEmail := fmt.Sprintf("__dummy_%s@steamtemp__", info.userid)
|
|
return true, info.userid, dummyEmail
|
|
|
|
}
|