authentication.inc.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Authentication functions
  4. *
  5. * @package Swekey
  6. */
  7. ?>
  8. <script>
  9. var g_SwekeyPlugin = null;
  10. // -------------------------------------------------------------------
  11. // Create the swekey plugin if it does not exists
  12. function Swekey_Plugin()
  13. {
  14. try
  15. {
  16. if (g_SwekeyPlugin != null)
  17. return g_SwekeyPlugin;
  18. if (window.ActiveXObject)
  19. {
  20. g_SwekeyPlugin = document.getElementById("swekey_activex");
  21. if (g_SwekeyPlugin == null)
  22. {
  23. // we must create the activex that way instead of
  24. // new ActiveXObject("FbAuthAx.FbAuthCtl");
  25. // otherwise SetClientSite is not called and
  26. // we can not get the url
  27. var div = document.createElement('div');
  28. div.innerHTML='<object id="swekey_activex" style="display:none" CLASSID="CLSID:8E02E3F9-57AA-4EE1-AA68-A42DD7B0FADE"></object>';
  29. // Never append to the body because it may still loading and it breaks IE
  30. document.body.insertBefore(div, document.body.firstChild);
  31. g_SwekeyPlugin = document.getElementById("swekey_activex");
  32. }
  33. return g_SwekeyPlugin;
  34. }
  35. g_SwekeyPlugin = document.getElementById("swekey_plugin");
  36. if (g_SwekeyPlugin != null)
  37. return g_SwekeyPlugin;
  38. for (i = 0; i < navigator.plugins.length; i ++)
  39. {
  40. try
  41. {
  42. if (navigator.plugins[i] == null)
  43. {
  44. navigator.plugins.refresh();
  45. }
  46. else if (navigator.plugins[i][0] != null && navigator.plugins[i][0].type == "application/fbauth-plugin")
  47. {
  48. var x = document.createElement('embed');
  49. x.setAttribute('type', 'application/fbauth-plugin');
  50. x.setAttribute('id', 'swekey_plugin');
  51. x.setAttribute('width', '0');
  52. x.setAttribute('height', '0');
  53. x.style.dislay='none';
  54. //document.body.appendChild(x);
  55. document.body.insertBefore(x, document.body.firstChild);
  56. g_SwekeyPlugin = document.getElementById("swekey_plugin");
  57. return g_SwekeyPlugin;
  58. }
  59. }
  60. catch (e)
  61. {
  62. navigator.plugins.refresh();
  63. //alert ('Failed to create plugin: ' + e);
  64. }
  65. }
  66. }
  67. catch (e)
  68. {
  69. //alert("Swekey_Plugin " + e);
  70. g_SwekeyPlugin = null;
  71. }
  72. return null;
  73. }
  74. // -------------------------------------------------------------------
  75. // Returns true if the swekey plugin is installed
  76. function Swekey_Installed()
  77. {
  78. return (Swekey_Plugin() != null);
  79. }
  80. // -------------------------------------------------------------------
  81. // List the id of the Swekey connected to the PC
  82. // Returns a string containing comma separated Swekey Ids
  83. // A Swekey is a 32 char hexadecimal value.
  84. function Swekey_ListKeyIds()
  85. {
  86. try
  87. {
  88. return Swekey_Plugin().list();
  89. }
  90. catch (e)
  91. {
  92. // alert("Swekey_ListKeyIds " + e);
  93. }
  94. return "";
  95. }
  96. // -------------------------------------------------------------------
  97. // Ask the Connected Swekey to generate an OTP
  98. // id: The id of the connected Swekey (returne by Swekey_ListKeyIds())
  99. // rt: A random token
  100. // return: The calculated OTP encoded in a 64 chars hexadecimal value.
  101. function Swekey_GetOtp(id, rt)
  102. {
  103. try
  104. {
  105. return Swekey_Plugin().getotp(id, rt);
  106. }
  107. catch (e)
  108. {
  109. // alert("Swekey_GetOtp " + e);
  110. }
  111. return "";
  112. }
  113. // -------------------------------------------------------------------
  114. // Ask the Connected Swekey to generate a OTP linked to the current https host
  115. // id: The id of the connected Swekey (returne by Swekey_ListKeyIds())
  116. // rt: A random token
  117. // return: The calculated OTP encoded in a 64 chars hexadecimal value.
  118. // or "" if the current url does not start with https
  119. function Swekey_GetLinkedOtp(id, rt)
  120. {
  121. try
  122. {
  123. return Swekey_Plugin().getlinkedotp(id, rt);
  124. }
  125. catch (e)
  126. {
  127. // alert("Swekey_GetSOtp " + e);
  128. }
  129. return "";
  130. }
  131. // -------------------------------------------------------------------
  132. // Calls Swekey_GetOtp or Swekey_GetLinkedOtp depending if we are in
  133. // an https page or not.
  134. // id: The id of the connected Swekey (returne by Swekey_ListKeyIds())
  135. // rt: A random token
  136. // return: The calculated OTP encoded in a 64 chars hexadecimal value.
  137. function Swekey_GetSmartOtp(id, rt)
  138. {
  139. var res = Swekey_GetLinkedOtp(id, rt);
  140. if (res == "")
  141. res = Swekey_GetOtp(id, rt);
  142. return res;
  143. }
  144. // -------------------------------------------------------------------
  145. // Set a unplug handler (url) to the specified connected feebee
  146. // id: The id of the connected Swekey (returne by Swekey_ListKeyIds())
  147. // key: The key that index that url, (aplhanumeric values only)
  148. // url: The url that will be launched ("" deletes the url)
  149. function Swekey_SetUnplugUrl(id, key, url)
  150. {
  151. try
  152. {
  153. return Swekey_Plugin().setunplugurl(id, key, url);
  154. }
  155. catch (e)
  156. {
  157. // alert("Swekey_SetUnplugUrl " + e);
  158. }
  159. }
  160. </script>