10
10
#include " oidc_session_create_handler.h"
11
11
#include " oidc_settings.h"
12
12
#include " openid_connect.h"
13
+ #include " context.h"
13
14
14
15
using namespace NMVP ::NOIDC;
15
16
@@ -719,23 +720,18 @@ Y_UNIT_TEST_SUITE(Mvp) {
719
720
TStringBuilder request;
720
721
request << " GET /auth/callback?code=code_template&state=" << state << " HTTP/1.1\r\n " ;
721
722
request << " Host: " + hostProxy + " \r\n " ;
722
- request << " Cookie: " << CreateNameYdbOidcCookie (settings.ClientSecret , wrongState) << " =" << GenerateCookie (wrongState, " /requested/page" , settings.ClientSecret , redirectStrategy.IsAjaxRequest ()) << " \r\n " ;
723
+ TContext context (wrongState, " /requested/page" , redirectStrategy.IsAjaxRequest ());
724
+ request << " Cookie: " << context.CreateYdbOidcCookie (settings.ClientSecret ) << " \r\n " ;
723
725
NHttp::THttpIncomingRequestPtr incomingRequest = new NHttp::THttpIncomingRequest ();
724
726
EatWholeString (incomingRequest, redirectStrategy.CreateRequest (request));
725
727
incomingRequest->Endpoint ->Secure = true ;
726
728
runtime.Send (new IEventHandle (sessionCreator, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest (incomingRequest)));
727
729
728
730
TAutoPtr<IEventHandle> handle;
729
731
NHttp::TEvHttpProxy::TEvHttpOutgoingResponse* outgoingResponseEv = runtime.GrabEdgeEvent <NHttp::TEvHttpProxy::TEvHttpOutgoingResponse>(handle);
730
- UNIT_ASSERT_STRINGS_EQUAL (outgoingResponseEv->Response ->Status , " 302" );
731
- const NHttp::THeaders headers (outgoingResponseEv->Response ->Headers );
732
- UNIT_ASSERT (headers.Has (" Location" ));
733
- TString location = TString (headers.Get (" Location" ));
734
- UNIT_ASSERT_STRING_CONTAINS (location, " https://auth.test.net/oauth/authorize" );
735
- UNIT_ASSERT_STRING_CONTAINS (location, " response_type=code" );
736
- UNIT_ASSERT_STRING_CONTAINS (location, " scope=openid" );
737
- UNIT_ASSERT_STRING_CONTAINS (location, " client_id=" + settings.ClientId );
738
- UNIT_ASSERT_STRING_CONTAINS (location, " redirect_uri=https://" + hostProxy + " /auth/callback" );
732
+ UNIT_ASSERT_STRINGS_EQUAL (outgoingResponseEv->Response ->Status , " 400" );
733
+ UNIT_ASSERT_STRING_CONTAINS (outgoingResponseEv->Response ->Body , " Unknown error has occurred. Please open the page again" );
734
+
739
735
}
740
736
741
737
Y_UNIT_TEST (OpenIdConnectotWrongStateAuthorizationFlow) {
@@ -773,8 +769,8 @@ Y_UNIT_TEST_SUITE(Mvp) {
773
769
TStringBuilder request;
774
770
request << " GET /auth/callback?code=code_template&state=" << state << " HTTP/1.1\r\n " ;
775
771
request << " Host: oidcproxy.net\r\n " ;
776
- const TString oidcCookie = CreateNameYdbOidcCookie (settings. ClientSecret , state );
777
- request << " Cookie: " << oidcCookie << " = " << GenerateCookie (state, " /requested/page " , settings.ClientSecret , false ) << " \r\n\r\n " ;
772
+ TContext context (state, " /requested/page " , false );
773
+ request << " Cookie: " << context. CreateYdbOidcCookie ( settings.ClientSecret ) << " \r\n\r\n " ;
778
774
NHttp::THttpIncomingRequestPtr incomingRequest = new NHttp::THttpIncomingRequest ();
779
775
EatWholeString (incomingRequest, request);
780
776
runtime.Send (new IEventHandle (sessionCreator, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest (incomingRequest)));
@@ -823,7 +819,8 @@ Y_UNIT_TEST_SUITE(Mvp) {
823
819
TStringBuilder request;
824
820
request << " GET /auth/callback?code=code_template&state=" << state << " HTTP/1.1\r\n " ;
825
821
request << " Host: oidcproxy.net\r\n " ;
826
- request << " Cookie: " << CreateNameYdbOidcCookie (settings.ClientSecret , state) << " =" << GenerateCookie (state, " /requested/page" , settings.ClientSecret , redirectStrategy.IsAjaxRequest ()) << " \r\n " ;
822
+ TContext context (state, " /requested/page" , redirectStrategy.IsAjaxRequest ());
823
+ request << " Cookie: " << context.CreateYdbOidcCookie (settings.ClientSecret ) << " \r\n " ;
827
824
NHttp::THttpIncomingRequestPtr incomingRequest = new NHttp::THttpIncomingRequest ();
828
825
EatWholeString (incomingRequest, redirectStrategy.CreateRequest (request));
829
826
incomingRequest->Endpoint ->Secure = true ;
@@ -843,22 +840,11 @@ Y_UNIT_TEST_SUITE(Mvp) {
843
840
" Content-Length: " + ToString (authorizationServerResponse.length ()) + " \r\n\r\n " + authorizationServerResponse);
844
841
runtime.Send (new IEventHandle (handle->Sender , edge, new NHttp::TEvHttpProxy::TEvHttpIncomingResponse (outgoingRequestEv->Request , incomingResponse)));
845
842
auto outgoingResponseEv = runtime.GrabEdgeEvent <NHttp::TEvHttpProxy::TEvHttpOutgoingResponse>(handle);
846
- redirectStrategy.CheckRedirectStatus (outgoingResponseEv);
847
- TString location = redirectStrategy.GetRedirectUrl (outgoingResponseEv);
848
- UNIT_ASSERT_STRING_CONTAINS (location, " https://auth.test.net/oauth/authorize" );
849
- UNIT_ASSERT_STRING_CONTAINS (location, " response_type=code" );
850
- UNIT_ASSERT_STRING_CONTAINS (location, " scope=openid" );
851
- UNIT_ASSERT_STRING_CONTAINS (location, " client_id=" + settings.ClientId );
852
- UNIT_ASSERT_STRING_CONTAINS (location, " redirect_uri=https://oidcproxy.net/auth/callback" );
853
-
854
- NHttp::TUrlParameters urlParameters (location);
855
- const TString newState = urlParameters[" state" ];
856
-
857
- NHttp::THeaders headers (outgoingResponseEv->Response ->Headers );
858
- UNIT_ASSERT (headers.Has (" Set-Cookie" ));
859
- const TStringBuf setCookie = headers.Get (" Set-Cookie" );
860
- UNIT_ASSERT_STRING_CONTAINS (setCookie, CreateNameYdbOidcCookie (settings.ClientSecret , newState));
861
- redirectStrategy.CheckSpecificHeaders (headers);
843
+ UNIT_ASSERT_STRINGS_EQUAL (outgoingResponseEv->Response ->Status , " 302" );
844
+ const NHttp::THeaders headers (outgoingResponseEv->Response ->Headers );
845
+ UNIT_ASSERT (headers.Has (" Location" ));
846
+ TStringBuf location = headers.Get (" Location" );
847
+ UNIT_ASSERT_STRING_CONTAINS (location, " /requested/page" );
862
848
}
863
849
864
850
Y_UNIT_TEST (OpenIdConnectSessionServiceCreateAccessTokenInvalid) {
@@ -896,8 +882,8 @@ Y_UNIT_TEST_SUITE(Mvp) {
896
882
TStringBuilder request;
897
883
request << " GET /callback?code=code_template&state=" << state << " HTTP/1.1\r\n " ;
898
884
request << " Host: oidcproxy.net\r\n " ;
899
- const TString oidcCookie = CreateNameYdbOidcCookie (settings. ClientSecret , state );
900
- request << " Cookie: " << oidcCookie << " = " << GenerateCookie (state, " /requested/page " , settings.ClientSecret , false ) << " \r\n\r\n " ;
885
+ TContext context (state, " /requested/page " , false );
886
+ request << " Cookie: " << context. CreateYdbOidcCookie ( settings.ClientSecret ) << " \r\n\r\n " ;
901
887
NHttp::THttpIncomingRequestPtr incomingRequest = new NHttp::THttpIncomingRequest ();
902
888
EatWholeString (incomingRequest, request);
903
889
runtime.Send (new IEventHandle (sessionCreator, edge, new NHttp::TEvHttpProxy::TEvHttpIncomingRequest (incomingRequest)));
@@ -941,14 +927,14 @@ Y_UNIT_TEST_SUITE(Mvp) {
941
927
std::unique_ptr<grpc::Server> sessionServer (builder.BuildAndStart ());
942
928
943
929
const NActors::TActorId sessionCreator = runtime.Register (new TSessionCreateHandler (edge, settings));
944
- TStringBuf firstRequestState = " first_request_state" ;
945
- TStringBuf secondRequestState = " second_request_state" ;
946
- TString firstCookie { CreateNameYdbOidcCookie (settings. ClientSecret , firstRequestState) + " = " + GenerateCookie (firstRequestState , " /requested/page" , settings. ClientSecret , redirectStrategy.IsAjaxRequest ())} ;
947
- TString secondCookie { CreateNameYdbOidcCookie (settings. ClientSecret , secondRequestState) + " = " + GenerateCookie (secondRequestState , " /requested/page" , settings. ClientSecret , redirectStrategy.IsAjaxRequest ())} ;
930
+ TString firstRequestState = " first_request_state" ;
931
+ TString secondRequestState = " second_request_state" ;
932
+ TContext context1 ( firstRequestState, " /requested/page" , redirectStrategy.IsAjaxRequest ());
933
+ TContext context2 ( secondRequestState, " /requested/page" , redirectStrategy.IsAjaxRequest ());
948
934
TStringBuilder request;
949
935
request << " GET /auth/callback?code=code_template&state=" << firstRequestState << " HTTP/1.1\r\n " ;
950
936
request << " Host: oidcproxy.net\r\n " ;
951
- request << " Cookie: " << firstCookie << " ; " << secondCookie << " \r\n " ;
937
+ request << " Cookie: " << context1. CreateYdbOidcCookie (settings. ClientSecret ) << " ; " << context2. CreateYdbOidcCookie (settings. ClientSecret ) << " \r\n " ;
952
938
NHttp::THttpIncomingRequestPtr incomingRequest = new NHttp::THttpIncomingRequest ();
953
939
EatWholeString (incomingRequest, redirectStrategy.CreateRequest (request));
954
940
incomingRequest->Endpoint ->Secure = true ;
0 commit comments