mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Microsoft Sign-in: Add refresh token support (#1838)
This commit is contained in:
parent
e68a51dcff
commit
4b8ca158a8
3 changed files with 26 additions and 21 deletions
|
|
@ -211,7 +211,14 @@ namespace MinecraftClient
|
|||
if (result != ProtocolHandler.LoginResult.Success)
|
||||
{
|
||||
Translations.WriteLineFormatted("mcc.session_invalid");
|
||||
if (Settings.Password == "" && Settings.AccountType == ProtocolHandler.AccountType.Mojang)
|
||||
// Try to refresh access token
|
||||
if (!string.IsNullOrWhiteSpace(session.RefreshToken))
|
||||
{
|
||||
result = ProtocolHandler.MicrosoftLoginRefresh(session.RefreshToken, out session);
|
||||
}
|
||||
if (result != ProtocolHandler.LoginResult.Success
|
||||
&& Settings.Password == ""
|
||||
&& Settings.AccountType == ProtocolHandler.AccountType.Mojang)
|
||||
RequestPassword();
|
||||
}
|
||||
else ConsoleIO.WriteLineFormatted(Translations.Get("mcc.session_valid", session.PlayerName));
|
||||
|
|
@ -221,13 +228,13 @@ namespace MinecraftClient
|
|||
{
|
||||
Translations.WriteLine("mcc.connecting", Settings.AccountType == ProtocolHandler.AccountType.Mojang ? "Minecraft.net" : "Microsoft");
|
||||
result = ProtocolHandler.GetLogin(Settings.Login, Settings.Password, Settings.AccountType, out session);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success && Settings.SessionCaching != CacheType.None)
|
||||
{
|
||||
SessionCache.Store(Settings.Login.ToLower(), session);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -504,20 +504,13 @@ namespace MinecraftClient.Protocol
|
|||
string code = ConsoleIO.ReadLine();
|
||||
|
||||
var msaResponse = Microsoft.RequestAccessToken(code);
|
||||
try
|
||||
{
|
||||
return MicrosoftLogin(msaResponse, out session);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
public static LoginResult MicrosoftLoginRefresh(string refreshToken, out SessionToken session)
|
||||
{
|
||||
session = new SessionToken() { ClientID = Guid.NewGuid().ToString().Replace("-", "") };
|
||||
ConsoleIO.WriteLineFormatted("§cMicrosoft authenticate failed: " + e.Message);
|
||||
if (Settings.DebugMessages)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§c" + e.StackTrace);
|
||||
}
|
||||
return LoginResult.WrongPassword; // Might not always be wrong password
|
||||
}
|
||||
var msaResponse = Microsoft.RefreshAccessToken(refreshToken);
|
||||
return MicrosoftLogin(msaResponse, out session);
|
||||
}
|
||||
|
||||
private static LoginResult MicrosoftLogin(Microsoft.LoginResponse msaResponse, out SessionToken session)
|
||||
|
|
@ -539,6 +532,7 @@ namespace MinecraftClient.Protocol
|
|||
session.PlayerName = profile.UserName;
|
||||
session.PlayerID = profile.UUID;
|
||||
session.ID = accessToken;
|
||||
session.RefreshToken = msaResponse.RefreshToken;
|
||||
Settings.Login = msaResponse.Email;
|
||||
return LoginResult.Success;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ namespace MinecraftClient.Protocol.Session
|
|||
public string PlayerName { get; set; }
|
||||
public string PlayerID { get; set; }
|
||||
public string ClientID { get; set; }
|
||||
public string RefreshToken { get; set; }
|
||||
|
||||
public SessionToken()
|
||||
{
|
||||
|
|
@ -21,11 +22,12 @@ namespace MinecraftClient.Protocol.Session
|
|||
PlayerName = String.Empty;
|
||||
PlayerID = String.Empty;
|
||||
ClientID = String.Empty;
|
||||
RefreshToken = String.Empty;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Join(",", ID, PlayerName, PlayerID, ClientID);
|
||||
return String.Join(",", ID, PlayerName, PlayerID, ClientID, RefreshToken);
|
||||
}
|
||||
|
||||
public static SessionToken FromString(string tokenString)
|
||||
|
|
@ -39,6 +41,7 @@ namespace MinecraftClient.Protocol.Session
|
|||
session.PlayerName = fields[1];
|
||||
session.PlayerID = fields[2];
|
||||
session.ClientID = fields[3];
|
||||
session.RefreshToken = fields[4];
|
||||
|
||||
Guid temp;
|
||||
if (!JwtRegex.IsMatch(session.ID))
|
||||
|
|
@ -49,6 +52,7 @@ namespace MinecraftClient.Protocol.Session
|
|||
throw new InvalidDataException("Invalid player ID");
|
||||
if (!Guid.TryParseExact(session.ClientID, "N", out temp))
|
||||
throw new InvalidDataException("Invalid client ID");
|
||||
// No validation on refresh token because it is custom format token (not Jwt)
|
||||
|
||||
return session;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue