Introduction
In the Basic Service example, we learned to perform a heartbeat check on a service to monitor its health.
Moreover, sample tests to expose the basic functionalities of a SOAJS service, and those of the IMFV.
In this section, one of the APIs from the Basic Service example was secured with oAuth. When securing a service with oAuth, the service becomes accessible only to users with valid oAuth credentials.
Every request made to the service is first validated by the SOAJS oAuth Service before being forwarded to the service.
Code Walkthrough
This page shows you how to interact with example02 service after you install it and run it.
The code walkthrough is located in a sub page and explained in depth. Click here to read the explanation.
Service Exploration
To turn on the gateway in a development environment you just need to start the environment as follow
sudo soajs services start --env=example
Verify auth
To verify if oauth is running you need to login to soajs console and
- go under deploy tab
- select environment example
- click on SOAJS catalog from the left menu
- expand auth item
- click on maintenance operations
- select heartbeat
Using the Service APIs
In the Basic Service example, the "testGet" API was not designed to be secured with oAuth. In this example we added this security option to it.
The following example is a request to the testGet API, without providing the required authentication credentials.
# go to examples/example02 and start the service cd soajs.examples/example02/ # start the soajs example environment sudo soajs services start --env=example # export necessary environment variables to create local awareness export SOAJS_ENV=EXAMPLE export SOAJS_SRVPORT=4022 export SOAJS_DEPLOY_MANUAL=1 export SOAJS_REGISTRY_API=127.0.0.1:21000 # start service node . # hit the API curl -X GET "http://127.0.0.1:20000/example02/buildName?firstName=John&lastName=Smith"
As expected, the API replied with an error stating that The access token was not found. Thus, the user was not permitted to access the services of this API.
{result: false, errors: {codes: [ 400 ],details: [ { code: 400, message: "The access token was not found"}]}}
Login to oAuth
The user must login to oAuth and get an access token.
The code block below represents a request to the oAuth/token API.
The header contains the tenant key. More details regarding the tenant key can be found in the Multitenancy section in the documentation.
The Authorization in the header is used by oAuth to validate both posted body and is explained in detail in oAuth section under documentation.
The body, on the other hand, accepts three input parameters: username, password, grant_type.
curl -X POST -H "Authorization: Basic MTBkMmNiNWZjMDRjZTUxZTA2MDAwMDAxOnNoaGggdGhpcyBpcyBhIHNlY3JldA==" -H "key:aa39b5490c4a4ed0e56d7ec1232a428f771e8bb83cfcee16de14f735d0f5da587d5968ec4f785e38570902fd24e0b522b46cb171872d1ea038e88328e7d973ff47d9392f72b2d49566209eb88eb60aed8534a965cf30072c39565bd8d72f68ac" "http://127.0.0.1:20000/oauth/token" -d 'username=oauthuser&password=oauthpassword&grant_type=password'
The corresponding response contains the corresponding access_token, which expires in one hour (in this example), and refresh_token, that is used once the access_token expires.
{"token_type":"bearer","access_token":"30f3a13fcdb60cde1cdf576634cbb7777df31177","expires_in":3600,"refresh_token":"6ea967dfe6c005d86b6fb0b0331ed52b89a1cee7"}
TestGet with a valid oAuth access token
What follows is a new request to the testGet API. However, the request in this example contains the valid access_token obtained above. As can be seen in the code block below, the access_token is appended to the request.
curl -X GET "http://127.0.0.1:20000/example02/buildName?firstName=John&lastName=Smith&access_token=30f3a13fcdb60cde1cdf576634cbb7777df31177"
As a result, after validating the legitimacy of the access_token, the API responds with a successful response to the user.
{"result":true,"data":{"fullName":"John Smith"}}
Add Comment