Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.07 KB | None | 0 0
  1. <%-- Copyright 2001-2009 WiKID Systems, Inc. --%>
  2. <%-- License: WiKID Systems, Inc. All Rights Reserved. --%>
  3. <%@ page contentType="text/html; charset=UTF-8" %>
  4. <%@ page import="com.wikidsystems.client.*" %>
  5. <%@ page import="com.wikidsystems.util.Config" %>
  6. <%@ page import="com.wikidsystems.data.User" %>
  7. <%@ page import="java.util.Iterator" %>
  8. <%@ page import="com.wikidsystems.data.Token" %>
  9.  
  10. <html>
  11. <head>
  12. <title>
  13. Example wClient Usage nikos
  14. </title>
  15. </head>
  16. <body bgcolor="#ffffff">
  17. <!--
  18. // Instantiation (on the WAS)
  19. /* This section instantiates the connection between the network client application and the WiKID server. For this
  20. to succeed, the network client must have been issued a certificate from the WiKID server. The certificate is
  21. contained withing a PKCS12 certificate store and requires a passphrase to access.
  22.  
  23. When the wClient object is instantiated it will load the cert and establish a persistent authenticated
  24. SSL connection. This is normally done once per server or application and shared by multiple threads. In
  25. this example the object is created and destroyed each page request. This greatly (1000 times) increases
  26. overhead of the process but allows all the functions to be shown in this single example page.
  27.  
  28. Parameters are:
  29.  
  30. wClient(String host, int port, String keyfile, String pass)
  31.  
  32. host = IP address of WIKID server
  33. port = TCP port number to connect to (default 8388)
  34. keyfile = Path to the PKCS12 certificate file
  35. pass = Passphrase to open the PKCS12 file
  36.  
  37.  
  38. */
  39. -->
  40.  
  41. <%
  42. String defaultservercode = "192168010005";
  43. String status = "";
  44. String chall;
  45. wClient wc;
  46. // if (session.getServletContext().getAttribute("wClient") == null) {
  47. wc = new wClient("192.168.10.5", 8388,"/home/ndrico/WiKID/ubuntuNetworkClient.p12", "N+SFr1!C_s0",
  48. Config.getValue("BASEPATH") + "private/CACertStore", "changeit");
  49. // NOTE: The passphrase above "changeit" is the default passphrase for the CA trust store as delivered
  50. // by Sun with Java. If you change this value you will also need to change the passphrase used to
  51. // encrypt the CACertStore file. You must change both if you change either.
  52. session.getServletContext().setAttribute("wClient", wc);
  53. //} else {
  54. // wc = (wClient) session.getServletContext().getAttribute("wClient");
  55. // }
  56. if (wc.isConnected()) {
  57.  
  58. %>
  59.  
  60. <!--
  61. // Instantiation (on foreign system)
  62. /* This section instantiates the connection between the network client application and the WiKID server. For this
  63. to succeed, the network client must have been issued a certificate from the WiKID server. The certificate is
  64. contained withing a PKCS12 certificate store and requires a passphrase to access. The caStore is a Java Keystore
  65. that contains the certificates of the WiKID servers you will connect to with this client. You can copy this from
  66. WAS as the /opt/WiKID/private/CACertStore. The caStorePass is the passphrase to open the caStore.
  67.  
  68. When the wClient object is instantiated it will load the cert and establish a persistent authenticated
  69. SSL connection. This is normally done once per server or application and shared by multiple threads. In
  70. this example the object is created and destroyed each page request. This greatly (1000 times) increases
  71. overhead of the process but allows all the functions to be shown in this single example page.
  72.  
  73. Parameters are:
  74.  
  75. wClient(String host, int port, String keyfile, String pass)
  76.  
  77. host = IP address of WIKID server
  78. port = TCP port number to connect to (default 8388)
  79. keyfile = Path to the PKCS12 certificate file
  80. pass = Passphrase to open the PKCS12 file
  81. caStore - The certificate authority store for validating the WAS server certificate <-- DO NOT USE Java's cacerts file
  82. caStorePass - The passphrase securing the caStore file
  83.  
  84.  
  85.  
  86.  
  87. String status="";
  88. String chall="";
  89.  
  90. wClient wc = new wClient("192.168.0.1", 8388, Config.getValue("BASEPATH")+"private/localhost.p12", "passphrase",
  91. Config.getValue("BASEPATH")+"private/caStore", "changeit");
  92.  
  93.  
  94. -->
  95.  
  96. <!--
  97. // Registration
  98.  
  99. /* The registration process associates a device that has resitered it's key with the WiKID server to a userid
  100. that represents a individual with rights in the network. Devices can register with the server at will but
  101. have no access rights until registered to a userid. Inactive registrations are purged from the system
  102. automatically.
  103.  
  104. The registration process should be completed *only* after validating that the user is not an imposter. This
  105. may be done in various ways according to local security policy. It is assumed that whatever validation is
  106. required has been completed successfully before callint the registerUsername function.
  107.  
  108. Parameters are:
  109.  
  110. registerUsername(String user, String regcode, String servercode)
  111.  
  112. user = userid with which to associate device
  113. regcode = the registration code provided to the device
  114. servercode = the 12-digit code that represents the server/domain
  115.  
  116. This method returns an integer representing the result of the registration.
  117.  
  118.  
  119. */
  120. -->
  121.  
  122. <%
  123. int res;
  124. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("register")) {
  125. res = wc.registerUsername(request.getParameter("user"), request.getParameter("regcode"), request.getParameter("servercode"));
  126. if (res == 0) {
  127. status = "Success";
  128. } else {
  129. status = "Failed (" + res + ")";
  130. }
  131. }
  132. %>
  133.  
  134. <!--
  135. // Login Online
  136.  
  137. /* This function is the normal-state login for users. This is called when the users device is connected to
  138. the network and able to directly request a passcode for access.
  139.  
  140. Parameters are:
  141.  
  142. CheckCredentials(String user, String passcode, String servercode)
  143.  
  144. user = userid to validate credentials for
  145. passcode = time-bounded, 1 use passcode
  146. servercode = 12-digit code that represents the server/domain
  147.  
  148. This method returns a boolean representing sucessful or unsuccessful authentication
  149.  
  150. */
  151. -->
  152.  
  153. <%
  154. boolean isValid;
  155. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Check Online")) {
  156. isValid = wc.CheckCredentials(request.getParameter("user"), request.getParameter("passcode"), request.getParameter("servercode"));
  157. if (isValid) {
  158. status = "Success";
  159. } else {
  160. status = "Authentication Failed";
  161. }
  162. }
  163. %>
  164.  
  165.  
  166. <!--
  167. // Login Offline
  168.  
  169. /* This function implements the challenge-reponse authentication for offline devices. Users are given a
  170. random challenge and the signed response is returned and validated.
  171.  
  172. Parameters are:
  173.  
  174. CheckCredentials(String user, String challenge, String response, String servercode)
  175.  
  176. user = userid to validate credentials for
  177. challenge = the challeng value provided to the user
  178. response = the hashed/signed responss from the device
  179. servercode = 12-digit code that represents the server/domain
  180.  
  181.  
  182. */
  183. -->
  184.  
  185. <%
  186.  
  187. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Check Offline")) {
  188. isValid = wc.CheckCredentials(request.getParameter("user"), request.getParameter("challenge"), request.getParameter("response"), request.getParameter("servercode"));
  189. if (isValid) {
  190. status = "Success";
  191. } else {
  192. status = "Authentication Failed";
  193. }
  194. }
  195. %>
  196.  
  197.  
  198. <!--
  199. // Add additional device to existing userid
  200.  
  201. /* This method is used to add an additional device to the users account. It follows the same process as a
  202. normal registration but requires a passcode from a device already registered to the userid. This method
  203. will authenticate the user with the passcode provided prior to registering the new device.
  204.  
  205. Parameters are:
  206.  
  207. registerUsername(String user, String regcode, String servercode, String passcode)
  208.  
  209. user = userid with which to associate device
  210. regcode = the registration code provided to the device
  211. servercode = the 12-digit code that represents the server/domain
  212. passcode = time-bounded, 1 use passcode from a device already registered to this user
  213.  
  214. This method returns an integer representing the result of the registration.
  215.  
  216.  
  217.  
  218. */
  219. -->
  220.  
  221. <%
  222.  
  223. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Add device")) {
  224. res = wc.registerUsername(request.getParameter("user"), request.getParameter("regcode"), request.getParameter("servercode"), request.getParameter("passcode"));
  225. if (res == 0) {
  226. status = "Success";
  227. } else {
  228. status = "Failed (" + res + ")";
  229. }
  230. }
  231. %>
  232.  
  233.  
  234. <!--
  235. // Pre-register a user
  236.  
  237. /* This method supports user pre-registration. You may upload a list of userids and
  238. pre-registration codes into the server via the WiKIDAdmin interface. Users can then
  239. use the pre-registration code provided to them securely by the administrator in
  240. conjunction with the registration code provided by the WiKID token to register in
  241. an expidited manner.
  242.  
  243. Parameters are:
  244.  
  245. preRegister(String preRegistrationCode, String tokenRegistrationCode, String domainCode)
  246.  
  247. preRegistrationCode = code associated with the username that was uploaded to the server
  248. tokenRegistrationCode = the registration code provided by the token
  249. servercode = the 12-digit code that represents the server/domain
  250.  
  251. This method returns an integer representing the result of the registration.
  252.  
  253. */
  254. -->
  255.  
  256. <%
  257.  
  258. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Pre-register")) {
  259. res = wc.preRegister(request.getParameter("preregcode"), request.getParameter("tokenregcode"), request.getParameter("servercode"));
  260. if (res == 0) {
  261. status = "Success";
  262. } else {
  263. status = "Failed (" + res + ")";
  264. }
  265. }
  266. %>
  267.  
  268. <!--
  269. // Add additional device to existing userid WITHOUT passcode check
  270.  
  271. /* This method is used to add an additional device to the users account. It follows the same process as a
  272. normal registration but requires a passcode from a device already registered to the userid. This method
  273. will authenticate the user with the passcode provided prior to registering the new device.The
  274. server must have the ALLOW_REG_WITHOUT_PASSCODE parameter devfined and set to True for this call to
  275. succeed.
  276.  
  277. Parameters are:
  278.  
  279. registerUsernameWithoutCheck(String user, String regcode, String servercode)
  280.  
  281. user = userid with which to associate device
  282. regcode = the registration code provided to the device
  283. servercode = the 12-digit code that represents the server/domain
  284. passcode = time-bounded, 1 use passcode from a device already registered to this user
  285.  
  286. This method returns an integer representing the result of the registration.
  287.  
  288.  
  289.  
  290. */
  291. -->
  292.  
  293. <%
  294.  
  295. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Add device without passcode")) {
  296. res = wc.registerUsernameWithoutCheck(request.getParameter("user"), request.getParameter("regcode"), request.getParameter("servercode"));
  297. if (res == 0) {
  298. status = "Success";
  299. } else {
  300. status = "Failed (" + res + ")";
  301. }
  302. }
  303. %>
  304.  
  305.  
  306. <%--Test No Preamble
  307. This generates an out-of sync transaction, possible caused bu network errors, to test the connection resiliency
  308. --%>
  309.  
  310. <%
  311.  
  312. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Test No Preamble")) {
  313. wc.testNoPreamble();
  314. status = "Invalid Transaction Sent";
  315. }
  316. %>
  317.  
  318.  
  319. <!--
  320. // Find a user by servercode + userid
  321.  
  322. /* This method is used to retrieve a user object from the server. The network client certificate that was used to establish
  323. the wClient connection must be authorized to perform this action.
  324.  
  325. Parameters are:
  326.  
  327. findUser(String servercode, String userID)
  328.  
  329. servercode = the 12-digit code that represents the server/domain
  330. userID = userid of the user to retrieve
  331.  
  332. This method returns a com.wikidsystems.data.User representing the user on the WAS.
  333.  
  334. */
  335. -->
  336.  
  337. <%
  338.  
  339. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Find User")) {
  340. User user = wc.findUser(request.getParameter("servercode"), request.getParameter("user"));
  341. if (user != null) {
  342. session.setAttribute("wikidUser", user);
  343. StringBuffer sb = new StringBuffer(user.getUserID());
  344. sb.append(" / ").append(user.getCreation())
  345. .append(" / ").append(user.getStatus());
  346. status = sb.toString();
  347. } else {
  348. status = "Failed (No user returned)";
  349. }
  350.  
  351. }
  352. %>
  353.  
  354. <!--
  355. // Update the previously "found" user
  356.  
  357. /* This method is used to update a user object on the server. The network client certificate that was used to establish
  358. the wClient connection must be authorized to perform this action.
  359.  
  360. Parameters are:
  361.  
  362. updateUser(User updatedUser)
  363.  
  364. updatedUser = com.wikidsystems.data.User object returned from findUser()
  365.  
  366. This method returns an integer representing the result of the update on the WAS.
  367.  
  368. */
  369. -->
  370.  
  371.  
  372. <%
  373.  
  374. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Update User")) {
  375. User user = (User) session.getAttribute("wikidUser");
  376. user.setBads(Integer.parseInt(request.getParameter("bads")));
  377. user.setStatus(Integer.parseInt(request.getParameter("status")));
  378. Iterator it = user.getTokens().iterator();
  379. while (it.hasNext()) {
  380. Token t = (Token) it.next();
  381. long did = t.getDeviceID();
  382. if (request.getParameter(did + "_offs") != null)
  383. t.setOffs(Integer.parseInt(request.getParameter(did + "_offs")));
  384. if (request.getParameter(did + "_bads") != null)
  385. t.setBads(Integer.parseInt(request.getParameter(did + "_bads")));
  386. if (request.getParameter(did + "_status") != null)
  387. t.setStatus(Integer.parseInt(request.getParameter(did + "_status")));
  388. if (request.getParameter(did + "_delete") != null) t.setForDeletion(true);
  389. }
  390. status = "Result: " + wc.updateUser(user);
  391. session.removeAttribute("wikidUser");
  392. }
  393. %>
  394.  
  395.  
  396. <!--
  397. // Delete the previously "found" user
  398.  
  399. /* This method is used to delete a user object on the server. The network client certificate that was used to establish
  400. the wClient connection must be authorized to perform this action.
  401.  
  402. Parameters are:
  403.  
  404. deleteUser(User updatedUser)
  405.  
  406. deletedUser = com.wikidsystems.data.User object returned from findUser()
  407.  
  408. This method returns an integer representing the result of the update on the WAS.
  409.  
  410. */
  411. -->
  412.  
  413. <%
  414.  
  415. if (request.getParameter("action") != null && request.getParameter("action").equalsIgnoreCase("Delete User")) {
  416. User user = (User) session.getAttribute("wikidUser");
  417. status = "Result: " + wc.deleteUser(user);
  418.  
  419. }
  420. %>
  421.  
  422. <h1>
  423. <%=status%>
  424. </h1>
  425.  
  426. <%
  427. // if(request.getParameter("action")==null || request.getParameter("action").equalsIgnoreCase("Find User")){
  428.  
  429. //generate a random number for the offline challenge
  430. java.security.SecureRandom sr = java.security.SecureRandom.getInstance("SHA1PRNG");
  431. long num = sr.nextLong();
  432. while (num <= 1000000000l) {
  433. num = sr.nextLong();
  434. }
  435. chall = num + "";
  436. chall = chall.substring(0, 8);
  437.  
  438.  
  439. %>
  440. <h1>
  441. This page demonstrates the general usage of the wClient component.
  442. </h1>
  443.  
  444. <!-- Registration -->
  445. <hr/>
  446. <h2>Registration:
  447. </h2>
  448.  
  449. <form action="./example.jsp" method="POST">
  450. <br><br>
  451. UserID: <input type="text" size="25" name="user" value=""/><br/>
  452. Registration code: <input type="text" size="12" name="regcode" value=""/><br/>
  453. Domain code: <input type="text" size="16" name="servercode" value="<%=defaultservercode%>"/>
  454.  
  455. <p/>
  456. <input type="submit" name="action" value="Register"><br/>
  457. </form>
  458.  
  459. <!-- Online Login -->
  460. <hr/>
  461. <h2>Online Login:
  462. </h2>
  463.  
  464. <form action="./example.jsp" method="POST">
  465. <br><br>
  466. UserID: <input type="text" size="25" name="user" value=""/><br/>
  467. Passcode: <input type="text" size="12" name="passcode" value=""/><br/>
  468. Domain code: <input type="text" size="16" name="servercode" value="<%=defaultservercode%>"/>
  469.  
  470. <p/>
  471. <input type="submit" name="action" value="Check Online"><br/>
  472. </form>
  473.  
  474. <!-- Offline Login -->
  475. <hr/>
  476. <h2>Offline Login:
  477. </h2>
  478.  
  479. <form action="./example.jsp" method="POST">
  480. <br><br>
  481. UserID: <input type="text" size="25" name="user" value=""/><br/>
  482. Challenge: <%= chall %> <input type="hidden" name="challenge" value="<%=chall%>"/><br/>
  483. Response: <input type="text" size="12" name="response" value=""/><br/>
  484. Domain code: <input type="text" size="16" name="servercode" value="<%=defaultservercode%>"/>
  485.  
  486. <p/>
  487. <input type="submit" name="action" value="Check Offline"><br/>
  488. </form>
  489.  
  490. <!-- Add device -->
  491. <hr/>
  492. <h2>Add device:
  493. </h2>
  494.  
  495. <form action="./example.jsp" method="POST">
  496. <br><br>
  497. UserID: <input type="text" size="25" name="user" value=""/><br/>
  498. Registration code: <input type="text" size="12" name="regcode" value=""/><br/>
  499. Passcode: <input type="text" size="12" name="passcode" value=""/><br/>
  500. Domain code: <input type="text" size="16" name="servercode" value="<%=defaultservercode%>"/>
  501.  
  502. <p/>
  503. <input type="submit" name="action" value="Add device"><br/>
  504. </form>
  505.  
  506. <!-- Pre-registration -->
  507. <hr/>
  508. <h2>Pre-registration:
  509. </h2>
  510.  
  511. <form action="./example.jsp" method="POST">
  512. <br><br>
  513. Token Registration code: <input type="text" size="12" name="tokenregcode" value=""/><br/>
  514. Pre-registration code: <input type="text" size="25" name="preregcode" value=""/><br/>
  515. Domain code: <input type="text" size="16" name="servercode" value="<%=defaultservercode%>"/>
  516. <p/>
  517. <input type="submit" name="action" value="Pre-register"><br/>
  518. </form>
  519.  
  520. <!-- Add device without passcode-->
  521. <hr/>
  522. <h2>Add device without passcode:
  523. </h2>
  524.  
  525. <form action="./example.jsp" method="POST">
  526. <br><br>
  527. UserID: <input type="text" size="25" name="user" value=""/><br/>
  528. Registration code: <input type="text" size="12" name="regcode" value=""/><br/>
  529. Domain code: <input type="text" size="16" name="servercode" value="<%=defaultservercode%>"/>
  530.  
  531. <p/>
  532. <input type="submit" name="action" value="Add device without passcode"><br/>
  533. </form>
  534. <!-- TX no Preamble -->
  535.  
  536. <hr/>
  537. <h2>Test bad transaction:
  538. </h2>
  539.  
  540. <form action="./example.jsp" method="POST">
  541. <input type="submit" name="action" value="Test No Preamble"><br/>
  542. </form>
  543.  
  544.  
  545. <!-- Find User By Name -->
  546. <hr/>
  547. <h2>Find User By Name:
  548. </h2>
  549.  
  550. <form action="./example.jsp" method="POST">
  551. <br><br>
  552. UserID: <input type="text" size="25" name="user" value=""/><br/>
  553. Domain code: <input type="text" size="16" name="servercode" value="<%=defaultservercode%>"/>
  554.  
  555. <p/>
  556. <input type="submit" name="action" value="Find User"><br/>
  557. </form>
  558.  
  559. <% if (session.getAttribute("wikidUser") != null) {
  560. User user = (User) session.getAttribute("wikidUser");
  561. %>
  562. <!-- Update User -->
  563.  
  564. <hr/>
  565. <h2>Update User:
  566. </h2>
  567.  
  568. <form action="./example.jsp" method="POST">
  569. <br><br>
  570. UserID: <%=user.getUserID()%>@<%=user.getDomainCode()%><br/>
  571. Bad Passcode Att: <input type="text" size="3" name="bads" value="<%=user.getBads()%>"/><br/>
  572. Status: <input type="text" size="2" name="status" value="<%=user.getStatus()%>"/><br/>
  573. <table border=1>
  574. <tr>
  575. <td>Device ID</td>
  576. <td>Offline Auths</td>
  577. <td>Bad Pin Att</td>
  578. <td>Status</td>
  579. <td>Delete</td>
  580. </tr>
  581. <%
  582. Iterator it = user.getTokens().iterator();
  583. while (it.hasNext()) {
  584. Token t = (Token) it.next();
  585. %>
  586. <tr>
  587. <td><%=t.getDeviceID()%>
  588. </td>
  589. <td><input type="text" size="8" name="<%=t.getDeviceID()%>_offs" value="<%=t.getOffs()%>"/></td>
  590. <td><input type="text" size="8" name="<%=t.getDeviceID()%>_bads" value="<%=t.getBads()%>"/></td>
  591. <td><input type="text" size="8" name="<%=t.getDeviceID()%>_status" value="<%=t.getStatus()%>"/></td>
  592. <td><input type="checkbox" name="<%=t.getDeviceID()%>_delete"/></td>
  593. </tr>
  594. <%
  595. }
  596. %>
  597. </table>
  598. <input type="submit" name="action" value="Update User"><br/>
  599. </form>
  600.  
  601. <!-- Delete User -->
  602.  
  603. <hr/>
  604. <h2>Delete User:
  605. </h2>
  606.  
  607. <form action="./example.jsp" method="POST">
  608. <br><br>
  609. UserID: <%=user.getUserID()%>@<%=user.getDomainCode()%><br/>
  610. <input type="submit" name="action" value="Delete User"><br/>
  611. </form>
  612. <%
  613. }
  614. %>
  615.  
  616. <%
  617.  
  618. } else {
  619. %>
  620. <h1>The wClient connection to the server was NOT successfully established </h1>
  621. <%
  622. }
  623. %>
  624.  
  625. </body>
  626. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement