Here we get into the details of creating an extension endpoint in CDC (a.k.a Gigya) and its deployment in cloud foundry.
The challenge faced by CDC developer is to understand the behavior of CDC extension that is hosted elsewhere, the blog is an attempt to explain the intricacies of creating such an extension using Node.js , this will help in testing an extension events.
Review a step-by-step guide below to create extension using Node.js and test this using postman followed by steps to deploy this on a cloud foundry.
// require http modules first
const http = require('http');
//import app.js file
const app = require('./app');
app.get('/', (req, res, next) => {
res.status(200).send('You have called the root directory');
});
//define port to be used
const port = process.env.PORT || 3100;
const server = http.createServer(app);
server.listen(port, () => {
// let's print a message when the server run successfully
console.log("Server restarted successfully")
});
// **********************
// AUXILIAR FUNCTIONS
// **********************
const express = require('express');
const cache = require('memory-cache');
const jws = require('jws');
const jws_jwk = require('jws-jwk');
const Gigya = require('gigya').Gigya
// Initialize SDK with your API Key , data center, user key and Secret.
const gigya = new Gigya('<API_Key>', '<data_center>', '<user_key>' , '<Secret_key>');
const app = express();
const router = express.Router();
// import body-parser
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// Routes which should handle request
app.get('/register', (req, res, next) => {
res.json(["Array1","Array2","Array3"]);
});
app.post('/register', async (req, res) => {
var token = req.body.jws;
var decoded = jws.decode(token);
var jwk = cache.get('jwk');
//verify token
var body = JSON.parse(decoded.payload);
var ret = { status: 'OK' };
if (body.extensionPoint === 'OnBeforeAccountsRegister' && !body.data.params.email.endsWith('@xyz.com'))
{
ret.status = 'FAIL';
var customMessage = 'Email should belong to domain "xyz.com" ';
ret.data = {
validationErrors: [
{ fieldName: 'profile.email', message: customMessage },
]
}
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(ret));
});
// Global error handler - route handlers/middlewares which throw end up here
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.end();
});
//export app
module.exports = app;
The validation here is for the event OnBeforeAccountsRegister when email id ends with domain other than ‘xyz.com’, it throws an error ‘Email should belong to domain \"xyz.com\’
Sample payload for OnBeforeAccountsRegister is available at the link below
This is in "json" format which needs to be converted into "jws" format, online tool like jwt.io can help with conversion( here you can edit the decoded information one at a time as per our requirement and this in turn encoded the format in jws ), finally we need this( jws) to test in postman.
"apiKey": "3_ve...tyu",
"callID": "809.....169f",
"extensionPoint": "OnBeforeAccountsRegister",
"data": {
"params": {
"email": "test1@abc.com",
"password": "what ever password the user entered",
"profile": {
"firstName": "test"
},
"data": {
"terms": true
}
},
"context": {
"clientIP": "1.0.0.0"
}
}
}
{
"jws": "eyJhbGciOiJIUzI1NiIsImtpZCI6IlJEQXpSRVl5TmpCRk5USTVSak5ETURrd1JEUkJNMEZDUkRRM1FqQkNSRUpDUmpZNE9ESkZRUSJ9.eyJhcGlLZXkiOiIzX3ZlLi4udHl1IiwiY2FsbElEIjoiODA5Li4uLi4xNjlmIiwiZXh0ZW5zaW9uUG9pbnQiOiJPbkJlZm9yZUFjY291bnRzUmVnaXN0ZXIiLCJkYXRhIjp7InBhcmFtcyI6eyJlbWFpbCI6InRlc3QxQGFiYy5jb20iLCJwYXNzd29yZCI6IndoYXQgZXZlciBwYXNzd29yZCB0aGUgdXNlciBlbnRlcmVkIiwicHJvZmlsZSI6eyJmaXJzdE5hbWUiOiJ0ZXN0In0sImRhdGEiOnsidGVybXMiOnRydWV9fSwiY29udGV4dCI6eyJjbGllbnRJUCI6IjEuMC4wLjAifX19.3tzVu5n3NvXPDIDQaVpJ5UYEA8zs8KpsxAnacovkMac"
}
The final step is to test using postman and check for the response in case the email ends with domain other than xyz.com.
URL: http://localhost:3100/register
Body: the JWS format encoded in step above
Type: JSON
Response
Manifest Folder
Run the Powershell/msdos in administrator mode and navigate to folder that contain manifest.yml file
A. cf api https://api.cf.us10.hana.ondemand.com/ for example
B. cf login and if you have multiple org, choose the org where you shall upload this.
C. cf push
D . Make Note of the URL as this will be used in extension in CDC. You can find this URL within the service that is created in cloud foundry.
Test the extension by registering an account with email ending with domain other than 'xyz.com' .
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |