Saturday, June 7, 2014

Writing a Jaggery Client for secured REST Resource




Every day, the more I learn new stuff,the more I feel exited !!!

Last week I had a delicious bite from {Jaggery.js} and let me share a bit with you all. :)

First we need to setup a {Jaggery.js} server to host the client application we are about to write.

Setting up Jaggery

  • Download {Jaggery.js} server from {Jaggery.js} site
  • Extract jaggery-0.9.0-SNAPSHOT.zip to a directory you prefer. lets call it JAGGERY_HOME
  • Navigate to JAGGERY_HOME/bin directory which contains all the Jaggery execution scripts.
  • Run sh JAGGERY_HOME/bin/server.sh   ( JAGGERY_HOME/bin/server.bat in windows) command to start the server.

Writing the Jaggery Client

  • Goto JAGGERY_HOME/apps directory. 
  • Create directory JaggeryClient inside JAGGERY_HOME/apps.
  • Create secureClient.jag and Base64Encode.jag files inside JAGGERY_HOME/apps/JaggeryClient directory.
  • secureClient.jag file content is as follows.                                                   

    1. </%function secure_call(request){                           
    2. var Base64Encode = require("Base64Encode.jag");  

    3. var username = request.username;                 
    4. var password = request.password;                 
    5. var url = request.url;                           
    6. var data = request.data;                                                                  
    7. var code = "Basic " + username + ":" + password;;
    8. var auth = Base64Encode.base64_encode(code);     
    9. var header = {"Authorization":auth};             
    10. var repleyJson = get(url, data ,header,"json");  
    11.                                                  
    12. response.content  = repleyJson;                  
    13. }                                                        
    14. %> 
  • Above request filed is in JSON format as follows.
{ username:"username",password:"password", url:"url of the resource", data:"" }
  • Jaggery GET method is used to access the rest resource and Basic Authentication username and password are sent, encoded in the header.
    • Base64Encode file content is as follows. [source: http://phpjs.org/functions/base64_encode/]     
      1. function base64_encode(data) {                           
      2.  var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
      3.   var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
      4.     ac = 0,
      5.     enc = '',
      6.     tmp_arr = [];

      7.   if (!data) {
      8.     return data;
      9.   }

      10.   do { // pack three octets into four hexets
      11.     o1 = data.charCodeAt(i++);
      12.     o2 = data.charCodeAt(i++);
      13.     o3 = data.charCodeAt(i++);

      14.     bits = o1 << 16 | o2 << 8 | o3;

      15.     h1 = bits >> 18 & 0x3f;
      16.     h2 = bits >> 12 & 0x3f;
      17.     h3 = bits >> 6 & 0x3f;
      18.     h4 = bits & 0x3f;

      19.     // use hexets to index into b64, and append result to encoded string
      20.     tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
      21.   } while (i < data.length);
      22.  enc = tmp_arr.join('');
      23.  var r = data.length % 3;
      24.  return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
      25. }
      26. %>
    • Now we are done with the Jaggery Client.

    Accessing secured resource via the Client

    • Next we try it with a secured REST Resource as shown in following secureCall.jag.
      1. <%                                                       
      2. var include = require("secureClient.jag");
      3. include.secure_call({ username:"replace_with_valid_username",password:"replace_with_valid_password", url:"replace_with_valid_url_to secured source", data:"" });
      4. %>
    • place above secureCall.jag file in JAGGERY_HOME/apps/JaggeryClient folder and goto http://localhost:9763/JaggerySecureClient/secureCall.jag to see whether it works. 
      Hope you enjoyed the taste. smiley

        Read More
        Designed ByBlogger Templates