Skip to content

Commit

Permalink
refactor: move Certificate class definition to a separate source file
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 13, 2024
1 parent 5455e6a commit b497b19
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
28 changes: 18 additions & 10 deletions include/bedrock/certificates/certificate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@

#pragma once

#include "bedrock/certificates/unverified_certificate.h"
#include <memory>

#include "bedrock/certificates/web_token.h"

class UnverifiedCertificate {
friend class Certificate;

WebToken raw_token_; // +0
std::unique_ptr<UnverifiedCertificate> parent_unverified_certificate_; // +128
};

class Certificate {
public:
[[nodiscard]] Json::Value getExtraData(const std::string &key, const Json::Value &default_value) const
{
auto extra_data = unverified_certificate.raw_token.data_info.get("extraData", {});
return extra_data.get(key, default_value);
}
[[nodiscard]] Json::Value getExtraData(const std::string &key, const Json::Value &default_value) const;
[[nodiscard]] bool isValid() const;
[[nodiscard]] bool isSelfSigned() const;

UnverifiedCertificate unverified_certificate; // +0
std::unique_ptr<Certificate> parent; // +136
bool is_valid; // +144
bool unknown; // +145
private:
UnverifiedCertificate unverified_certificate_; // +0
std::unique_ptr<Certificate> parent_; // +136
bool valid_; // +144
bool self_signed_; // +145
};
8 changes: 1 addition & 7 deletions include/bedrock/certificates/extended_certificate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,5 @@

class ExtendedCertificate {
public:
static std::string getXuid(const Certificate &certificate, bool flag)
{
if (!flag && certificate.unknown) {
return "";
}
return certificate.getExtraData("XUID", {}).asString();
}
static std::string getXuid(const Certificate &certificate, bool trust_self_signed);
};
1 change: 0 additions & 1 deletion include/bedrock/network/packet/types/connection_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <string>

#include "bedrock/certificates/certificate.h"
#include "bedrock/certificates/unverified_certificate.h"
#include "bedrock/certificates/web_token.h"
#include "bedrock/deps/jsoncpp/value.h"
#include "bedrock/network/sub_client_id.h"
Expand Down
31 changes: 31 additions & 0 deletions src/endstone_runtime/bedrock/certificates/certificate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "bedrock/certificates/certificate.h"

Json::Value Certificate::getExtraData(const std::string &key, const Json::Value &default_value) const
{
const auto extra_data = unverified_certificate_.raw_token_.data_info.get("extraData", {});
return extra_data.get(key, default_value);
}

bool Certificate::isValid() const
{
return valid_;
}

bool Certificate::isSelfSigned() const
{
return self_signed_;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include "bedrock/certificates/extended_certificate.h"

#include "bedrock/certificates/web_token.h"

class UnverifiedCertificate {
public:
WebToken raw_token; // +0
std::unique_ptr<UnverifiedCertificate> parent; // +128
};
std::string ExtendedCertificate::getXuid(const Certificate &certificate, bool trust_self_signed)
{
if (!trust_self_signed && certificate.isSelfSigned()) {
return "";
}
return certificate.getExtraData("XUID", {}).asString();
}

0 comments on commit b497b19

Please sign in to comment.