The below snippets represent the content of both index.js and config.js that represent the oAuth Security Service (example 02).
The code of oAuth Security Service resembles that of the Basic SOAJS Service. However, in this example, oAuth is used, as will be seen in the code snippets below.
Below each snippet is a small explanation of how each of them works.
'use strict'; module.exports = { "serviceName": "example02", //service name "serviceGroup": "SOAJS Example Services", //service group "serviceVersion": 1, //service version "servicePort": 4011, //service port "extKeyRequired": false, //set to true is the service is multi-tenant "oauth": true, //set to true is service is protected by oauth "errors": {}, //object that contains the error codes of the service "schema": { //object that contains the APIs schema "/buildName": { //one API "_apiInfo":{ //API labels "l": "Build Name" }, "firstName": { //API input "source": ['query.firstName'], "required": true, "default": "John", "validation": { "type": "string" } }, "lastName": { //API input "source": ['query.lastName'], "required": true, "validation": { "type": "string" } } } } };
The config file of example02 differs only by one line than that of example01.The only difference between the file of this example, and that of the previous one is that oAuth is set to true in this example.
Setting oAuth to true notifies the service that only requests with valid oAuth tokens are able to use the APIs of the service.
'use strict'; var soajs = require('soajs'); //require soajs var config = require('./config.js'); var service = new soajs.server.service(config); //create new service instance service.init(function () { //initialize the service service.get("/buildName", function (req, res) { //implement the API business logic var fullName = req.soajs.inputmaskData.firstName + ' ' + req.soajs.inputmaskData.lastName; res.json(req.soajs.buildResponse(null, { //generate API response fullName: fullName })); }); service.start(); //start the service });
The index.js file above contains the implementation of the buildName API, which takes as an input a firstName and a lastName, and then returns a response containing the full name.
As can be seen, integrating oAuth is as simple as adding one key to the config.js file. That being done, the service will only accept requests with valid oAuth tokens.
0 Comments