private MSession GetLocalToken() { MSession session; if (!File.Exists(TokenFile)) //로그인 정보가 없을경우 { var ClientToken = Guid.NewGuid().ToString().Replace("-", ""); //새로운 클라이언트 토큰 생성 session = MSession.createEmpty(); session.ClientToken = ClientToken; WriteLogin(session); } else { //로그인 정보가 남아있을경우 클라이언트 토큰 불러옴 var filedata = File.ReadAllText(TokenFile, Encoding.UTF8); try { var job = JObject.Parse(filedata); session = new MSession(); session.AccessToken = job["session"]?.ToString(); session.UUID = job["uuid"]?.ToString(); session.Username = job["username"]?.ToString(); session.ClientToken = job["clientToken"]?.ToString(); } catch (Newtonsoft.Json.JsonReaderException) // if JSON file isn't vaild { DeleteTokenFile(); session = GetLocalToken(); } } return(session); }
internal static MSession createEmpty() { var session = new MSession(); session.Username = ""; session.AccessToken = ""; session.UUID = ""; session.ClientToken = ""; return(session); }
public static MSession GetOfflineSession(string username) { var login = new MSession(); login.Username = username; login.AccessToken = "access_token"; login.UUID = "user_uuid"; login.Result = MLoginResult.Success; login.Message = ""; login.ClientToken = ""; return(login); }
public void Invalidate() { var result = new MSession(); var session = GetLocalToken(); var job = new JObject(); job.Add("accessToken", session.AccessToken); job.Add("clientToken", session.ClientToken); mojangRequest("signout", job.ToString()); }
public MSession Refresh() //엑세스 토큰 새로가져오기 { var result = new MSession(); try { var session = GetLocalToken(); JObject selectedProfile = new JObject(); selectedProfile.Add("id", session.UUID); //uuid 추가 selectedProfile.Add("name", session.Username); //유저네임 추가 JObject req = new JObject { { "accessToken", session.AccessToken }, //새로고침할 토큰 (오래된 토큰) { "clientToken", session.ClientToken }, //클라이언트 고유 토큰 { "selectedProfile", selectedProfile } ////// }; var resHeader = mojangRequest("refresh", req.ToString()); //응답 가져오기 using (var res = new StreamReader(resHeader.GetResponseStream())) { var response = res.ReadToEnd(); result._RawResponse = response; JObject job = JObject.Parse(response); //json 파일 파싱 result.AccessToken = job["accessToken"].ToString(); result.AccessToken = job["accessToken"].ToString(); result.UUID = job["selectedProfile"]["id"].ToString(); result.Username = job["selectedProfile"]["name"].ToString(); result.ClientToken = session.ClientToken; WriteLogin(result); //로그인 정보 쓰기 result.Result = MLoginResult.Success; } } catch { result.Result = MLoginResult.UnknownError; } return(result); }
public MSession Refresh() { var result = new MSession(); try { var session = GetLocalToken(); var req = new JObject { { "accessToken", session.AccessToken }, { "clientToken", session.ClientToken }, { "selectedProfile", new JObject() { { "id", session.UUID }, { "name", session.Username } } } }; var resHeader = mojangRequest("refresh", req.ToString()); using (var res = new StreamReader(resHeader.GetResponseStream())) { var response = res.ReadToEnd(); result._RawResponse = response; JObject job = JObject.Parse(response); result.AccessToken = job["accessToken"].ToString(); result.AccessToken = job["accessToken"].ToString(); result.UUID = job["selectedProfile"]["id"].ToString(); result.Username = job["selectedProfile"]["name"].ToString(); result.ClientToken = session.ClientToken; WriteLogin(result); result.Result = MLoginResult.Success; } } catch { result.Result = MLoginResult.UnknownError; } return(result); }
public MSession Validate() //현재 토큰이 유효한 토큰인지 검사 { //(오래된 토큰은 사용불가능하므로 RefreshLogin 메서드로 새로가져와야함) var result = new MSession(); try { var session = GetLocalToken(); JObject job = new JObject(); job.Add("accessToken", session.AccessToken); job.Add("clientToken", session.ClientToken); var resHeader = mojangRequest("validate", job.ToString()); //리스폰 데이터 받아오기 using (var res = new StreamReader(resHeader.GetResponseStream())) { int ResultCode = (int)((HttpWebResponse)resHeader).StatusCode; if (ResultCode == 204) { result.Result = MLoginResult.Success; result.AccessToken = session.AccessToken; result.UUID = session.UUID; result.Username = session.Username; result.ClientToken = session.ClientToken; } else { result.Result = MLoginResult.NeedLogin; } } } catch { result.Result = MLoginResult.UnknownError; } return(result); }
public MSession Validate() //현재 토큰이 유효한 토큰인지 검사 { //(오래된 토큰은 사용불가능하므로 RefreshLogin 메서드로 새로가져와야함) var result = new MSession(); try { var session = GetLocalToken(); JObject job = new JObject { { "accessToken", session.AccessToken }, { "clientToken", session.ClientToken } }; var resHeader = mojangRequest("validate", job.ToString()); using (var res = new StreamReader(resHeader.GetResponseStream())) { if (resHeader.StatusCode == HttpStatusCode.NoContent) // StatusCode == 204 { result.Result = MLoginResult.Success; result.AccessToken = session.AccessToken; result.UUID = session.UUID; result.Username = session.Username; result.ClientToken = session.ClientToken; } else { result.Result = MLoginResult.NeedLogin; } } } catch { result.Result = MLoginResult.UnknownError; } return(result); }
private void WriteLogin(MSession result) { WriteLogin(result.Username, result.AccessToken, result.UUID, result.ClientToken); }
public MSession Authenticate(string id, string pw) //로그인 { try { MSession result = new MSession(); string ClientToken = GetLocalToken().ClientToken; string Response = ""; var req = "{ \"agent\" : { \"name\" : \"Minecraft\" , \"version\" : 1 }, \"username\" : \"" + id + "\", \"password\" : \"" + pw + "\", \"clientToken\" : \"" + ClientToken + "\"}"; var resHeader = mojangRequest("authenticate", req); using (var res = new StreamReader(resHeader.GetResponseStream())) { Response = res.ReadToEnd(); //받아옴 result._RawResponse = Response; if ((int)(resHeader).StatusCode == 200) //ResultCode 가 200 (성공) 일때만 { var jObj = JObject.Parse(Response); //json 파싱 result.AccessToken = jObj["accessToken"].ToString(); result.AccessToken = jObj["accessToken"].ToString(); result.UUID = jObj["selectedProfile"]["id"].ToString(); result.Username = jObj["selectedProfile"]["name"].ToString(); result.ClientToken = ClientToken; WriteLogin(result); //로그인 정보 쓰기 result.Result = MLoginResult.Success; } else //로그인이 실패하였을때 { var json = JObject.Parse(Response); //에러메세지 불러옴 var msg = json["error"]?.ToString(); switch (msg) //메세지를 상황에 맞게 바꿈 { case "Method Not Allowed": case "Not Found": case "Unsupported Media Type": result.Result = MLoginResult.BadRequest; break; case "IllegalArgumentException": case "ForbiddenOperationException": result.Result = MLoginResult.WrongAccount; break; default: result.Result = MLoginResult.UnknownError; break; } } return(result); } } catch //예외발생 { var o = new MSession(); o.Result = MLoginResult.UnknownError; return(o); } }
public MSession Authenticate(string id, string pw) { MSession result = new MSession(); string ClientToken = GetLocalToken().ClientToken; var job = new JObject { { "username", id }, { "password", pw }, { "clientToken", ClientToken }, { "agent", new JObject { { "name", "Minecraft" }, { "version", 1 } } } }; var resHeader = mojangRequest("authenticate", job.ToString()); using (var res = new StreamReader(resHeader.GetResponseStream())) { var Response = res.ReadToEnd(); result.ClientToken = ClientToken; if (resHeader.StatusCode == HttpStatusCode.OK) // ResultCode == 200 { var jObj = JObject.Parse(Response); //json parse result.AccessToken = jObj["accessToken"].ToString(); result.UUID = jObj["selectedProfile"]["id"].ToString(); result.Username = jObj["selectedProfile"]["name"].ToString(); WriteLogin(result); result.Result = MLoginResult.Success; } else // fail to login { var json = JObject.Parse(Response); var error = json["error"]?.ToString(); // error type result._RawResponse = Response; result.Message = json["message"]?.ToString() ?? ""; // detail error message switch (error) { case "Method Not Allowed": case "Not Found": case "Unsupported Media Type": result.Result = MLoginResult.BadRequest; break; case "IllegalArgumentException": case "ForbiddenOperationException": result.Result = MLoginResult.WrongAccount; break; default: result.Result = MLoginResult.UnknownError; break; } } return(result); } }