|
| 1 | +#include <library/cpp/json/json_reader.h> |
| 2 | +#include <library/cpp/string_utils/base64/base64.h> |
| 3 | +#include <ydb/library/actors/http/http.h> |
| 4 | +#include <ydb/library/security/util.h> |
| 5 | +#include <ydb/mvp/core/mvp_log.h> |
| 6 | +#include <ydb/mvp/core/mvp_tokens.h> |
| 7 | +#include "openid_connect.h" |
| 8 | +#include "oidc_session_create.h" |
| 9 | +#include "oidc_settings.h" |
| 10 | +#include "oidc_impersonate_start_page_nebius.h" |
| 11 | + |
| 12 | +namespace NMVP { |
| 13 | +namespace NOIDC { |
| 14 | + |
| 15 | +THandlerImpersonateStart::THandlerImpersonateStart(const NActors::TActorId& sender, |
| 16 | + const NHttp::THttpIncomingRequestPtr& request, |
| 17 | + const NActors::TActorId& httpProxyId, |
| 18 | + const TOpenIdConnectSettings& settings) |
| 19 | + : Sender(sender) |
| 20 | + , Request(request) |
| 21 | + , HttpProxyId(httpProxyId) |
| 22 | + , Settings(settings) |
| 23 | +{} |
| 24 | + |
| 25 | +TString THandlerImpersonateStart::DecodeToken(const TStringBuf& cookie, const NActors::TActorContext& ctx) { |
| 26 | + TString token; |
| 27 | + try { |
| 28 | + Base64StrictDecode(cookie, token); |
| 29 | + } catch (std::exception& e) { |
| 30 | + LOG_DEBUG_S(ctx, EService::MVP, "Base64Decode " << cookie << " cookie: " << e.what()); |
| 31 | + token.clear(); |
| 32 | + } |
| 33 | + return token; |
| 34 | +} |
| 35 | + |
| 36 | +void THandlerImpersonateStart::Bootstrap(const NActors::TActorContext& ctx) { |
| 37 | + LOG_DEBUG_S(ctx, EService::MVP, "Start impersonation process"); |
| 38 | + NHttp::TUrlParameters urlParameters(Request->URL); |
| 39 | + TString serviceAccountId = urlParameters["service_account_id"]; |
| 40 | + |
| 41 | + NHttp::THeaders headers(Request->Headers); |
| 42 | + NHttp::TCookies cookies(headers.Get("Cookie")); |
| 43 | + |
| 44 | + TString sessionCookieName = CreateNameSessionCookie(Settings.ClientId); |
| 45 | + TStringBuf sessionCookieValue = cookies.Get(sessionCookieName); |
| 46 | + if (!sessionCookieValue.Empty()) { |
| 47 | + LOG_DEBUG_S(ctx, EService::MVP, "Using session cookie (" << sessionCookieName << ": " << NKikimr::MaskTicket(sessionCookieValue) << ")"); |
| 48 | + } |
| 49 | + |
| 50 | + TString sessionToken = DecodeToken(sessionCookieValue, ctx); |
| 51 | + |
| 52 | + TString jsonError; |
| 53 | + if (sessionToken.empty()) { |
| 54 | + jsonError = "Wrong impersonate parameter: session cookie not found"; |
| 55 | + } else if (serviceAccountId.empty()) { |
| 56 | + jsonError = "Wrong impersonate parameter: account_id not found"; |
| 57 | + } |
| 58 | + |
| 59 | + if (jsonError) { |
| 60 | + NHttp::THeadersBuilder responseHeaders; |
| 61 | + responseHeaders.Set("Content-Type", "text/plain"); |
| 62 | + NHttp::THttpOutgoingResponsePtr httpResponse = Request->CreateResponse("400", "Bad Request", responseHeaders, jsonError); |
| 63 | + ctx.Send(Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(httpResponse)); |
| 64 | + Die(ctx); |
| 65 | + } else { |
| 66 | + RequestImpersonatedToken(sessionToken, serviceAccountId, ctx); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void THandlerImpersonateStart::RequestImpersonatedToken(const TString& sessionToken, const TString& serviceAccountId, const NActors::TActorContext& ctx) { |
| 71 | + LOG_DEBUG_S(ctx, EService::MVP, "Request impersonated token"); |
| 72 | + NHttp::THttpOutgoingRequestPtr httpRequest = NHttp::THttpOutgoingRequest::CreateRequestPost(Settings.GetImpersonateEndpointURL()); |
| 73 | + httpRequest->Set<&NHttp::THttpRequest::ContentType>("application/x-www-form-urlencoded"); |
| 74 | + |
| 75 | + TMvpTokenator* tokenator = MVPAppData()->Tokenator; |
| 76 | + TString token = ""; |
| 77 | + if (tokenator) { |
| 78 | + token = tokenator->GetToken(Settings.SessionServiceTokenName); |
| 79 | + } |
| 80 | + httpRequest->Set("Authorization", token); // Bearer included |
| 81 | + |
| 82 | + TStringBuilder body; |
| 83 | + body << "session=" << sessionToken |
| 84 | + << "&service_account_id=" << serviceAccountId; |
| 85 | + httpRequest->Set<&NHttp::THttpRequest::Body>(body); |
| 86 | + |
| 87 | + ctx.Send(HttpProxyId, new NHttp::TEvHttpProxy::TEvHttpOutgoingRequest(httpRequest)); |
| 88 | + Become(&THandlerImpersonateStart::StateWork); |
| 89 | +} |
| 90 | + |
| 91 | +void THandlerImpersonateStart::ProcessImpersonatedToken(const TString& impersonatedToken, const NActors::TActorContext& ctx) { |
| 92 | + TString impersonatedCookieName = CreateNameImpersonatedCookie(Settings.ClientId); |
| 93 | + TString impersonatedCookieValue = Base64Encode(impersonatedToken); |
| 94 | + LOG_DEBUG_S(ctx, EService::MVP, "Set impersonated cookie: (" << impersonatedCookieName << ": " << NKikimr::MaskTicket(impersonatedCookieValue) << ")"); |
| 95 | + |
| 96 | + NHttp::THeadersBuilder responseHeaders; |
| 97 | + responseHeaders.Set("Set-Cookie", CreateSecureCookie(impersonatedCookieName, impersonatedCookieValue)); |
| 98 | + NHttp::THttpOutgoingResponsePtr httpResponse; |
| 99 | + httpResponse = Request->CreateResponse("200", "OK", responseHeaders); |
| 100 | + ctx.Send(Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(httpResponse)); |
| 101 | + Die(ctx); |
| 102 | +} |
| 103 | + |
| 104 | +void THandlerImpersonateStart::Handle(NHttp::TEvHttpProxy::TEvHttpIncomingResponse::TPtr event, const NActors::TActorContext& ctx) { |
| 105 | + NHttp::THttpOutgoingResponsePtr httpResponse; |
| 106 | + if (event->Get()->Error.empty() && event->Get()->Response) { |
| 107 | + NHttp::THttpIncomingResponsePtr response = event->Get()->Response; |
| 108 | + LOG_DEBUG_S(ctx, EService::MVP, "Incoming response from authorization server: " << response->Status); |
| 109 | + if (response->Status == "200") { |
| 110 | + TStringBuf jsonError; |
| 111 | + NJson::TJsonValue jsonValue; |
| 112 | + NJson::TJsonReaderConfig jsonConfig; |
| 113 | + if (NJson::ReadJsonTree(response->Body, &jsonConfig, &jsonValue)) { |
| 114 | + const NJson::TJsonValue* jsonImpersonatedToken; |
| 115 | + if (jsonValue.GetValuePointer("impersonation", &jsonImpersonatedToken)) { |
| 116 | + TString impersonatedToken = jsonImpersonatedToken->GetStringRobust(); |
| 117 | + ProcessImpersonatedToken(impersonatedToken, ctx); |
| 118 | + return; |
| 119 | + } else { |
| 120 | + jsonError = "Wrong OIDC provider response: impersonated token not found"; |
| 121 | + } |
| 122 | + } else { |
| 123 | + jsonError = "Wrong OIDC response"; |
| 124 | + } |
| 125 | + NHttp::THeadersBuilder responseHeaders; |
| 126 | + responseHeaders.Set("Content-Type", "text/plain"); |
| 127 | + httpResponse = Request->CreateResponse("400", "Bad Request", responseHeaders, jsonError); |
| 128 | + } else { |
| 129 | + NHttp::THeadersBuilder responseHeaders; |
| 130 | + responseHeaders.Parse(response->Headers); |
| 131 | + httpResponse = Request->CreateResponse(response->Status, response->Message, responseHeaders, response->Body); |
| 132 | + } |
| 133 | + } else { |
| 134 | + NHttp::THeadersBuilder responseHeaders; |
| 135 | + responseHeaders.Set("Content-Type", "text/plain"); |
| 136 | + httpResponse = Request->CreateResponse("400", "Bad Request", responseHeaders, event->Get()->Error); |
| 137 | + } |
| 138 | + ctx.Send(Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(httpResponse)); |
| 139 | + Die(ctx); |
| 140 | +} |
| 141 | + |
| 142 | +TImpersonateStartPageHandler::TImpersonateStartPageHandler(const NActors::TActorId& httpProxyId, const TOpenIdConnectSettings& settings) |
| 143 | + : TBase(&TImpersonateStartPageHandler::StateWork) |
| 144 | + , HttpProxyId(httpProxyId) |
| 145 | + , Settings(settings) |
| 146 | +{} |
| 147 | + |
| 148 | +void TImpersonateStartPageHandler::Handle(NHttp::TEvHttpProxy::TEvHttpIncomingRequest::TPtr event, const NActors::TActorContext& ctx) { |
| 149 | + ctx.Register(new THandlerImpersonateStart(event->Sender, event->Get()->Request, HttpProxyId, Settings)); |
| 150 | +} |
| 151 | + |
| 152 | +} // NOIDC |
| 153 | +} // NMVP |
0 commit comments