-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathauth_managers.cc
More file actions
103 lines (85 loc) · 3.59 KB
/
auth_managers.cc
File metadata and controls
103 lines (85 loc) · 3.59 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
/*
* 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_managers.h"
#include <unordered_set>
#include "iceberg/catalog/rest/auth/auth_manager_internal.h"
#include "iceberg/catalog/rest/auth/auth_properties.h"
#include "iceberg/util/string_util.h"
namespace iceberg::rest::auth {
namespace {
/// \brief Registry type for AuthManager factories with heterogeneous lookup support.
using AuthManagerRegistry =
std::unordered_map<std::string, AuthManagerFactory, StringHash, StringEqual>;
const std::unordered_set<std::string, StringHash, StringEqual>& KnownAuthTypes() {
static const std::unordered_set<std::string, StringHash, StringEqual> kAuthTypes = {
AuthProperties::kAuthTypeNone,
AuthProperties::kAuthTypeBasic,
AuthProperties::kAuthTypeOAuth2,
AuthProperties::kAuthTypeSigV4,
};
return kAuthTypes;
}
// Infer the authentication type from properties.
std::string InferAuthType(
const std::unordered_map<std::string, std::string>& properties) {
// Deprecated alias: rest.sigv4-enabled=true forces SigV4.
if (auto it = properties.find(AuthProperties::kSigV4Enabled);
it != properties.end() && StringUtils::EqualsIgnoreCase(it->second, "true")) {
return AuthProperties::kAuthTypeSigV4;
}
auto it = properties.find(AuthProperties::kAuthType);
if (it != properties.end() && !it->second.empty()) {
return StringUtils::ToLower(it->second);
}
// Infer from OAuth2 properties (credential or token)
bool has_credential = properties.contains(AuthProperties::kCredential.key());
bool has_token = properties.contains(AuthProperties::kToken.key());
if (has_credential || has_token) {
return AuthProperties::kAuthTypeOAuth2;
}
return AuthProperties::kAuthTypeNone;
}
AuthManagerRegistry& GetRegistry() {
static AuthManagerRegistry registry = {
{AuthProperties::kAuthTypeNone, MakeNoopAuthManager},
{AuthProperties::kAuthTypeBasic, MakeBasicAuthManager},
{AuthProperties::kAuthTypeOAuth2, MakeOAuth2Manager},
{AuthProperties::kAuthTypeSigV4, MakeSigV4AuthManager},
};
return registry;
}
} // namespace
void AuthManagers::Register(std::string_view auth_type, AuthManagerFactory factory) {
GetRegistry()[StringUtils::ToLower(auth_type)] = std::move(factory);
}
Result<std::unique_ptr<AuthManager>> AuthManagers::Load(
std::string_view name,
const std::unordered_map<std::string, std::string>& properties) {
std::string auth_type = InferAuthType(properties);
auto& registry = GetRegistry();
auto it = registry.find(auth_type);
if (it == registry.end()) {
if (KnownAuthTypes().contains(auth_type)) {
return NotImplemented("Authentication type '{}' is not yet supported", auth_type);
}
return InvalidArgument("Unknown authentication type: '{}'", auth_type);
}
return it->second(name, properties);
}
} // namespace iceberg::rest::auth