Skip to content

Commit 8e630ad

Browse files
UgnineSirdisGazizonoki
authored andcommitted
Moved "Implement OAuth 2.0 Token Exchange credentials provider in C++ SDK" commit from ydb repo
1 parent 411f8ba commit 8e630ad

File tree

11 files changed

+1408
-3
lines changed

11 files changed

+1408
-3
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#pragma once
2+
3+
#include <ydb-cpp-sdk/client/types/credentials/credentials.h>
4+
#include <ydb-cpp-sdk/client/types/fluent_settings_helpers.h>
5+
6+
#include <ydb-cpp-sdk/util/datetime/base.h>
7+
8+
#include <string>
9+
10+
namespace NYdb {
11+
12+
class ITokenSource {
13+
public:
14+
struct TToken {
15+
std::string Token;
16+
17+
// token type according to OAuth 2.0 token exchange protocol
18+
// https://www.rfc-editor.org/rfc/rfc8693#TokenTypeIdentifiers
19+
// for example urn:ietf:params:oauth:token-type:jwt
20+
std::string TokenType;
21+
};
22+
23+
virtual ~ITokenSource() = default;
24+
virtual TToken GetToken() const = 0;
25+
};
26+
27+
std::shared_ptr<ITokenSource> CreateFixedTokenSource(const std::string& token, const std::string& tokenType);
28+
29+
#define FLUENT_SETTING_VECTOR_OR_SINGLE(type, name) \
30+
FLUENT_SETTING_VECTOR(type, name); \
31+
TSelf& name(const type& value) { \
32+
name##_.resize(1); \
33+
name##_[0] = value; \
34+
return static_cast<TSelf&>(*this); \
35+
}
36+
37+
struct TOauth2TokenExchangeParams {
38+
using TSelf = TOauth2TokenExchangeParams;
39+
40+
FLUENT_SETTING(std::string, TokenEndpoint);
41+
42+
FLUENT_SETTING_DEFAULT(std::string, GrantType, "urn:ietf:params:oauth:grant-type:token-exchange");
43+
44+
FLUENT_SETTING(std::string, Resource);
45+
FLUENT_SETTING_VECTOR_OR_SINGLE(std::string, Audience);
46+
FLUENT_SETTING_VECTOR_OR_SINGLE(std::string, Scope);
47+
48+
FLUENT_SETTING_DEFAULT(std::string, RequestedTokenType, "urn:ietf:params:oauth:token-type:access_token");
49+
50+
FLUENT_SETTING(std::shared_ptr<ITokenSource>, SubjectTokenSource);
51+
FLUENT_SETTING(std::shared_ptr<ITokenSource>, ActorTokenSource);
52+
53+
FLUENT_SETTING_DEFAULT(TDuration, SocketTimeout, TDuration::Seconds(5));
54+
FLUENT_SETTING_DEFAULT(TDuration, ConnectTimeout, TDuration::Seconds(30));
55+
FLUENT_SETTING_DEFAULT(TDuration, SyncUpdateTimeout, TDuration::Seconds(20));
56+
};
57+
58+
// Creates OAuth 2.0 token exchange credentials provider factory that exchanges token using standard protocol
59+
// https://www.rfc-editor.org/rfc/rfc8693
60+
std::shared_ptr<ICredentialsProviderFactory> CreateOauth2TokenExchangeCredentialsProviderFactory(const TOauth2TokenExchangeParams& params);
61+
62+
} // namespace NYdb
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include "credentials.h"
4+
5+
#include <ydb-cpp-sdk/client/types/fluent_settings_helpers.h>
6+
7+
#include <ydb-cpp-sdk/util/datetime/base.h>
8+
9+
namespace NYdb {
10+
11+
constexpr TDuration DEFAULT_JWT_TOKEN_TTL = TDuration::Hours(1);
12+
13+
struct TJwtTokenSourceParams {
14+
using TSelf = TJwtTokenSourceParams;
15+
16+
FLUENT_SETTING(std::string, KeyId);
17+
18+
template <class TAlg, class... T>
19+
TSelf& SigningAlgorithm(T&&... args) {
20+
SigningAlgorithm_ = std::make_shared<TJwtSigningAlgorithm<TAlg>>(std::forward<T>(args)...);
21+
return *this;
22+
}
23+
24+
// JWT Claims
25+
FLUENT_SETTING(std::string, Issuer);
26+
FLUENT_SETTING(std::string, Subject);
27+
FLUENT_SETTING(std::string, Id);
28+
FLUENT_SETTING_VECTOR_OR_SINGLE(std::string, Audience);
29+
30+
FLUENT_SETTING_DEFAULT(TDuration, TokenTtl, DEFAULT_JWT_TOKEN_TTL);
31+
32+
33+
// Helpers
34+
class ISigningAlgorithm {
35+
public:
36+
virtual ~ISigningAlgorithm() = default;
37+
virtual std::string sign(const std::string& data, std::error_code& ec) const = 0;
38+
virtual std::string name() const = 0;
39+
};
40+
41+
// Interface implementation for jwt-cpp algorithm classes
42+
template <class TAlg>
43+
class TJwtSigningAlgorithm: public ISigningAlgorithm {
44+
public:
45+
template <class... T>
46+
explicit TJwtSigningAlgorithm(T&&... args)
47+
: Alg(std::forward<T>(args)...)
48+
{
49+
}
50+
51+
std::string sign(const std::string& data, std::error_code& ec) const override {
52+
return Alg.sign(data, ec);
53+
}
54+
55+
std::string name() const override {
56+
return Alg.name();
57+
}
58+
59+
private:
60+
TAlg Alg;
61+
};
62+
63+
std::shared_ptr<ISigningAlgorithm> SigningAlgorithm_;
64+
};
65+
66+
std::shared_ptr<ITokenSource> CreateJwtTokenSource(const TJwtTokenSourceParams& params);
67+
68+
} // namespace NYdb

src/client/types/credentials/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_subdirectory(login)
2+
add_subdirectory(oauth2_token_exchange)
23

34
add_library(client-ydb_types-credentials)
45

@@ -13,5 +14,3 @@ target_link_libraries(client-ydb_types-credentials PUBLIC
1314
target_sources(client-ydb_types-credentials PRIVATE
1415
credentials.cpp
1516
)
16-
17-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
add_library(ydb_types-credentials-oauth2)
2+
3+
target_link_libraries(ydb_types-credentials-oauth2 PUBLIC
4+
yutil
5+
jwt-cpp::jwt-cpp
6+
library-cpp-cgiparam
7+
cpp-http-misc
8+
cpp-http-simple
9+
library-cpp-json
10+
library-cpp-retry
11+
library-cpp-uri
12+
client-ydb_types-credentials
13+
cpp-client-ydb_types
14+
)
15+
16+
target_sources(ydb_types-credentials-oauth2 PRIVATE
17+
credentials.cpp
18+
jwt_token_source.cpp
19+
)

0 commit comments

Comments
 (0)