White Papers
Abstract
Part 6 of this multi-part series describes WebSphere DataPower Appliance support for the OAuth 2.0 authorization code grant type. It covers configuration for both the authorization server and the enforcement point.
Content
Authorization code is one of the grant types that support the traditional OAuth protocol (see Section 4.1 of the OAuth 2.0 Specification). In this tutorial, we provide background information about the authorization code grant type and how to use WebSphere DataPower to provide this OAuth grant type support. This tutorial has been updated for firmware 6.0 features. As defined in the OAuth 2.0 Specification, an authorization grant type is optimized for confidential clients. A confidential client is a client that maintains the confidentiality of its credentials. With an authorization code grant type, the client must be able to interact with the resource owner's user agent (such as a browser or mobile application) and receive an incoming request with redirection. This grant type is appropriate for server-side web applications that provide the required level of confidentiality. Figure 1 provides a high-level overview of the authorization code grant type. The explanation below is numbered according to the steps labeled in Figure 1. This grant type allows the resource owner to share resources with a third-party client application without sharing his or her credentials. Moreover, the access permission (in the form of access token) is never shared with the resource owner. This provides security and assurance that access to resources is limited to the OAuth client. The use case provided in the specification, (IETF-OAuth20) is a good example. It discusses how the resource owner wants to print pictures with a printing service (OAuth Client application). However, the pictures were at a photo sharing site (resource server). For this scenario, the resource owner never shares the photo sharing site credentials (username, and password) with the printing service. In the scenario above, we position DataPower as the authorization server and as the enforcement point for the resource server. When DataPower acts as an authorization server, it provides: When DataPower acts as an enforcement point for the resource server, it verifies the access token. DataPower only allows the request to the backend resource server if the access token is valid. There are three distinct operations: In order to concentrate on OAuth configuration, some preconfigured DataPower objects are provided from the Downloadable resources section of this tutorial. You can test the loopback process by performing the following cURL GET requests. It returns a sample REST document if properly configured. You wish to review the XML Firewall and the policy rules it contains. An OAuth client application is represented on DataPower by an OAuth Client Profile object. The registration of a client is the inclusion of its OAuth client profile within an OAuth Client Group object associated with the authorization or enforcement service. An OAuth client group is associated to a service through a reference within an AAA policy. Figure 2 illustrates this relationship. The following steps create an OAuth client group: In this step, we create an AAA policy that accepts identity credentials from either basic authentication HTTP headers or via an OAuth authorization code token. This policy is later attached to a Web Token Service that accepts requests, validates basic authentication headers, and produces the authentication code. The authorization code then be exchanged for an access token. While you could implement this with a Multi-protocol Gateway (MPGW) service, a web token service provides automatic policy creation as discussed in Part 2 of this tutorial series. In this step, we create the AAA policy that validate incoming OAuth access tokens for the enforcement point. The access token has been produced by a Web Token service that uses the previous AAA policy (AAA-AZ). After creating this AAA policy, we'll attach it to a MPGW service to act as the enforcement point for the resource server. Starting with firmware 6.0.0.x, DataPower automatically validate the scope against the oauth-scope-metadata with these settings. However, in 5.0.0.x, this must be done with a custom stylesheet in the "Credential-mapping" stage. In this step, we create the Web Token service that uses the AAA policy (AAA-AZ) to authenticate the resource owner that uses basic authentication headers. It validates the OAuth client request and produces an authorization code. For a token request, it produces an access token. The access token later is sent to a MPGW authorizing the request to the resource server. In this step, we create the MPGW that validates the access token produced by the web token service to authorize requests for resources on the resource server. This completes the OAuth configuration. Testing the authorization code grant is more involved than Part 4, resource owner password credential scenarios, and Part 5, client credential scenarios. In this part, we have to simulate three distinct parties: (1) the client, (2) the resource owner, and (3) the token/resource server (combined as one for this exercise). This tutorial provides three options for testing the authorization code grant scenario. The cURL tool is the easiest to install, but it requires the most manual effort to complete the test. This extra manual effort does provide more insight into the details of the exchanges. Node.js and PHP require you to install their respective runtimes. The Node.js option is closest to what one encounters in practice. This section demonstrates testing of our configuration that uses the cURL tool. This process simply shows the results of each service invocation. You need to copy the authorization code tokens and access tokens between steps. The DataPower IP address is referenced as "[DP_IP]" in the samples below. The imaginary client IP is "1.2.3.4". If your browser does not trust your DataPower certificate, you have to add it as an exception. You receive an error from the browser, since the 302 HTTP redirect from the DataPower authorization endpoint cannot reach 1.2.3.4. However, you can copy the code from the browser header, as shown in Figure 9. Make sure to start the copy after "code=" and stop before the next parameter that starts with an ampersand (&). The authorization code must be URL-encoded when it is sent back to the token endpoint on the DataPower appliance. Since it was already URL-encoded within the redirect, it can be copied from the redirect URL and pasted in place of From the above command, you receive an access token that can be used for accessing the resource. Download the Node.js client from its OAuth Clients hosting site. Follow the instructions for unzipping the archive and starting the client on the command-line. The client command-line output displays a "Client App Homepage" for Part 6 (this tutorial). While following the steps below, it is recommended to have the command-line visible as you interact with the browser so you can see what the client is doing behind the scenes. After the client receives the authorization code, it responds to the browser with a simple text message. The rest of the interaction between the client (Node.js) and DataPower is displayed on the command-line. A PHP OAuth client and its corresponding HTML page (code.php and code.htm) are provided in the Part6Supplements.zip archive with this tutorial. The PHP client simulates an OAuth client that supports the authorization code grant type. The PHP client running on WebServer with support for curl, PHP, and SSL. The TCP ports in your configuration differ from the screenshots in this section. The response from the authorization server is redirected back to the PHP client. The response from that redirect is shown in Figure 12. The authorization code that was received is displayed after "code". The redirect_uri and endpoint are displayed as well as the client secret and the client ID. This is for the OAuth client to request an access token based on the authorization code it received earlier. The OAuth client requests an access token from the authorization server. Figure 13 shows the OAuth client revealing the access token back to your browser (the resource owner). This is for educational purposes only. Normally, the OAuth client would instead issue a resource request directly to the resource server with the access token (as the Node.js client does). Notice how the resource is enforced by the appliance based on the setting in the AAA ("Resource Extraction" or "Resource Mapping"). The response from This response was returned to the OAuth client. The OAuth client is providing it to your browser for illustrative purposes only. The exchanges shown in Figures 14 and 15 would normally occur exclusively between the OAuth client and the resource server as demonstrated with the Node.js sample. In this tutorial, we covered the motivations for that uses an authorization code grant type. This grant type is appropriate for server-side web applications, which maintain the confidentiality of client credentials. This grant type also provides more security benefits, including the ability to authenticate the OAuth client and pass the access token to the OAuth client without exposing it to the resource owner. The authors would like to thank John Rasmussen and Peter Argue for their review of this tutorial.Introduction
Overview of an authorization code grant type

