As noted, the API from your backend system is now fully accessible without any restrictions. In a productive scenario, the backend should always be protected by typical authentication mechanisms such as basic authentication, SAML, OAuth or JWT. API Management can be used as “security mediation” layer, in which any kind of credential is checked and then passed to the backend in the desired format using predefined policies. In our scenario, we will keep things simple: you will add an API key verification, which will make sure the consumer is allowed to access the API. Note that this is not sufficient to properly secure your API since API keys can be read from applications (retro engineering) or network sniffers (it is not required to use SSL for an API key - in contrary to OAuth for example).
Find your _MSO_API under Configure -> APIs and select it to open the API viewer. Use the search facility if necessary.
Select Policies to open the policy editor, then select Edit to make changes.
Click on ProxyEndpoint and PreFlow in the “Flows” panel on the left and select Verify API key security policy
Enter VerifyAPIkey as Policy Name and click Add
Replace the value of the ref property of the APIKey tag with the following value: request.queryparam.apikey
Here is the complete XML configuration of the policy in case you need it:
123<VerifyAPIKey async='true' continueOnError='false' enabled='true' xmlns='http://www.sap.com/apimgmt'>
<APIKey ref='request.queryparam.apikey'/>
</VerifyAPIKey>Click on Update in the policy editor, then on Save in the API proxy and finally on Deploy.
Test your policy by using the link of the API Proxy, as you did previously. API Management now throws an error telling you that no parameter with the name “api key” has been found.
As you can see the error message:
12345678{
"fault":{
"faultstring":"Failed to resolve API Key variable request.queryparam.apikey",
"detail":{
"errorcode":"steps.oauth.v2.FailedToResolveAPIKey"
}
}
}At the end of the URL of your API, add the following text: ?apikey=12345. This adds the apikey parameter with a dummy value to the request. Now execute your request again.
12345678{
"fault":{
"faultstring":"Invalid ApiKey",
"detail":{
"errorcode":"oauth.v2.InvalidApiKey"
}
}
}Congratulations! You are now ready for the next lesson!