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