Friday, March 27, 2015

Calling an admin service with Signed JWT using Jaggery

This sample shows how to call admin services using Signed JWT from a Jaggery application.
The advantage of using Signed JWT header in the admin service call is, we do not have to hard code the user name or the password. (Though, In this sample I have hard coded the user name since this is a single page sample) This is very helpful when tenants are logging into the application. I have extracted the token generation logic. Please find the source code here. By building the source you can get the signed-jwt-header-1.0.0.jar
I'll be calling ESB's SequenceAdminService as an example. (My ESB is running in port offset 2)
ESB to authenticate the JWT, add the following jars into ESB_HOME/repository/components/dropins/ nimbus.jarsignedjwt-authenticator.jar

The quick way to setup the environment is creating a Jaggery application in wso2 application server.
Download the application server and unzip it in your installs location. And add the nimbus.jar into wso2as-<version>/repository/components/dropins and add the custom signed-jwt-header-1.0.0.jar into wso2as-<version>/repository/components/lib/
Create a directory in wso2as-<version>/repository/deployment/server/jaggeryapps/
For example admin-call, which is our project name. Inside the directory create the following index.jag


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%
var ws = require("ws");
var req = new ws.WSRequest();
var options = new Array();
var log = new Log();

var carbon = require('carbon');
var username = "admin";
var adminCallUtil = Packages.com.se.sample.AdminCallUtil;

var fullName = username + "@" + carbon.server.tenantDomain();
var authHeader = adminCallUtil.getAuthHeader(fullName);

options["HTTPHeaders"] = [{name: "Authorization", value: String(authHeader)}];

options.useSOAP = 1.2;
options.action = "urn:getSequence";

var payload = '<xsd:getSequence xmlns:xsd="http://org.apache.synapse/xsd">'
        + '<xsd:sequenceName>main</xsd:sequenceName>'
        + '</xsd:getSequence>';

var result;
try {
    req.open(options, "https://localhost:9445/services/SequenceAdminService", false);
    req.send(payload);
    result = req.responseE4X;
} catch (e) {
    log.error(e.toString());
}
print('<textarea rows="20" cols="100">');
print(result);
print('</textarea>');
%>

Start the application server by running 'sh wso2as-<version>/bin/wso2server.sh'
Go to the management console https://localhost:9443/carbon/
In the  application list view you'll see your project name as a jaggery application click on the Go To URL. You'll see the main sequence configuration as the output

No comments:

Post a Comment