Skip to content

Commit

Permalink
It compiles, Yggdrasil login working except error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-goode committed Oct 26, 2024
1 parent 7dbaa89 commit c32aaf2
Show file tree
Hide file tree
Showing 27 changed files with 389 additions and 880 deletions.
2 changes: 2 additions & 0 deletions launcher/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
#include "icons/IconList.h"
#include "net/HttpMetaCache.h"

#include "ui/GuiUtil.h"

#include "java/JavaInstallList.h"

#include "updater/ExternalUpdater.h"
Expand Down
6 changes: 2 additions & 4 deletions launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ set(MINECRAFT_SOURCES
minecraft/auth/MinecraftAccount.h
minecraft/auth/Parsers.cpp
minecraft/auth/Parsers.h
minecraft/auth/Yggdrasil.cpp
minecraft/auth/Yggdrasil.h

minecraft/auth/AuthFlow.cpp
minecraft/auth/AuthFlow.h
Expand All @@ -257,6 +255,8 @@ set(MINECRAFT_SOURCES
minecraft/auth/steps/XboxProfileStep.h
minecraft/auth/steps/XboxUserStep.cpp
minecraft/auth/steps/XboxUserStep.h
minecraft/auth/steps/YggdrasilMinecraftProfileStep.cpp
minecraft/auth/steps/YggdrasilMinecraftProfileStep.h
minecraft/auth/steps/YggdrasilStep.cpp
minecraft/auth/steps/YggdrasilStep.h

Expand Down Expand Up @@ -1071,8 +1071,6 @@ SET(LAUNCHER_SOURCES
ui/dialogs/IconPickerDialog.h
ui/dialogs/ImportResourceDialog.cpp
ui/dialogs/ImportResourceDialog.h
ui/dialogs/LoginDialog.cpp
ui/dialogs/LoginDialog.h
ui/dialogs/AuthlibInjectorLoginDialog.cpp
ui/dialogs/AuthlibInjectorLoginDialog.h
ui/dialogs/MSALoginDialog.cpp
Expand Down
51 changes: 31 additions & 20 deletions launcher/CreateAuthlibInjectorAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
#include <QNetworkRequest>
#include <QStringList>
#include <QUrl>
#include "net/ByteArraySink.h"

#include "Application.h"
#include "BuildConfig.h"

CreateAuthlibInjectorAccount::CreateAuthlibInjectorAccount(QUrl url, MinecraftAccountPtr account, QString username)
: NetRequest(), m_url(url), m_account(account), m_username(username)
{}
: NetRequest(), m_account(account), m_username(username)
{
m_url = url;
m_sink.reset(new Sink(*this));
}

QNetworkReply* CreateAuthlibInjectorAccount::getReply(QNetworkRequest& request)
{
Expand All @@ -43,26 +46,34 @@ CreateAuthlibInjectorAccount::Ptr CreateAuthlibInjectorAccount::make(QUrl url, M
return CreateAuthlibInjectorAccount::Ptr(new CreateAuthlibInjectorAccount(url, account, username));
}

void CreateAuthlibInjectorAccount::downloadFinished()
auto CreateAuthlibInjectorAccount::Sink::init(QNetworkRequest& request) -> Task::State
{
return Task::State::Running;
}

auto CreateAuthlibInjectorAccount::Sink::write(QByteArray& data) -> Task::State
{
return Task::State::Running;
}

auto CreateAuthlibInjectorAccount::Sink::abort() -> Task::State
{
return Task::State::Failed;
}

auto CreateAuthlibInjectorAccount::Sink::finalize(QNetworkReply& reply) -> Task::State
{
if (m_state != State::Failed) {
QVariant header = m_reply->rawHeader("X-Authlib-Injector-API-Location");
if (header.isValid()) {
auto location = header.toString();
m_url = m_url.resolved(location);
} else {
qDebug() << "X-Authlib-Injector-API-Location header not found!";
}
m_account.reset(MinecraftAccount::createFromUsernameAuthlibInjector(m_username, m_url.toString()));
m_state = State::Succeeded;
emit succeeded();
return;
QVariant header = reply.rawHeader("X-Authlib-Injector-API-Location");
QUrl url = m_outer.m_url;
if (header.isValid()) {
auto location = header.toString();
url = url.resolved(location);
} else {
qDebug() << m_reply->readAll();
m_reply.reset();
emitFailed();
return;
qDebug() << "X-Authlib-Injector-API-Location header not found!";
}

m_outer.m_account.reset(MinecraftAccount::createFromUsernameAuthlibInjector(m_outer.m_username, url.toString()));
return Task::State::Succeeded;
}

MinecraftAccountPtr CreateAuthlibInjectorAccount::getAccount()
Expand Down
18 changes: 16 additions & 2 deletions launcher/CreateAuthlibInjectorAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,26 @@ class CreateAuthlibInjectorAccount : public Net::NetRequest {

MinecraftAccountPtr getAccount();

class Sink : public Net::Sink {
public:
Sink(CreateAuthlibInjectorAccount& outer) : m_outer(outer) {}
virtual ~Sink() = default;

public:
auto init(QNetworkRequest& request) -> Task::State override;
auto write(QByteArray& data) -> Task::State override;
auto abort() -> Task::State override;
auto finalize(QNetworkReply& reply) -> Task::State override;
auto hasLocalData() -> bool override { return false; }

private:
CreateAuthlibInjectorAccount& m_outer;
};

protected slots:
virtual QNetworkReply* getReply(QNetworkRequest&) override;
void downloadFinished();

private:
QUrl m_url;
MinecraftAccountPtr m_account;
QString m_username;
};
2 changes: 1 addition & 1 deletion launcher/LaunchController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
*/

#include "LaunchController.h"
#include <meta/Index.h>
#include "Application.h"
#include "launch/steps/PrintServers.h"
#include "minecraft/auth/AccountData.h"
#include "minecraft/auth/AccountList.h"
#include "settings/MissingAuthlibInjectorBehavior.h"
#include "ui/pages/instance/VersionPage.h"

#include "ui/InstanceWindow.h"
#include "ui/MainWindow.h"
Expand Down
2 changes: 1 addition & 1 deletion launcher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int main(int argc, char* argv[])
Q_INIT_RESOURCE(multimc);
Q_INIT_RESOURCE(backgrounds);
Q_INIT_RESOURCE(documents);
Q_INIT_RESOURCE(prismlauncher);
Q_INIT_RESOURCE(fjordlauncher);

Q_INIT_RESOURCE(pe_dark);
Q_INIT_RESOURCE(pe_light);
Expand Down
92 changes: 0 additions & 92 deletions launcher/minecraft/auth/AccountTask.h

This file was deleted.

7 changes: 4 additions & 3 deletions launcher/minecraft/auth/AuthFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
#include "minecraft/auth/steps/XboxAuthorizationStep.h"
#include "minecraft/auth/steps/XboxProfileStep.h"
#include "minecraft/auth/steps/XboxUserStep.h"
#include "minecraft/auth/steps/YggdrasilMinecraftProfileStep.h"
#include "minecraft/auth/steps/YggdrasilStep.h"
#include "tasks/Task.h"

#include "AuthFlow.h"

#include <Application.h>

AuthFlow::AuthFlow(AccountData* data, Action action, QObject* parent) : Task(parent), m_data(data)
AuthFlow::AuthFlow(AccountData* data, Action action, QObject* parent, const std::optional<QString> password) : Task(parent), m_data(data)
{
if (data->type == AccountType::MSA) {
if (action == Action::DeviceCode) {
Expand All @@ -43,8 +44,8 @@ AuthFlow::AuthFlow(AccountData* data, Action action, QObject* parent) : Task(par
m_steps.append(makeShared<MinecraftProfileStep>(m_data));
m_steps.append(makeShared<GetSkinStep>(m_data));
} else if (data->type == AccountType::AuthlibInjector) {
m_steps.append(makeShared<YggdrasilStep>(m_data, QString()));
m_steps.append(makeShared<MinecraftProfileStepMojang>(m_data));
m_steps.append(makeShared<YggdrasilStep>(m_data, password));
m_steps.append(makeShared<YggdrasilMinecraftProfileStep>(m_data));
m_steps.append(makeShared<GetSkinStep>(m_data));
}
changeState(AccountTaskState::STATE_CREATED);
Expand Down
5 changes: 4 additions & 1 deletion launcher/minecraft/auth/AuthFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class AuthFlow : public Task {
public:
enum class Action { Refresh, Login, DeviceCode };

explicit AuthFlow(AccountData* data, Action action = Action::Refresh, QObject* parent = 0);
explicit AuthFlow(AccountData* data,
Action action = Action::Refresh,
QObject* parent = 0,
std::optional<QString> password = std::nullopt);
virtual ~AuthFlow() = default;

void executeTask() override;
Expand Down
13 changes: 2 additions & 11 deletions launcher/minecraft/auth/MinecraftAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ MinecraftAccountPtr MinecraftAccount::loadFromJsonV3(const QJsonObject& json)
return nullptr;
}

MinecraftAccountPtr MinecraftAccount::createFromUsername(const QString& username)
{
auto account = makeShared<MinecraftAccount>();
account->data.type = AccountType::Mojang;
account->data.yggdrasilToken.extra["userName"] = username;
account->data.yggdrasilToken.extra["clientToken"] = QUuid::createUuid().toString().remove(QRegularExpression("[{}-]"));
return account;
}

MinecraftAccountPtr MinecraftAccount::createFromUsernameAuthlibInjector(const QString& username, const QString& authlibInjectorUrl)
{
auto account = makeShared<MinecraftAccount>();
Expand Down Expand Up @@ -143,11 +134,11 @@ QPixmap MinecraftAccount::getFace() const
return skin.scaled(64, 64, Qt::KeepAspectRatio);
}

shared_qobject_ptr<AuthFlow> MinecraftAccount::login(bool useDeviceCode)
shared_qobject_ptr<AuthFlow> MinecraftAccount::login(bool useDeviceCode, std::optional<QString> password)
{
Q_ASSERT(m_currentTask.get() == nullptr);

m_currentTask.reset(new AuthFlow(&data, useDeviceCode ? AuthFlow::Action::DeviceCode : AuthFlow::Action::Login, this));
m_currentTask.reset(new AuthFlow(&data, useDeviceCode ? AuthFlow::Action::DeviceCode : AuthFlow::Action::Login, this, password));
connect(m_currentTask.get(), &Task::succeeded, this, &MinecraftAccount::authSucceeded);
connect(m_currentTask.get(), &Task::failed, this, &MinecraftAccount::authFailed);
connect(m_currentTask.get(), &Task::aborted, this, [this] { authFailed(tr("Aborted")); });
Expand Down
3 changes: 1 addition & 2 deletions launcher/minecraft/auth/MinecraftAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class MinecraftAccount : public QObject, public Usable {
//! Default constructor
explicit MinecraftAccount(QObject* parent = 0);

static MinecraftAccountPtr createFromUsername(const QString& username);
static MinecraftAccountPtr createFromUsernameAuthlibInjector(const QString& username, const QString& authlibInjectorUrl);

static MinecraftAccountPtr createBlankMSA();
Expand All @@ -98,7 +97,7 @@ class MinecraftAccount : public QObject, public Usable {
QJsonObject saveToJson() const;

public: /* manipulation */
shared_qobject_ptr<AuthFlow> login(bool useDeviceCode = false);
shared_qobject_ptr<AuthFlow> login(bool useDeviceCode = false, std::optional<QString> password = std::nullopt);

shared_qobject_ptr<AuthFlow> refresh();

Expand Down
Loading

0 comments on commit c32aaf2

Please sign in to comment.