network, web_service: Add Verification backend and use new lobby API
Added verify_backend to load user_data for members. and removed method to generate UID as this is now done server-side. Added GetUsername function and a "token" param to room_member. Also added a username to ChatEntry, so that the username can be shown (along with nicknames) in the chat dialog.master
parent
5f0e189238
commit
1a8841f96e
@ -0,0 +1,18 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "network/verify_user.h"
|
||||
|
||||
namespace Network::VerifyUser {
|
||||
|
||||
Backend::~Backend() = default;
|
||||
|
||||
NullBackend::~NullBackend() = default;
|
||||
|
||||
UserData NullBackend::LoadUserData([[maybe_unused]] const std::string& verify_UID,
|
||||
[[maybe_unused]] const std::string& token) {
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace Network::VerifyUser
|
@ -0,0 +1,45 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "common/logging/log.h"
|
||||
|
||||
namespace Network::VerifyUser {
|
||||
|
||||
struct UserData {
|
||||
std::string username;
|
||||
std::string display_name;
|
||||
std::string avatar_url;
|
||||
};
|
||||
|
||||
/**
|
||||
* A backend used for verifying users and loading user data.
|
||||
*/
|
||||
class Backend {
|
||||
public:
|
||||
virtual ~Backend();
|
||||
|
||||
/**
|
||||
* Verifies the given token and loads the information into a UserData struct.
|
||||
* @param verify_UID A GUID that may be used for verification.
|
||||
* @param token A token that contains user data and verification data. The format and content is
|
||||
* decided by backends.
|
||||
*/
|
||||
virtual UserData LoadUserData(const std::string& verify_UID, const std::string& token) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* A null backend where the token is ignored.
|
||||
* No verification is performed here and the function returns an empty UserData.
|
||||
*/
|
||||
class NullBackend final : public Backend {
|
||||
public:
|
||||
~NullBackend();
|
||||
|
||||
UserData LoadUserData(const std::string& verify_UID, const std::string& token) override;
|
||||
};
|
||||
|
||||
} // namespace Network::VerifyUser
|
@ -0,0 +1,56 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <system_error>
|
||||
#include <jwt/jwt.hpp>
|
||||
#include "common/logging/log.h"
|
||||
#include "common/web_result.h"
|
||||
#include "web_service/verify_user_jwt.h"
|
||||
#include "web_service/web_backend.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
static std::string public_key;
|
||||
std::string GetPublicKey(const std::string& host) {
|
||||
if (public_key.empty()) {
|
||||
Client client(host, "", ""); // no need for credentials here
|
||||
public_key = client.GetJson("/jwt/external/key.pem", true).returned_data;
|
||||
if (public_key.empty()) {
|
||||
LOG_ERROR(WebService, "Could not fetch external JWT public key, verification may fail");
|
||||
} else {
|
||||
LOG_INFO(WebService, "Fetched external JWT public key (size={})", public_key.size());
|
||||
}
|
||||
}
|
||||
return public_key;
|
||||
}
|
||||
|
||||
VerifyUserJWT::VerifyUserJWT(const std::string& host) : pub_key(GetPublicKey(host)) {}
|
||||
|
||||
Network::VerifyUser::UserData VerifyUserJWT::LoadUserData(const std::string& verify_UID,
|
||||
const std::string& token) {
|
||||
const std::string audience = fmt::format("external-{}", verify_UID);
|
||||
using namespace jwt::params;
|
||||
std::error_code error;
|
||||
auto decoded =
|
||||
jwt::decode(token, algorithms({"rs256"}), error, secret(pub_key), issuer("citra-core"),
|
||||
aud(audience), validate_iat(true), validate_jti(true));
|
||||
if (error) {
|
||||
LOG_INFO(WebService, "Verification failed: category={}, code={}, message={}",
|
||||
error.category().name(), error.value(), error.message());
|
||||
return {};
|
||||
}
|
||||
Network::VerifyUser::UserData user_data{};
|
||||
if (decoded.payload().has_claim("username")) {
|
||||
user_data.username = decoded.payload().get_claim_value<std::string>("username");
|
||||
}
|
||||
if (decoded.payload().has_claim("displayName")) {
|
||||
user_data.display_name = decoded.payload().get_claim_value<std::string>("displayName");
|
||||
}
|
||||
if (decoded.payload().has_claim("avatarUrl")) {
|
||||
user_data.avatar_url = decoded.payload().get_claim_value<std::string>("avatarUrl");
|
||||
}
|
||||
return user_data;
|
||||
}
|
||||
|
||||
} // namespace WebService
|
@ -0,0 +1,25 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include "network/verify_user.h"
|
||||
#include "web_service/web_backend.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
class VerifyUserJWT final : public Network::VerifyUser::Backend {
|
||||
public:
|
||||
VerifyUserJWT(const std::string& host);
|
||||
~VerifyUserJWT() = default;
|
||||
|
||||
Network::VerifyUser::UserData LoadUserData(const std::string& verify_UID,
|
||||
const std::string& token) override;
|
||||
|
||||
private:
|
||||
std::string pub_key;
|
||||
};
|
||||
|
||||
} // namespace WebService
|
Loading…
Reference in New Issue