Skip to content

Commit

Permalink
Add scope for AuthentificationInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Sep 18, 2023
1 parent 0db6fc4 commit 2763f11
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 36 deletions.
1 change: 1 addition & 0 deletions libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ set(LIBMAMBA_PUBLIC_HEADERS
${LIBMAMBA_INCLUDE_DIR}/mamba/util/url_manip.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/util/url.hpp
# Implementation of version and matching specs
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/authentification_info.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/archive.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/platform.hpp
${LIBMAMBA_INCLUDE_DIR}/mamba/specs/conda_url.hpp
Expand Down
22 changes: 3 additions & 19 deletions libmamba/include/mamba/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mamba/core/mamba_fs.hpp"
#include "mamba/core/palette.hpp"
#include "mamba/core/tasksync.hpp"
#include "mamba/specs/authentification_info.hpp"
#include "mamba/specs/platform.hpp"
#include "mamba/version.hpp"

Expand Down Expand Up @@ -46,23 +47,6 @@ namespace mamba
Strict
};

struct BasicHTTPAuthentication
{
std::string user;
std::string password;
};

struct BearerToken
{
std::string token;
};

struct CondaToken
{
std::string token;
};

using AuthenticationInfo = std::variant<BasicHTTPAuthentication, BearerToken, CondaToken>;

class Logger;
class Context;
Expand Down Expand Up @@ -230,7 +214,7 @@ namespace mamba
};

std::string channel_alias = "https://conda.anaconda.org";
using authentication_info_map_t = std::map<std::string, AuthenticationInfo>;
using authentication_info_map_t = std::map<std::string, specs::AuthenticationInfo>;
authentication_info_map_t& authentication_info();
const authentication_info_map_t& authentication_info() const;
std::vector<fs::u8path> token_locations{ "~/.continuum/anaconda-client/tokens" };
Expand Down Expand Up @@ -269,7 +253,7 @@ namespace mamba
bool on_ci = false;

void load_authentication_info();
std::map<std::string, AuthenticationInfo> m_authentication_info;
std::map<std::string, specs::AuthenticationInfo> m_authentication_info;
bool m_authentication_infos_loaded = false;

std::shared_ptr<Logger> logger;
Expand Down
8 changes: 4 additions & 4 deletions libmamba/src/core/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,14 @@ namespace mamba
const auto& authentication_info = m_context.authentication_info();
auto it = authentication_info.find(auth);
if (it != authentication_info.end()
&& std::holds_alternative<CondaToken>(it->second))
&& std::holds_alternative<specs::CondaToken>(it->second))
{
chan.m_token = std::get<CondaToken>(it->second).token;
chan.m_token = std::get<specs::CondaToken>(it->second).token;
break;
}
else if (it != authentication_info.end() && std::holds_alternative<BasicHTTPAuthentication>(it->second))
else if (it != authentication_info.end() && std::holds_alternative<specs::BasicHTTPAuthentication>(it->second))
{
const auto& l_auth = std::get<BasicHTTPAuthentication>(it->second);
const auto& l_auth = std::get<specs::BasicHTTPAuthentication>(it->second);
chan.m_auth = util::concat(
l_auth.user,
l_auth.password.empty() ? "" : ":",
Expand Down
13 changes: 7 additions & 6 deletions libmamba/src/core/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,14 @@ namespace mamba
// cut ".token" ending
token_url = token_url.substr(0, token_url.size() - 6);

m_authentication_info[token_url] = CondaToken{ read_contents(entry.path()) };
m_authentication_info[token_url] = specs::CondaToken{ read_contents(entry.path()
) };
LOG_INFO << "Found token for " << token_url << " at " << entry.path();
}
}
}

std::map<std::string, AuthenticationInfo> res;
std::map<std::string, specs::AuthenticationInfo> res;
fs::u8path auth_loc(mamba::env::home_directory() / ".mamba" / "auth" / "authentication.json");
try
{
Expand All @@ -242,10 +243,10 @@ namespace mamba
{
std::string const host = key;
const auto type = el["type"].get<std::string_view>();
AuthenticationInfo info;
specs::AuthenticationInfo info;
if (type == "CondaToken")
{
info = CondaToken{ el["token"].get<std::string>() };
info = specs::CondaToken{ el["token"].get<std::string>() };
LOG_INFO << "Found token for host " << host
<< " in ~/.mamba/auth/authentication.json";
}
Expand All @@ -255,7 +256,7 @@ namespace mamba
auto pass = decode_base64(el["password"].get<std::string>());
if (pass)
{
info = BasicHTTPAuthentication{
info = specs::BasicHTTPAuthentication{
/* user= */ user,
/* password= */ pass.value(),
};
Expand All @@ -272,7 +273,7 @@ namespace mamba
}
else if (type == "BearerToken")
{
info = BearerToken{ el["token"].get<std::string>() };
info = specs::BearerToken{ el["token"].get<std::string>() };
LOG_INFO << "Found bearer token for host " << host
<< " in ~/.mamba/auth/authentication.json";
}
Expand Down
4 changes: 2 additions & 2 deletions libmamba/src/core/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ namespace mamba
if (context.authentication_info().count(host))
{
const auto& auth = context.authentication_info().at(host);
if (std::holds_alternative<BearerToken>(auth))
if (std::holds_alternative<specs::BearerToken>(auth))
{
m_handle.add_header(
fmt::format("Authorization: Bearer {}", std::get<BearerToken>(auth).token)
fmt::format("Authorization: Bearer {}", std::get<specs::BearerToken>(auth).token)
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions libmamba/src/core/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ namespace mamba
if (m_context.authentication_info().count(host))
{
const auto& auth = m_context.authentication_info().at(host);
if (std::holds_alternative<BearerToken>(auth))
if (std::holds_alternative<specs::BearerToken>(auth))
{
m_curl_handle->add_header(
fmt::format("Authorization: Bearer {}", std::get<BearerToken>(auth).token)
fmt::format("Authorization: Bearer {}", std::get<specs::BearerToken>(auth).token)
);
}
}
Expand Down
8 changes: 5 additions & 3 deletions libmamba/tests/src/core/test_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ namespace mamba
TEST_CASE("add_token")
{
auto& ctx = mambatests::context();
ctx.authentication_info()["conda.anaconda.org"] = CondaToken{ "my-12345-token" };
ctx.authentication_info()["conda.anaconda.org"] = specs::CondaToken{ "my-12345-token" };

ChannelContext channel_context{ ctx };

Expand All @@ -503,8 +503,10 @@ namespace mamba
TEST_CASE("add_multiple_tokens")
{
auto& ctx = mambatests::context();
ctx.authentication_info()["conda.anaconda.org"] = CondaToken{ "base-token" };
ctx.authentication_info()["conda.anaconda.org/conda-forge"] = CondaToken{ "channel-token" };
ctx.authentication_info()["conda.anaconda.org"] = specs::CondaToken{ "base-token" };
ctx.authentication_info()["conda.anaconda.org/conda-forge"] = specs::CondaToken{
"channel-token"
};

ChannelContext channel_context{ ctx };

Expand Down

0 comments on commit 2763f11

Please sign in to comment.