@@ -98,10 +98,13 @@ public void Inbound(IInboundContext context)
9898 context . SetVariable ( "SAPBearerToken" , GetSAPBearerToken ( context . ExpressionContext ) ) ;
9999 context . SetVariable ( "SAPRefreshToken" , GetSAPRefreshToken ( context . ExpressionContext ) ) ;
100100 context . SetVariable ( "RandomBackOffDelay" , GetRandomBackOffDelay ( context . ExpressionContext ) ) ;
101-
102- context . InlinePolicy ( "<cache-store-value key=\" @(\" SAPPrincipal\" + context.Request.Headers.GetValueOrDefault(\" Authorization\" ,\" \" ).AsJwt()?.Subject)\" value=\" @((string)context.Variables[\" SAPBearerToken\" ])\" duration=\" @((int)context.Variables[\" iSAPBearerTokenExpiry\" ] - (int)context.Variables[\" RandomBackOffDelay\" ])\" />" ) ;
103- context . InlinePolicy ( "<cache-store-value key=\" @(\" SAPPrincipalRefresh\" + context.Request.Headers.GetValueOrDefault(\" Authorization\" ,\" \" ).AsJwt()?.Subject)\" value=\" @((string)context.Variables[\" SAPRefreshToken\" ])\" duration=\" @(int.Parse((string)context.Variables[\" SAPOAuthRefreshExpiry\" ]) - (int)context.Variables[\" RandomBackOffDelay\" ])\" />" ) ;
104- } else if ( ContainsRefreshTokenOnly ( context . ExpressionContext ) )
101+
102+ context . InlinePolicy (
103+ "<cache-store-value key=\" @(\" SAPPrincipal\" + context.Request.Headers.GetValueOrDefault(\" Authorization\" ,\" \" ).AsJwt()?.Subject)\" value=\" @((string)context.Variables[\" SAPBearerToken\" ])\" duration=\" @((int)context.Variables[\" iSAPBearerTokenExpiry\" ] - (int)context.Variables[\" RandomBackOffDelay\" ])\" />" ) ;
104+ context . InlinePolicy (
105+ "<cache-store-value key=\" @(\" SAPPrincipalRefresh\" + context.Request.Headers.GetValueOrDefault(\" Authorization\" ,\" \" ).AsJwt()?.Subject)\" value=\" @((string)context.Variables[\" SAPRefreshToken\" ])\" duration=\" @(int.Parse((string)context.Variables[\" SAPOAuthRefreshExpiry\" ]) - (int)context.Variables[\" RandomBackOffDelay\" ])\" />" ) ;
106+ }
107+ else if ( ContainsRefreshTokenOnly ( context . ExpressionContext ) )
105108 {
106109 context . SendRequest ( new SendRequestConfig
107110 {
@@ -160,16 +163,16 @@ public void Inbound(IInboundContext context)
160163 }
161164 ] ,
162165 } ) ;
163- if ( IsCSRFRequestSuccessfull ( context . ExpressionContext ) )
166+ if ( IsCSRFRequestSuccessfull ( context . ExpressionContext ) )
164167 {
165168 context . SetVariable ( "SAPCSRFToken" , GetCSRFToken ( context . ExpressionContext ) ) ;
166169 context . SetVariable ( "SAPXSRFCookie" , GetXsrfCookie ( context . ExpressionContext ) ) ;
167170 }
168171 }
169-
172+
170173 context . SetHeader ( "Authorization" , GetSAPAuthorizationBearerToken ( context . ExpressionContext ) ) ;
171174 context . RemoveHeader ( "Ocp-Apim-Subscription-Key" ) ;
172- if ( IsGetNotToMetadataRequest ( context . ExpressionContext ) )
175+ if ( IsGetNotToMetadataRequest ( context . ExpressionContext ) )
173176 {
174177 context . SetHeader ( "$format" , "json" ) ;
175178 }
@@ -185,7 +188,7 @@ public void Outbound(IOutboundContext context)
185188 context . Base ( ) ;
186189 context . InlinePolicy ( "<find-and-replace from=\" @(context.Api.ServiceUrl.Host +\" :\" + context.Api.ServiceUrl.Port)\" to=\" @(context.Request.OriginalUrl.Host + \" :\" + context.Request.OriginalUrl.Port + context.Api.Path)\" />" ) ;
187190 }
188-
191+
189192 public void OnError ( IOnErrorContext context )
190193 {
191194 context . Base ( ) ;
@@ -207,7 +210,7 @@ string CreateAadTokenRequestBody(IExpressionContext context)
207210 var _AADRegisteredAppClientId = context . Variables [ "APIMAADRegisteredAppClientId" ] ;
208211 var _AADRegisteredAppClientSecret = context . Variables [ "APIMAADRegisteredAppClientSecret" ] ;
209212 var _AADSAPResource = context . Variables [ "AADSAPResource" ] ;
210- var assertion = context . Request . Headers . GetValueOrDefault ( "Authorization" , [ "" ] ) [ 0 ] . Replace ( "Bearer " , "" ) ;
213+ var assertion = context . Request . Headers . GetValueOrDefault ( "Authorization" , "" ) . Replace ( "Bearer " , "" ) ;
211214 return $ "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={ assertion } &client_id={ _AADRegisteredAppClientId } &client_secret={ _AADRegisteredAppClientSecret } &scope={ _AADSAPResource } /.default&requested_token_use=on_behalf_of&requested_token_type=urn:ietf:params:oauth:token-type:saml2";
212215 }
213216
@@ -239,42 +242,58 @@ string CreateSapRefreshTokenRequestBody(IExpressionContext context)
239242
240243 JObject GetSAPBearerResponseObject ( IExpressionContext context )
241244 => ( ( IResponse ) context . Variables [ "fetchSAPBearer" ] ) . Body . As < JObject > ( ) ;
245+
242246 JObject GetSAPRefreshResponseObject ( IExpressionContext context )
243- => ( ( IResponse ) context . Variables [ "fetchrefreshedSAPBearer" ] ) . Body . As < JObject > ( )
247+ => ( ( IResponse ) context . Variables [ "fetchrefreshedSAPBearer" ] ) . Body . As < JObject > ( ) ;
248+
244249 string GetSAPBearerTokenExpiry ( IExpressionContext context )
245250 => ( ( JObject ) context . Variables [ "SAPResponseObject" ] ) [ "expires_in" ] . ToString ( ) ;
251+
246252 int GetIntSAPBearerTokenExpiry ( IExpressionContext context )
247253 => int . Parse ( ( string ) context . Variables [ "SAPBearerTokenExpiry" ] ) ;
254+
248255 string GetSAPBearerToken ( IExpressionContext context )
249- => ( ( JObject ) context . Variables [ "SAPResponseObject" ] ) [ "access_token" ] . ToString ( )
256+ => ( ( JObject ) context . Variables [ "SAPResponseObject" ] ) [ "access_token" ] . ToString ( ) ;
257+
250258 string GetSAPRefreshToken ( IExpressionContext context )
251- => ( ( JObject ) context . Variables [ "SAPResponseObject" ] ) [ "refresh_token" ] . ToString ( )
259+ => ( ( JObject ) context . Variables [ "SAPResponseObject" ] ) [ "refresh_token" ] . ToString ( ) ;
260+
252261 double GetRandomBackOffDelay ( IExpressionContext context )
253- => new Random ( ) . Next ( 0 , ( int ) context . Variables [ "iSAPBearerTokenExpiry" ] / 3 ) ;
262+ => new Random ( ) . Next ( 0 , ( int ) context . Variables [ "iSAPBearerTokenExpiry" ] / 3 ) ;
263+
254264 bool ContainsRefreshTokenOnly ( IExpressionContext context )
255265 => ! context . Variables . ContainsKey ( "SAPBearerToken" ) && context . Variables . ContainsKey ( "SAPRefreshToken" ) ;
266+
256267 bool IsNotGetOrHeadRequest ( IExpressionContext context )
257268 => context . Request . Method != "GET" && context . Request . Method != "HEAD" ;
269+
258270 string GetRequestURL ( IExpressionContext context ) => context . Request . Url . ToString ( ) ;
259- string GetSAPAuthorizationBearerToken ( IExpressionContext context )
271+
272+ string GetSAPAuthorizationBearerToken ( IExpressionContext context )
260273 => "Bearer " + ( string ) context . Variables [ "SAPBearerToken" ] ;
261- bool IsCSRFRequestSuccessfull ( IExpressionContext context )
274+
275+ bool IsCSRFRequestSuccessfull ( IExpressionContext context )
262276 => ( ( IResponse ) context . Variables [ "SAPCSRFToken" ] ) . StatusCode == 200 ;
277+
263278 string GetCSRFToken ( IExpressionContext context )
264279 => ( ( IResponse ) context . Variables [ "SAPCSRFToken" ] ) . Headers . GetValueOrDefault ( "x-csrf-token" ) ;
280+
265281 string GetXsrfCookie ( IExpressionContext context )
266282 {
267283 string rawcookie = ( ( IResponse ) context . Variables [ "SAPCSRFToken" ] ) . Headers . GetValueOrDefault ( "Set-Cookie" ) ;
268284 string [ ] cookies = rawcookie . Split ( ';' ) ;
269- string xsrftoken = cookies . FirstOrDefault ( ss => ss . Contains ( "sap-XSRF" ) ) ;
270- if ( xsrftoken == null ) {
271- xsrftoken = cookies . FirstOrDefault ( ss => ss . Contains ( "SAP_SESSIONID" ) ) ;
285+ string xsrftoken = cookies . FirstOrDefault ( ss => ss . Contains ( "sap-XSRF" ) ) ;
286+ if ( xsrftoken == null )
287+ {
288+ xsrftoken = cookies . FirstOrDefault ( ss => ss . Contains ( "SAP_SESSIONID" ) ) ;
272289 }
273290
274291 return xsrftoken . Split ( ',' ) [ 1 ] ;
275292 }
293+
276294 bool IsGetNotToMetadataRequest ( IExpressionContext context )
277295 => ! context . Request . Url . Path . Contains ( "/$metadata" ) && context . Request . Method == "GET" ;
296+
278297 string GetErrorSource ( IExpressionContext context ) => context . LastError . Source ;
279298 string GetErrorReason ( IExpressionContext context ) => context . LastError . Reason ;
280299 string GetErrorMessage ( IExpressionContext context ) => context . LastError . Message ;
0 commit comments