Using the authorization code grant type
Where does WebSphere DataPower fit in?
Preparing for DataPower configuration
curl http://<appliance-host>:5001/getCustomerInfo returns { "id":0, "name":"", "access_token":"Unknown" } curl http://<appliance-host>:5001/getAccountInfo returns { "name": "myAccount", "balance":1.00 } 
OAuth and click OAuth Client Profile.
myregistered_oauthclientpassw0rd^/getAccountInfo|/getCustomerInfo^https://\S+. Click Add to add the entry to the list.

OAuth-Code-Client for the Name field.Create AAA for OAuth authorization and token endpoint
AAA and click AAA Policy.store:///AAAInfo.xml. For this tutorial, we use the pre-established identity of "fred" with a password of "smith".Create an AAA policy for the token enforcement point
Register DataPower to act as an authorization server
Web Token Service at the search box under the Control Panel and select New Web Token Service.oauth-wts and click Next.

Configure DataPower as enforcement point for the resource server
resource-enforcementhttp://127.0.0.1:5001 (the URL of the Account loopback resource server)
https50615061mpgw, and click Add Policy.
Testing the configuration
Testing with cURL Client
--user parameter. The –o test.html sends the output to test.html rather than directly to the screen. Since you have no actual client to redirect to, specify https://1.2.3.4 for the IP address. curl -k "https://[DP_IP]:5060/authen? response_type=code&client_id=myregistered_oauthclient&redirect_uri= https://1.2.3.4&scope=/getAccountInfo%20/getCustomerInfo&state= MyTest" --user fred:smith -v -o test.html


--user parameter) to pass in the client_id and client_secret parameters. %CODE_FROM_BROWSER% as shown below. curl -k https://[DP_IP]:5060/token -d "grant_type=authorization_code&code=%CODE_FROM_BROWSER%&redirect_uri=https://1.2.3.4" --user myregistered_oauthclient:passw0rd
{ "token_type":"bearer", "access_token":"AAEYbX+y......", "expires_in":3600, "scope":"/getAccountInfo /getCustomerInfo" } curl -k https://%DP_ID%:5061/getAccountInfo -H "Authorization:Bearer %URL-ENCODED_ACCESS_TOKEN%" { "name": "myAccount", "balance":1.00 }
fred/smith for the username/password at the browser password challenge.Testing with PHP client
http://<yourwebserver>/code.htm (see Figure 10):
[DP_IP]:5060/authen.
fred/smith as the resource owner username/password as shown in Figure 11. Click OK. 



/getCustomerInfo shows a subset of the information in the access token (Figure 15).
Conclusion
Acknowledgments
Was this topic helpful?
Document Information
Modified date:
08 June 2021
UID
ibm11109469