

CSRF Token handling in SAP API Management
source link: https://blogs.sap.com/2021/09/18/csrf-token-handling-in-sap-api-management/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CSRF Token handling in SAP API Management
Introduction
While exposing SAP GW OData services via SAP APIM (API management), we have encountered one simple requirement to enable server to server authentication between S/4 Hana and SAP APIM system. So that the API consumers will not have to know backend system credentials and they can use policy based APIKey or OAuth 2.0 kind of authentication from Developer portal. Whenever we will make POST, PUT and DELETE HTTP verb, it will require CSRF token validation from backend, so we will try to automate that as well from API management layer.
I will not explain how to expose OData service using SAP APIM in this blog. Those are well documented in other blog by Benno Grimm. There are many other blogs as well you can refer.
So, let’s see the policies required to enable backend authentication from APIM.
- Key value map:
We have created a Key value map for storing credential of backend system.
Key Value Map for Backend Credential
- Then we will implement below 4 policies in target endpoint.
Policy
- kvmFetch (KeyValueMapOperations):
This policy will help to retrieve username and password from key value map and assign them to private variables. No condition string required for this policy.
<KeyValueMapOperations mapIdentifier="ES5Credential" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt">
<Get assignTo="private.BasicAuthUsername" index='1'>
<Key><Parameter>Username</Parameter></Key>
</Get>
<Get assignTo="private.BasicAuthPassword" index='1'>
<Key><Parameter>Password</Parameter></Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>
- baAuth (BasicAuthentication):
This policy will help to encode the username and password in Base64 and assign that to Authorization header. No condition string required for this policy.
<BasicAuthentication continueOnError='false' enabled='true' xmlns='http://www.sap.com/apimgmt'>
<Operation>Encode</Operation>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<User ref='private.BasicAuthUsername'></User>
<Password ref='private.BasicAuthPassword'></Password>
<AssignTo createNew="true">request.header.Authorization</AssignTo>
</BasicAuthentication>
- scCSRF (ServiceCallout):
This policy will help to fetch CSRF token from backend call. Please put below as Condition string. I have used an API provider named ES5 for backend connection in APIM.
(request.verb = "POST" OR request.verb = "PUT" OR request.verb = "DELETE")
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="true" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt">
<Request>
<Set>
<Headers>
<Header name="x-csrf-token">fetch</Header>
<Header name="Authorization">{request.header.Authorization}</Header>
</Headers>
<Verb>GET</Verb>
</Set>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</Request>
<Response>callOutResponse</Response>
<Timeout>30000</Timeout>
<HTTPTargetConnection>
<APIProvider>ES5</APIProvider>
<Path>sap/opu/odata/IWFND/CATALOGSERVICE/ServiceCollection</Path>
</HTTPTargetConnection>
</ServiceCallout>
- amCSRF (AssignMessage):
This policy will help to assign CSRF token and cookies to the request message. Please put below as Condition string.
(request.verb = "POST" OR request.verb = "PUT" OR request.verb = "DELETE")
<!-- This policy can be used to create or modify the standard HTTP request and response messages -->
<AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<!-- Sets a new value to the existing parameter -->
<Set>
<Headers>
<Header name="x-csrf-token">{callOutResponse.header.x-csrf-token}</Header>
<Header name="Cookie">{callOutResponse.header.Set-Cookie.1};{callOutResponse.header.Set-Cookie.2};{callOutResponse.header.Set-Cookie.3}</Header>
</Headers>
</Set>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="request">request</AssignTo>
</AssignMessage>
We can call from postman.
Testing
Conclusion
I have tried to replicate the same for On-Premises API provided but the service callout policy was failing, but I have used local proxy call in service callout policy and it started working.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="true" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt">
<Request>
<Set>
<Headers>
<Header name="x-csrf-token">fetch</Header>
<Header name="Authorization">{request.header.Authorization}</Header>
</Headers>
<Verb>GET</Verb>
</Set>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</Request>
<Response>callOutResponse</Response>
<Timeout>30000</Timeout>
<LocalTargetConnection>
<Path><API Base Path></Path>
</LocalTargetConnection>
</ServiceCallout>
Recommend
-
14
用Burpsuite 来处理csrf token c4bbage
-
15
November 10, 2020 How To Fix Laravel CSRF Token Mismatch Error From AJAX RequestAs I’ve mentioned in pre...
-
25
Technical Articles
-
10
stephen xue August 29, 2021 9 minute read
-
10
How To Handle CSRF Token in Jmeter Reading Time: 2 minutesWhat is CSRF– CSRF stands for Cross-Site Request Forgery. Generally when we login in website it always ask for authentication. For the...
-
11
【Root-Me】 CSRF 这题与【Web-Client : CSRF - 0 protection】是一样的,只是多了一个 token 校验。 切到 Profi...
-
8
GET and POST CSRF Token internally using policies in SAP APIM Introduction This blog post describes how to call CSRF token internally and post the token in headers using policies in SAP API Management
-
20
Dimiter Dimitrov Feb 07 at 03:07 AM CSRF token is missing in MDK Client 6.3.4 for iOS 168 Views...
-
7
Sunil Kumar Maurya November 30, 2020 5 minute read...
-
4
Casdoor < v1.331.0 - '/api/set-password' CSRF ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK