SecureKey Code Examples
The topic is provided to help developers who are writing applications that will call a Logi application using Logi SecureKey Authentication. Language examples include:
General Information
Logi SecureKey Authentication is a technology we offer for use in "single sign-on" scenarios, where a non-Logi application (the "Parent" application), which conducts the initial user authentication, is integrated with a Logi application. SecureKey Authentication provides a method of securely accessing the integrated Logi app, and is discussed in detail in Use Logi SecureKey Authentication.
This topic provides examples of the Parent application code required to establish the flow of secure exchanges with a Logi application using SecureKey.
The examples, in several languages, are very generic and are only intended to provide the basic, necessary code. They're provided with the assumption that developers who undertake using them and extending their functionality have in-depth skills with the language selected. Other languages can also be used and success with them will, again, depend on the developer's skills.
C# (.NET)
The following code example is written in C#, for use with the .NET framework:
- /* set up the SecureKey call */
- // define parts of the SecureKey request URL
- string strLogiAppBaseURL = "http://<yourServer>/<LogiApp>";
- string UserName = "Bob";
- string Roles = "Admin";
- string Rights = "Manager";
- string ClientBrowserAddress = Request.ServerVariables["REMOTE_ADDR"];
- // build complete URL to Logi app
- string sGetKeyURl = "/rdTemplate/rdGetSecureKey.aspx?Username=" + UserName + "&Rights=" + Rights + "&Roles=" + Roles + "&ClientBrowserAddress=" + ClientBrowserAddress;
- string sFinalURL = strLogiAppBaseURL + sGetKeyURl;
- // create and send SecureKey request
- string sKey = "";
- HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(sFinalURL);
- HttpWebResponse webResponse;
- // attempt to make SecureKey call, read return into sKey
- try
- {
- webResponse = (HttpWebResponse)webRequest.GetResponse();
- Stream myStream;
- myStream = webResponse.GetResponseStream();
- StreamReader sr = new StreamReader(myStream);
- sKey = sr.ReadToEnd();
- }
- catch (Exception ex) // if not bad URL, send error message
- {
- if (ex.Message.IndexOf("401") != -1)
- {
- Response.Write("Not Found");
- }
- else
- {
- Response.Write(ex.Message.ToString());
- }
- }
Visual Basic (.NET)
The following code example is written in Visual Basic, for use with the .NET framework:
- ' set up the SecureKey call
- ' define parts of the SecureKey request URL
- Dim strLogiAppBaseURL As String = "http://<yourServer>/<LogiApp>"
- Dim strUserName = "Bob"
- Dim strUserRoles = "Admin"
- Dim strUserRights = "Manager"
- Dim strClientBrowserAddr As String = Request.UserHostAddress
- ' build complete URL to Logi app
- Dim strGetKeyURL As String = "/rdTemplate/rdGetSecureKey.aspx?Username=" & strUserName & "&Roles=" & strUserRoles & "&Rights=" & strUserRights & "&ClientBrowserAddress=" & strClientBrowserAddr
- Dim sFinalURL As String = strLogiAppBaseURL & strGetKeyURL
- ' define web request and response receiver
- Dim webRequest As Net.HttpWebRequest
- Dim webResponse As Net.WebResponse|
- webRequest = Net.HttpWebRequest.Create(strFinalURL)
- ' attempt to make SecureKey call
- Try
- webResponse = webRequest.GetResponse()
- Catch ex As Exception
- ' if server has Basic or NTLM authentication, try to set credentials from the current process
- If ex.Message.IndexOf("401") <> -1 Then
- webRequest.Credentials = Net.CredentialCache.DefaultCredentials
- webResponse = webRequest.GetResponse()
- Else
- Throw ex
- End If
- End Try
- ' receive the response and return it as function result
- Dim sr As New IO.StreamReader(webResponse.GetResponseStream())
- getSecureKey = sr.ReadToEnd()
Java
The following code example is written in Java, for use with a JSP file:
- <%@ page language="java" import="java.util.*,java.net.*,javax.servlet.*,java.io.*" %>
- <%
- // set up the SecureKey call
- // define parts of the SecureKey request URL
- String LogiAppURL;
- String Username;
- String Roles;
- LogiAppURL = "http://<yourServer>/<LogiApp>";
- Username = "Bob";
- Roles = "1";
- // build complete URL to Logi app
- String getKeyURL = "/rdTemplate/rdGetSecureKey.aspx?Username=" + URLEncoder.encode(Username) + "&Roles=" + URLEncoder.encode(Roles) + "&ClientBrowserAddress=" + URLEncoder.encode(request.getRemoteAddr());
- String finalURL = LogiAppURL + getKeyURL;
- // back-end handshake, passing username, roles and rights to Logi app
- URL url = new URL(response.encodeRedirectURL(finalURL));
- // attempt to make SecureKey call
- try {
- // open the connection
- URLConnection conn = url.openConnection();
- conn.setDoOutput(true);
- // debugging aid: use StreamWriter to write POST data
- // OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
- // wr.write(data);
- // wr.flush();
- // wr.close();
- // get the response
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- StringBuffer sb = new StringBuffer();
- String line;
- while ((line = rd.readLine()) != null) {
- sb.append(line);
- }
- rd.close();
- // set the url with the retrieved SecureKey parameter
- LogiAppURL = LogiAppURL + "/rdPage.aspx?rdSecureKey=" + sb.toString();
- // redirect to the application with SecureKey parameter
- response.sendRedirect(LogiAppURL);
- }
- catch (Exception e) {
- throw new Exception("Failed to get SecureKey, request URL: [" + url + "], POST data: [" + data +"]", e);
- }
- %>
Node.js
The following code example is written in Node.js:
- var express = require("express");
- var http = require("http");
- var app = express();
- var LogiAppURL = "http://<yourServer>/<LogiApp>";
- var UserName = "Bob";
- var Roles = "Admin";
- var Rights = "Manager";
- var MessageBack = "";
- /* create an app to listen on the port # specified in the app arg */
- app.get("*", function(req, resM) {
- /* send the SecureKey request */
- http.get(LogiAppURL + "/rdTemplate/rdGetSecureKey.aspx?"Username=" + UserName + "&Roles=" + Roles + "&Rights=" + Rights + "&ClientBrowserAddress=" + resM.connection.remoteAddress, function(res) {
- /* when the key is returned */
- res.on("data", function(chunk) {
- MessageBack = chunk;
- /* send a response to the browser (using our Embedded Report API in this example) */
- resM.send("<p style=\"font-size:20px;text-align:center;\" > This is a node page </p> <div id=\"divNd\" data-autoSizing=\"all\" data-applicationUrl=\"' + LogiAppURL + '\" data-report=\"Default\" data-linkParams=\"{'rdSecurekey' : '" + MessageBack + "'}\" > </div> <script src=\"' + LogiAppURL + '/rdTemplate/rdEmbedApi/rdEmbed.js\" type=\"text/Javascript\"></script>" );
- });
- /* server-side error logging */
- }).on('error', function(e) {
- console.log("Got error: " + e.message);
- });
- });
- console.log("Web application opened.");
- app.listen(1337);
Perl
The following code example is written in the Perl language:
- #!/usr/local/bin/perl
- # Requires LWP to be installed
- use LWP::Simple;
- # define parts of the SecureKey request URL
- my $logiAppUrl = 'http://<yourServer>/<LogiApp>';
- my $user = 'Bob';
- my $role = 'Admin';
- my $rights = 'Manager';
- my $clientAddress = print $ENV{REMOTE_ADDR};
- # build complete URL to Logi app
- my $url = $logiAppUrl . '/rdTemplate/rdGetSecureKey.aspx?Username=' . $user . '&Role=' . $role . '&Rights=' . $rights . '&ClientBrowserAddress=' . $clientAddress;
- # make the SecureKey call and get the response
- my $response = get $url;
- # The response should be used as the rdSecureKey parameter value in HTML or Javascript in a Div or iFrame
- print $response;
Ruby
The following code example is written in the Ruby language:
- require 'net/http'
- # define parts of the SecureKey request URL
- LogiAppURL = "http://<yourServer>/<LogiApp>"
- username = "Bob"
- roles = "Admin"
- rights = "Manager"
- clientaddr = Socket.unpack_sockaddr_in(socket.getpeername) >> ip # ruby v1.9
- # build complete URL to Logi app
- getKeyURL = "/rdTemplate/rdGetSecureKey.aspx?Username=" << username << "&Rights=" << rights+ "&Roles=" << roles << "&ClientBrowserAddress=" << clientaddr finalURL = LogiAppURL << getKeyURL
- # make the SecureKey call and get the response
- result = Net::HTTP.get_response(URI.parse(finalURL))
- sKey = result.body
- print sKey