Duration : 20 mins
Persona : API Team/Security
You have an API that is needs to be consumed by a trusted client application. You want to secure that API using OAuth 2.0 and use an external identity provider such as Okta to protect the application end user’s identity. In this lab we will use Apigee as the OAuth provider to protect the API endpoints using OAuth 2.0. Okta will be used to protect the application end user’s identity. We will accomplish this by integrating Okta into the Apigee OAuth proxy, and implement OAuth 2.0 with the resource owner password credentials (or ROPC) grant type.
(optional) See: Apigee + Okta - Using OAuth 2.0 Resource Owner / Password Grant Type
Apigee has built in support to implement OAuth 2.0 with the ROPC grant type. Using the OAuthV2 policy, Apigee Edge can be configured to act as the authorization provider for access to the API, while using the Service Callout policy to invoke Okta’s authentication API to authenticate the identity of the end user.
For this lab we will use the client application - Hipster Products App - that needs to consume the API - {yourinitials}_Hipster-Products-API - both of which you built in the previous modules.
The ROPC grant type is mostly used in cases where the app is highly trusted. In this configuration, the end user provides their resource server credentials (username/password) to the client app, which sends them in an access token request to the OAuth authorization server (in this case Apigee Edge). An identity provider or IdP (in this case Okta) validates the credentials, and if they are valid, Apigee Edge proceeds to mint an access token and returns it to the app.
In this scenario, we will set up:
Invoke the following API request (either from a terminal or a REST client):
POST /api/v1/users?activate=true HTTP/1.1
Host: dev-271499-admin.okta.com
Content-Type: application/json
Authorization: SSWS 004LHmN3InpVQ9pOMWjsdb6ZDUmi1IP8_DLjaOlb6Z
{"profile": {"firstName": "<Enter First Name>","lastName": "<Enter Last Name>","email": "<Enter Email ID>","login": "<Enter Email ID>"},"credentials": {"password" : { "value": "<Enter Password>"}}}
Example curl
command (replace user details with your own):
curl -X POST "https://dev-271499-admin.okta.com/api/v1/users?activate=true" -H "Content-Type: application/json" -H "Authorization: SSWS 004LHmN3InpVQ9pOMWjsdb6ZDUmi1IP8_DLjaOlb6Z" -d '{"profile": {"firstName": "<Enter First Name>","lastName": "<Enter Last Name>","email": "<Enter Email ID>","login": "<Enter Email ID>"},"credentials": {"password" : { "value": "<Enter Password>"}}}'
This will create an active end user profile in Okta:
First, we must set up the OAuth token endpoint. To do this, download the API proxy bundle from here.
Once downloaded, navigate to Develop → API Proxies from the side navigation menu:
Now that we have configured the end user credentials in Okta, and the API Proxy in Apigee Edge, let us proceed to test the OAuth ROPC flow end to end.
POST /oauth-ext/token HTTP/1.1
Host: -.apigee.net
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Authorization:
grant_type=password&user=&password=
Replace with your actual Apigee org name, and with the deployment environment for your proxy (eg. test)
To obtain a base64 encoded value of ‘Key:Secret’, you can utilize an open tool like G Suite Toolbox:
Example curl
command (replace with values as described above):
curl -X POST -H "Accept:application/json" \
-H "Content-Type:application/x-www-form-urlencoded" \
-H "Authorization:" \
-d 'grant_type=password&user=&password=' \
"https://-.apigee.net/oauth-ext/token"
You should now have an OAuth access token returned in the body of the HTTP response. Copy the value of access_token
(not including the surrounding quotes) as you will need it for the next step.
Also, note in the trace session that the Service Callout policy in the oauth-okta-integration proxy is called to validate the end user identity in Okta. Upon successful authentication, the proxy then uses the OAuthV2 policy to generate the access token:
Now, let us test the {yourinitials}_Hipster-Products-API proxy which we protected with the OAuthV2 policy back in Module-2a lab 2.
(Optional) Navigate to the Trace screen of the {yourinitials}_Hipster-Products-API proxy, and start a trace session:
/products
endpoint without an Authorization header:GET /v1/_hipster-products-api/products HTTP/1.1
Host: -.apigee.net
Replace with the initials you used when creating the proxy
Replace with your actual Apigee org name, and with the deployment environment for your proxy (eg. test)
Notice a 401 unauthorized error response is returned because the access token was not sent in the request:
GET /v1/_hipster-products-api/products HTTP/1.1
Host: -.apigee.net
Authorization: Bearer
access_token
value you copied from the request to the oauth-okta-integration endpointOnce the access token is validated, a successful API response is returned:
If you like to learn by watching, here is a short video on the topic: Apigee/Okta Integration: Resource Owner / Password Grant Flow in Action
In this lab, you created an OAuth 2.0 access token endpoint to generate and refresh tokens using the resource owner password credentials (ROPC) grant type, validating end user credentials against an external identity provider (Okta). You also secured your API such that a valid token must be presented to authorize requests to your API.