예제 #1
0
        public MSession GetLocalToken()
        {
            MSession session;

            if (!File.Exists(TokenFile))                                      // no session data
            {
                var ClientToken = Guid.NewGuid().ToString().Replace("-", ""); // create new clienttoken

                session             = MSession.createEmpty();
                session.ClientToken = ClientToken;

                WriteLogin(session);
            }
            else // exists session data
            {
                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);
        }
예제 #2
0
        internal static MSession createEmpty()
        {
            var session = new MSession();

            session.Username    = "";
            session.AccessToken = "";
            session.UUID        = "";
            session.ClientToken = "";
            return(session);
        }
예제 #3
0
        public MSession offline(string user)
        {
            MSession result = MSession.GetOfflineSession(user);
            MSession token  = GetLocalToken();

            result.ClientToken = token.ClientToken;
            WriteLogin(result);

            return(result);
        }
예제 #4
0
        public static MSession GetOfflineSession(string username)
        {
            var login = new MSession();

            login.Username    = username;
            login.AccessToken = "Offline";
            login.UUID        = "Offline";
            login.Result      = MLoginResult.Success;
            login.Message     = "";
            login.ClientToken = "";
            return(login);
        }
예제 #5
0
        public MSession Refresh()
        {
            var result = new MSession();

            try
            {
                var session = GetLocalToken();

                if (session.AccessToken == "")
                {
                    result.Result = MLoginResult.NeedLogin;

                    return(result);
                }

                if (session.AccessToken == "Offline")
                {
                    result.Result = MLoginResult.Success;

                    return(result);
                }

                var req = new JObject
                {
                    { "accessToken", session.AccessToken },
                    { "clientToken", session.ClientToken }
                };

                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);
        }
예제 #6
0
        public MSession Validate()
        {
            var result = new MSession();

            try
            {
                var session = GetLocalToken();

                if (session.AccessToken == "Offline")
                {
                    return(session);
                }

                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);
        }
예제 #7
0
 private void WriteLogin(MSession result)
 {
     WriteLogin(result.Username, result.AccessToken, result.UUID, result.ClientToken);
 }
예제 #8
0
        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);
            }
        }