示例#1
0
        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);
        }
示例#3
0
        private MSession createNewSession()
        {
            var session = new MSession();

            if (SaveSession)
            {
                session.ClientToken = CreateNewClientToken();
                writeSessionCache(session);
            }
            return(session);
        }
示例#4
0
        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);
        }
示例#6
0
 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));
     }
 }
示例#7
0
        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);
        }
示例#8
0
        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));
            }
        }
示例#9
0
        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));
            }
        }
示例#10
0
        public bool Invalidate()
        {
            MSession session = ReadSessionCache();

            return(Invalidate(session));
        }
示例#11
0
        public MLoginResponse Validate()
        {
            MSession session = ReadSessionCache();

            return(Validate(session));
        }
示例#12
0
        public MLoginResponse Refresh()
        {
            MSession session = ReadSessionCache();

            return(Refresh(session));
        }
示例#13
0
        public MLoginResponse TryAutoLogin()
        {
            MSession session = ReadSessionCache();

            return(TryAutoLogin(session));
        }