Microsoft Sign-in: Add refresh token support (#1838)

This commit is contained in:
ReinforceZwei 2021-12-16 15:53:31 +08:00 committed by GitHub
parent e68a51dcff
commit 4b8ca158a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 21 deletions

View file

@ -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,14 +228,14 @@ 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 && Settings.SessionCaching != CacheType.None)
{
SessionCache.Store(Settings.Login.ToLower(), session);
}
if (result == ProtocolHandler.LoginResult.Success)
{
Settings.Username = session.PlayerName;

View file

@ -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)
{
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
}
return MicrosoftLogin(msaResponse, out session);
}
public static LoginResult MicrosoftLoginRefresh(string refreshToken, out SessionToken session)
{
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;
}

View file

@ -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;
}