Duration : 30 mins
Persona : API Team/Security
You have an API that is consumed by trusted applications. You want to secure that API using two-legged OAuth (client credentials grant type).
Apigee Edge quickly lets you secure your APIs using out of the box OAuth policies. OAuth defines token endpoints, authorization endpoints, and refresh token endpoints. Apps call these endpoints to get access tokens, to refresh access tokens, and, in some cases, to get authorization codes. These endpoints refer to specific OAuth 2.0 policies that execute when the endpoint is called.
Most typically, the “client_credentials” grant type is used when the app is also the API resource owner. For example, an app may need to access a backend cloud-based storage service to store and retrieve data that it uses to perform its work, rather than data specifically owned by the end user. This grant type flow occurs strictly between a client app and the authorization server. An end user does not participate in this grant type flow. In this flow, Apigee Edge is the OAuth authorization server. Its role is to generate access tokens, validate access tokens, and pass authorized requests for protected resources on to the resource server.
An introduction to OAuth 2.0 is available on our documentation site here.
As part of this lab, you will:
Note: In Apigee Edge trial account orgs, the oauth endpoint should already have been created at the time of org provisioning. Check you API Proxies list under Develop -> API Proxies. If there is already a proxy labeled ‘oauth’, simply go to the proxy details page, click on ‘Deployment’ and ensure that it is deployed to the ‘test’ environment (Step 9 below). After that, you can skip this section and head to the next section.
Go to https://apigee.com/edge and log in. This is the Edge management UI.
Select Develop → API Proxies in the side navigation menu.
Download the API proxy bundle named oauth.zip
that implements the OAuth 2.0 client credentials grant type from here.
Back in the proxy creation wizard, click the Choose File button, select the oauth.zip
file you just downloaded, and click Next:
/products
to the end of the URL field.Then, click +API Product in the upper right of the screen:
You should see the new Developer you just created in the list.
Click on +App in the upper right of the screen:
curl
command from a terminal. The HTTP request to send is:POST /oauth/client_credential/accesstoken?grant_type=client_credentials HTTP/1.1
Host: -.apigee.net
Accept: application/json
Content-Type: application/x-www-form-urlencoded
client_id=&client_secret=
Replace with your actual Apigee org name, and with the deployment environment for your proxy (eg. test)
Replace and with your real Key and Secret you noted down previously
Here is an example curl
command (you will need to replace values as described above):
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: application/json' "https://-.apigee.net/oauth/client_credential/accesstoken?grant_type=client_credentials" -d 'client_id=&client_secret='
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.
curl
command in a terminal. The HTTP request to send is:GET /v1/_hipster-products-api/products HTTP/1.1
Host: -.apigee.net
Authorization: Bearer
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)
Add a header named Authorization, and in the value field enter Bearer followed by the access_token
value you copied after your last POST request
Here is an example curl
command (you will need to replace values as described above):
curl -X GET -H "Authorization: Bearer " https://-.apigee.net/v1/_hipster-products-api/products
If you prefer to skip the steps above and watch a video, you can view this short clip that shows how to implement two-legged OAuth on Apigee Edge https://youtu.be/0pah5J7yQTQ
Now that you have learned how to secure your API with OAuth 2.0, try to control the expiry of the access token that is generated using the [
In this lab you learned how to secure an API using two-legged OAuth 2.0 with the client credentials grant type, by creating an OAuth proxy to obtain an access token, and then using that token to validate requests to your API.
Useful Apigee documentation links on OAuth v2.0:
OAuth 2.0: Configuring a new API proxy - http://docs.apigee.com/api-services/content/understanding-default-oauth-20-configuration
Secure an API with OAuth - http://docs.apigee.com/tutorials/secure-calls-your-api-through-oauth-20-client-credentials
Community posts and articles with topic “OAuth 2.0” - https://community.apigee.com/topics/oauth+2.0.html
Search and Revoke tokens - https://community.apigee.com/articles/1571/how-to-enable-oauth-20-token-search-and-revocation.html
You may now proceed to Module-2b.