1

This is the service callout policy:

<ServiceCallout name="GeoCodeClient">
    <Request clearPayload="false" variable="GeocodingRequest" />
    <Response>GeocodingResponse</Response>
    <Timeout>30000</Timeout>
    <HTTPTargetConnection>
      <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Let us say I have to access a resource that is username/password protected. How do I add that basic authorization to this policy to enable me to do that?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user1801279
  • 1,743
  • 5
  • 24
  • 40

4 Answers4

2

In our project a KeyValueMaps are used to store the basic auth info at org level. The authorisation information is retrieved using the KeyValueMap policy and added as the basic auth header to the request message.

See if this approach works for you.

Srikanth
  • 1,015
  • 12
  • 16
  • 3
    Would be ideal to provide a basic example here as well, as the doc urls are liable to change in the future. – brandonscript Jan 16 '14 at 16:26
  • Not sure i understand, though i worded it as a way to solve the issue. It is one of the solutions to the problem stated by the author of the question. Please let me know your thoughts. – Srikanth Jun 30 '14 at 09:09
2

To add Basic Authentication header for your service callout, you can use an 'AssignMessage' policy that sets the 'Authorization' header in the 'GeocodingRequest' as follows:

<AssignMessage enabled="true" continueOnError="true" async="false" name="AssignAuthorizationHeaderPolicy">
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="request">GeocodingRequest</AssignTo>
     <Add>
        <Headers>
            <Header name="Authorization">Basic YourAuthenticationHeader</Header>
        </Headers>
    </Add>
</AssignMessage>

Once you have created this policy, you will need to attach it in the request flow before the serviceCallout in the proxy.xml as flows:

            <Step>
                <FaultRules/>
                <Name>AssignAuthorizationHeaderPolicy</Name>
            </Step>
            <Step>
                <FaultRules/>
                <Name>GeoCodeClient</Name>
            </Step>
Brajesh
  • 21
  • 1
0

to add to what's already been said, if you need base64 encoding (and you probably will if you're using Basic Authorization), you'll need to do script callout. For instance, you can use the following Python:

import base64

if (client_secret is not None): 
 data = client_id + ":" + client_secret
 header_value = base64.b64encode(data)
 header_value = "Basic " + header_value
 flow.setVariable("request.header.Authorization", header_value)

JS will be a little trickier since you need to include appropriate libraries, but I'm sure SO has plenty of more examples to follow for that.

akoo1010
  • 606
  • 3
  • 9
0

Using Key Value Map to store sensitive data in a secure way

Step 1)Use below API to Create/Update the key Value maphttps://api.enterprise.apigee.com/v1/o/{orgname}/environments/{env}/keyvaluemaps Body:-{
  "entry" : [ {
    "name" : "basic_auth_system1",
    "value" : "Basic XXXXXXXXXXX"
  } ],
  "name" : "system1_credentials"
}
Step 2) Policy used to lookup The key Value map 

<KeyValueMapOperations enabled="true" continueOnError="false" async="false" name="keymap_get_credentials" mapIdentifier="system1_credentials">
    <DisplayName>keymap_get_credentials</DisplayName>
    <FaultRules/>
    <Properties/>
    <ExpiryTimeInSecs>-1</ExpiryTimeInSecs>
    <Get assignTo="basic_auth_system1">
        <Key>
            <Parameter>basic_auth_system1</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>
randomness
  • 1,377
  • 1
  • 14
  • 21