Authorization Code grant type with WSO2 API Cloud

Shenavi de Mel
5 min readOct 30, 2017

--

Introduction

Instead of requesting the authorization straight from the resource owner (resource owner’s credentials), in this grant type, the client directs the resource owner to an authorization server. The authorization server works as an intermediary between the client and resource owner to issues an authorization code. After obtaining this authorization code then the client can call the token endpoint. The authorization server authenticates the resource owner (via the user-agent) and establishes whether the resource owner granted or denied the client’s access request. After which the client can call the /token endpoint and request an access token. [1]

Get Started

Let’s see how we can demonstrate this scenario using a sample application.

1. For this I will be using the oauth2 playground application. You can setup the application as instructed in thisdoc [2] or you can download the war file from here.

2. Once you have set up this sample application let’s log into API Cloud and go to your store. If you don’t have any existant APIs created you can create a simple API following my previous blog post [3]

3. Once you click on an API in the store you will be taken to a page where you need to subscribe to an API using an application. Let’s create a new application in this case. Choose the ‘New Application’ option in the dropdown.

4. When creating the application you can specify a desired name Then add the application.

5. After this step you will be directed back to where you need to subscribe with this application. Select the newly created application and subscribe to it.

6. The above step will redirect you to the subscriptions page. Generate keys for this created application. One very important point we need to remember in this step is that we need to specify the callback url for this use case to work. This callback url which you will see as an option in the configuration is a redirection URI in the client application which is used by the authorization server to send the client’s user-agent (usually web browser) back after granting access. Specify the callback url as http://localhost:8080/playground2/oauth2client since we will be using this application as our sample client.

Generate the keys and copy the consumer key and consumer secret values. We will be needing this in the latter steps. Click on the “Show Keys” option to make the keys visible.

7. Assuming that you have started the TOMCAT server access the sample application using this url [4] and click on the ‘import photos’ icon. In the UI that appears enter the following details and select Authorize.

Authorization Grant Type : Authorization Code

Client ID : (you need to copy this client id from the generated keys above. You need to copy the consumer key value here.)

Scope : Production

Callback URL : http://localhost:8080/playground2/oauth2client

Authorize Endpoint : https://api.cloud.wso2.com/authorize

8. Next you will get another screen to enter your credentials, specify the full username which appears on the right top hand corner of the API store UI. ex:shenavidemel@gmail.com@mydemo.

Login and then approve/approve always in the next screen.

At this step the client directs the user-agent to make the following HTTP request using TLS.

GET /authorize?response_type=code&client_id=<client_id>&scope=PRODUCTION&redirect_uri=%20http%3A%2F%2Flocalhost%3A8080%2Fplayground2%2Foauth2client HTTP/1.1 Host:api.cloud.wso2.com Content-Type:application/x-www-form-urlencoded

9. After this the authorization server redirects the user-agent by sending the following HTTP response and this authorization code will be displayed in the next UI which appears.

HTTP 1.1, 302 Found Location: http://localhost:8080/playground2/oauth2client?code=<authorization_code>
10. Provide the details below to get the access token, notice that you will be receiving the authorization code mentioned in above in this UI.

Callback URL: http://localhost:8080/playground2/oauth2client
Access Token Endpoint: https://gateway.api.cloud.wso2.com/token
Client Secret: (client secret received at the application registration in the API cloud in above step

In order to get the access token our client application will be making a request to the token endpoint of the API cloud with the authorization code received from above which will generate the access token for us. See the request that gets sent to the token endpoint below.

POST /token HTTP/1.1 Host:gateway.api.cloud.wso2.comAuthorization: Basic base64<client id : client secret>Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA&redirect_uri=%20http%3A%2F%2Flocalhost%3A8080%2Fplayground2%2Foauth2client

11. Now that you have received your access token. You can provide this token and invoke the API which you created in the API Cloud successfully.

curl -k -X GET — header ‘Accept: text/xml’ — header ‘Authorization: Bearer <access_token_received_above>’ ‘https://gateway.api.cloud.wso2.com/t/mydemo/phoneverify/1.0.0/CheckPhoneNumber?PhoneNumber=123456&LicenseKey=0'

Now we have successfully used the authorization code grant type to generate and access token in the API Cloud

References

[1] https://docs.wso2.com/pages/viewpage.action?pageId=45951037
[2] https://docs.wso2.com/display/IS510/Setting+Up+the+Sample+Webapp
[3] http://shenavid.blogspot.com/2015/10/creating-api-using-swagger.html
[4] http://localhost:8080/playground2/
[5] https://docs.wso2.com/display/IS510/Authorization+Code+Grant
[6] https://docs.wso2.com/display/AM1100/Generating+Access+Tokens+with+Authorization+Code+-+Authorization+Code+Grant+Type

--

--

Shenavi de Mel
Shenavi de Mel

Written by Shenavi de Mel

Lead Solutions Engineer at WSO2 | Loves coding | Loves writing

No responses yet