public MLoginResponse Refresh(MSession session) { JObject req = new JObject { { "accessToken", session.AccessToken }, { "clientToken", session.ClientToken }, { "selectedProfile", new JObject { { "id", session.UUID }, { "name", session.Username } } } }; HttpWebResponse resHeader = mojangRequest("refresh", req.ToString()); using (StreamReader res = new StreamReader(resHeader.GetResponseStream() !)) { string rawResponse = res.ReadToEnd(); if ((int)resHeader.StatusCode / 100 == 2) { return(parseSession(rawResponse, session.ClientToken)); } else { return(errorHandle(rawResponse)); } } }
public static MSession GetOfflineSession(string username) { var login = new MSession(); login.Username = username; login.AccessToken = "access_token"; login.UUID = "user_uuid"; login.ClientToken = null; return(login); }
private MSession createNewSession() { var session = new MSession(); if (SaveSession) { session.ClientToken = CreateNewClientToken(); writeSessionCache(session); } return(session); }
public bool Invalidate(MSession session) { JObject job = new JObject { { "accessToken", session.AccessToken }, { "clientToken", session.ClientToken } }; HttpWebResponse res = mojangRequest("invalidate", job.ToString()); return(res.StatusCode == HttpStatusCode.NoContent); // 204 }
private void WriteSessionCache(MSession session) { if (!SaveSession) { return; } Directory.CreateDirectory(Path.GetDirectoryName(SessionCacheFilePath)); var json = JsonConvert.SerializeObject(session); File.WriteAllText(SessionCacheFilePath, json, Encoding.UTF8); }
public MLoginResponse TryAutoLogin(MSession session) { try { MLoginResponse result = Validate(session); if (result.Result != MLoginResult.Success) { result = Refresh(session); } return(result); } catch (Exception ex) { return(new MLoginResponse(MLoginResult.UnknownError, null, ex.ToString(), null)); } }
private void writeSessionCache(MSession session) { if (!SaveSession) { return; } var directoryPath = Path.GetDirectoryName(SessionCacheFilePath); if (!string.IsNullOrEmpty(directoryPath)) { Directory.CreateDirectory(directoryPath); } var json = JsonConvert.SerializeObject(session); File.WriteAllText(SessionCacheFilePath, json, Encoding.UTF8); }
public MLoginResponse Validate(MSession session) { JObject req = new JObject { { "accessToken", session.AccessToken }, { "clientToken", session.ClientToken } }; HttpWebResponse resHeader = mojangRequest("validate", req.ToString()); if (resHeader.StatusCode == HttpStatusCode.NoContent) // StatusCode == 204 { return(new MLoginResponse(MLoginResult.Success, session, null, null)); } else { return(new MLoginResponse(MLoginResult.NeedLogin, null, null, null)); } }
private MLoginResponse parseSession(string json, string?clientToken) { var job = JObject.Parse(json); //json parse var profile = job["selectedProfile"]; if (profile == null) { return(new MLoginResponse(MLoginResult.NoProfile, null, null, json)); } else { var session = new MSession { AccessToken = job["accessToken"]?.ToString(), UUID = profile["id"]?.ToString(), Username = profile["name"]?.ToString(), ClientToken = clientToken }; writeSessionCache(session); return(new MLoginResponse(MLoginResult.Success, session, null, null)); } }
public bool Invalidate() { MSession session = ReadSessionCache(); return(Invalidate(session)); }
public MLoginResponse Validate() { MSession session = ReadSessionCache(); return(Validate(session)); }
public MLoginResponse Refresh() { MSession session = ReadSessionCache(); return(Refresh(session)); }
public MLoginResponse TryAutoLogin() { MSession session = ReadSessionCache(); return(TryAutoLogin(session)); }