-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathauth_manager.cc
More file actions
168 lines (144 loc) · 6.79 KB
/
auth_manager.cc
File metadata and controls
168 lines (144 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 "iceberg/catalog/rest/auth/auth_manager.h"
#include <optional>
#include "iceberg/catalog/rest/auth/auth_manager_internal.h"
#include "iceberg/catalog/rest/auth/auth_properties.h"
#include "iceberg/catalog/rest/auth/auth_session.h"
#include "iceberg/catalog/rest/auth/oauth2_util.h"
#include "iceberg/util/macros.h"
#include "iceberg/util/transform_util.h"
namespace iceberg::rest::auth {
Result<std::shared_ptr<AuthSession>> AuthManager::InitSession(
HttpClient& init_client,
const std::unordered_map<std::string, std::string>& properties) {
// By default, use the catalog session for initialization
return CatalogSession(init_client, properties);
}
Result<std::shared_ptr<AuthSession>> AuthManager::ContextualSession(
[[maybe_unused]] const SessionContext& context, std::shared_ptr<AuthSession> parent) {
// By default, return the parent session as-is
return parent;
}
Result<std::shared_ptr<AuthSession>> AuthManager::TableSession(
[[maybe_unused]] const TableIdentifier& table,
[[maybe_unused]] const std::unordered_map<std::string, std::string>& properties,
std::shared_ptr<AuthSession> parent) {
// By default, return the parent session as-is
return parent;
}
/// \brief Authentication manager that performs no authentication.
class NoopAuthManager : public AuthManager {
public:
Result<std::shared_ptr<AuthSession>> CatalogSession(
[[maybe_unused]] HttpClient& client,
[[maybe_unused]] const std::unordered_map<std::string, std::string>& properties)
override {
return AuthSession::MakeDefault({});
}
};
Result<std::unique_ptr<AuthManager>> MakeNoopAuthManager(
[[maybe_unused]] std::string_view name,
[[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) {
return std::make_unique<NoopAuthManager>();
}
/// \brief Authentication manager that performs basic authentication.
class BasicAuthManager : public AuthManager {
public:
Result<std::shared_ptr<AuthSession>> CatalogSession(
[[maybe_unused]] HttpClient& client,
const std::unordered_map<std::string, std::string>& properties) override {
auto username_it = properties.find(AuthProperties::kBasicUsername);
ICEBERG_PRECHECK(username_it != properties.end() && !username_it->second.empty(),
"Missing required property '{}'", AuthProperties::kBasicUsername);
auto password_it = properties.find(AuthProperties::kBasicPassword);
ICEBERG_PRECHECK(password_it != properties.end() && !password_it->second.empty(),
"Missing required property '{}'", AuthProperties::kBasicPassword);
std::string credential = username_it->second + ":" + password_it->second;
return AuthSession::MakeDefault(
{{std::string(kAuthorizationHeader),
"Basic " + TransformUtil::Base64Encode(credential)}});
}
};
Result<std::unique_ptr<AuthManager>> MakeBasicAuthManager(
[[maybe_unused]] std::string_view name,
[[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) {
return std::make_unique<BasicAuthManager>();
}
/// \brief OAuth2 authentication manager.
class OAuth2Manager : public AuthManager {
public:
Result<std::shared_ptr<AuthSession>> InitSession(
HttpClient& init_client,
const std::unordered_map<std::string, std::string>& properties) override {
ICEBERG_ASSIGN_OR_RAISE(auto config, AuthProperties::FromProperties(properties));
// No token refresh during init (short-lived session).
config.Set(AuthProperties::kKeepRefreshed, false);
// Credential takes priority: fetch a fresh token for the config request.
if (!config.credential().empty()) {
auto init_session = AuthSession::MakeDefault(AuthHeaders(config.token()));
ICEBERG_ASSIGN_OR_RAISE(init_token_response_,
FetchToken(init_client, *init_session, config));
return AuthSession::MakeDefault(AuthHeaders(init_token_response_->access_token));
}
if (!config.token().empty()) {
return AuthSession::MakeDefault(AuthHeaders(config.token()));
}
return AuthSession::MakeDefault({});
}
Result<std::shared_ptr<AuthSession>> CatalogSession(
HttpClient& client,
const std::unordered_map<std::string, std::string>& properties) override {
ICEBERG_ASSIGN_OR_RAISE(auto config, AuthProperties::FromProperties(properties));
// Reuse token from init phase.
if (init_token_response_.has_value()) {
auto token_response = std::move(*init_token_response_);
init_token_response_.reset();
return AuthSession::MakeOAuth2(token_response, config.oauth2_server_uri(),
config.client_id(), config.client_secret(),
config.scope(), client);
}
// If token is provided, use it directly.
if (!config.token().empty()) {
return AuthSession::MakeDefault(AuthHeaders(config.token()));
}
// Fetch a new token using client_credentials grant.
if (!config.credential().empty()) {
auto base_session = AuthSession::MakeDefault(AuthHeaders(config.token()));
OAuthTokenResponse token_response;
ICEBERG_ASSIGN_OR_RAISE(token_response, FetchToken(client, *base_session, config));
// TODO(lishuxu): should we directly pass config to the MakeOAuth2 call?
return AuthSession::MakeOAuth2(token_response, config.oauth2_server_uri(),
config.client_id(), config.client_secret(),
config.scope(), client);
}
return AuthSession::MakeDefault({});
}
// TODO(lishuxu): Override TableSession() for token exchange (RFC 8693).
// TODO(lishuxu): Override ContextualSession() for per-context exchange.
private:
/// Cached token from InitSession
std::optional<OAuthTokenResponse> init_token_response_;
};
Result<std::unique_ptr<AuthManager>> MakeOAuth2Manager(
[[maybe_unused]] std::string_view name,
[[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) {
return std::make_unique<OAuth2Manager>();
}
} // namespace iceberg::rest::auth