authentication.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. function authentication_login_init() {
  2. var str = '<table border="1" width="100%">';
  3. str += '<tr><td colspan="2"><a href="#" id="checkCredential">checkCredential</a>&nbsp;<a href="#" id="login">login</a>&nbsp;<a href="#" id="createCode">createCode</a>&nbsp;<a href="#" id="codeLogin">codeLogin</a>&nbsp;<a href="#" id="createCaptcha">createCaptcha</a>&nbsp;<a href="#" id="captchaLogin">captchaLogin</a>&nbsp;<a href="#" id="createBind">createBind</a>&nbsp;<a href="#" id="bindLogin">bindLogin</a></td></tr>';
  4. str += '<tr><td>credential:</td><td><input type="text" id="credential" style="width:95%"/></td></tr>';
  5. str += '<tr><td>password:</td><td><input type="password" id="password" style="width:95%"/></td></tr>';
  6. str += '<tr><td>width:</td><td><input type="text" value="120" id="width" style="width:95%"/></td></tr>';
  7. str += '<tr><td>height:</td><td><input type="text" value="50" id="height" style="width:95%"/></td></tr>';
  8. str += '<tr><td>codeAnswer:</td><td><input type="text" id="codeAnswer" style="width:95%"/></td></tr>';
  9. str += '<tr><td>captcha:</td><td><input type="text" id="captcha" style="width:95%"/></td></tr>';
  10. str += '<tr><td>captchaAnswer:</td><td><input type="text" id="captchaAnswer" style="width:95%"/></td></tr>';
  11. str += '<tr><td colspan="2" id="captchaImage">&nbsp;</td></tr>';
  12. str += '<tr><td>meta:</td><td><input type="text" id="meta" style="width:95%"/></td></tr>';
  13. str += '<tr><td colspan="2" id="bindImage">&nbsp;</td></tr>';
  14. str += '</table>';
  15. $('#content').html(str);
  16. $('#result').html('');
  17. $('#checkCredential').click(function() {
  18. authentication_check_credential($('#credential').val());
  19. });
  20. $('#login').click(function() {
  21. authentication_login($('#credential').val(), $('#password').val());
  22. });
  23. $('#createCode').click(function() {
  24. authentication_create_code($('#credential').val());
  25. });
  26. $('#codeLogin').click(function() {
  27. authentication_login_code($('#credential').val(), $('#codeAnswer').val());
  28. });
  29. $('#createCaptcha').click(function() {
  30. authentication_create_captcha($('#width').val(), $('#height').val());
  31. });
  32. $('#captchaLogin').click(function() {
  33. authentication_login_captcha($('#credential').val(), $('#password').val(), $('#captcha').val(), $('#captchaAnswer').val());
  34. });
  35. $('#createBind').click(function() {
  36. authentication_create_bind();
  37. });
  38. $('#bindLogin').click(function() {
  39. authentication_login_bind($('#meta').val());
  40. });
  41. }
  42. function authentication_check_credential(credential) {
  43. $.ajax({
  44. type : 'get',
  45. dataType : 'json',
  46. url : '../jaxrs/authentication/check/credential/' + credential,
  47. contentType : 'application/json; charset=utf-8',
  48. xhrFields : {
  49. 'withCredentials' : true
  50. },
  51. crossDomain : true
  52. }).always(function(json) {
  53. $('#result').html(JSON.stringify(json, null, 4));
  54. });
  55. }
  56. function authentication_login(credential, password) {
  57. $.ajax({
  58. type : 'post',
  59. dataType : 'json',
  60. url : '../jaxrs/authentication',
  61. contentType : 'application/json; charset=utf-8',
  62. data : JSON.stringify({
  63. credential : credential,
  64. password : password
  65. }),
  66. xhrFields : {
  67. 'withCredentials' : true
  68. },
  69. crossDomain : true
  70. }).always(function(json) {
  71. $('#result').html(JSON.stringify(json, null, 4));
  72. });
  73. }
  74. function authentication_create_code(credential) {
  75. $.ajax({
  76. type : 'get',
  77. dataType : 'json',
  78. url : '../jaxrs/authentication/code/credential/' + credential,
  79. contentType : 'application/json; charset=utf-8',
  80. xhrFields : {
  81. 'withCredentials' : true
  82. },
  83. crossDomain : true
  84. }).always(function(json) {
  85. $('#result').html(JSON.stringify(json, null, 4));
  86. });
  87. }
  88. function authentication_login_code(credential, codeAnswer) {
  89. $.ajax({
  90. type : 'post',
  91. dataType : 'json',
  92. url : '../jaxrs/authentication/code',
  93. contentType : 'application/json; charset=utf-8',
  94. data : JSON.stringify({
  95. credential : credential,
  96. codeAnswer : codeAnswer
  97. }),
  98. xhrFields : {
  99. 'withCredentials' : true
  100. },
  101. crossDomain : true
  102. }).always(function(json) {
  103. $('#result').html(JSON.stringify(json, null, 4));
  104. });
  105. }
  106. function authentication_create_captcha(width, height) {
  107. $.ajax({
  108. type : 'get',
  109. dataType : 'json',
  110. url : '../jaxrs/authentication/captcha/width/' + width + '/height/' + height,
  111. contentType : 'application/json; charset=utf-8',
  112. xhrFields : {
  113. 'withCredentials' : true
  114. },
  115. crossDomain : true
  116. }).done(function(json) {
  117. $('#captchaImage').html('<img src="data:image/png;base64,' + json.data.image + '"/>');
  118. $('#captcha').val(json.data.id);
  119. }).always(function(json) {
  120. $('#result').html(JSON.stringify(json, null, 4));
  121. });
  122. }
  123. function authentication_login_captcha(credential, password, captcha, captchaAnswer) {
  124. $.ajax({
  125. type : 'post',
  126. dataType : 'json',
  127. url : '../jaxrs/authentication/captcha',
  128. contentType : 'application/json; charset=utf-8',
  129. data : JSON.stringify({
  130. credential : credential,
  131. password : password,
  132. captcha : captcha,
  133. captchaAnswer : captchaAnswer
  134. }),
  135. xhrFields : {
  136. 'withCredentials' : true
  137. },
  138. crossDomain : true
  139. }).always(function(json) {
  140. $('#result').html(JSON.stringify(json, null, 4));
  141. });
  142. }
  143. function authentication_create_bind() {
  144. $.ajax({
  145. type : 'get',
  146. dataType : 'json',
  147. url : '../jaxrs/authentication/bind',
  148. contentType : 'application/json; charset=utf-8',
  149. xhrFields : {
  150. 'withCredentials' : true
  151. },
  152. crossDomain : true
  153. }).done(function(json) {
  154. $('#bindImage').html('<img src="data:image/png;base64,' + json.data.image + '"/>');
  155. $('#meta').val(json.data.meta);
  156. }).always(function(json) {
  157. $('#result').html(JSON.stringify(json, null, 4));
  158. });
  159. }
  160. function authentication_login_bind(meta) {
  161. $.ajax({
  162. type : 'get',
  163. dataType : 'json',
  164. url : '../jaxrs/authentication/bind/meta/' + meta,
  165. contentType : 'application/json; charset=utf-8',
  166. xhrFields : {
  167. 'withCredentials' : true
  168. },
  169. crossDomain : true
  170. }).always(function(json) {
  171. $('#result').html(JSON.stringify(json, null, 4));
  172. });
  173. }
  174. function authentication_logout() {
  175. $('#content').html('');
  176. $('#result').html('');
  177. $.ajax({
  178. type : 'delete',
  179. dataType : 'json',
  180. url : '../jaxrs/authentication',
  181. xhrFields : {
  182. 'withCredentials' : true
  183. },
  184. crossDomain : true
  185. }).always(function(json) {
  186. $('#result').html(JSON.stringify(json, null, 4));
  187. });
  188. }
  189. function authentication_who() {
  190. $('#result').html('');
  191. $('#content').html('');
  192. $.ajax({
  193. type : 'get',
  194. dataType : 'json',
  195. url : '../jaxrs/authentication',
  196. xhrFields : {
  197. 'withCredentials' : true
  198. },
  199. crossDomain : true
  200. }).always(function(json) {
  201. $('#result').html(JSON.stringify(json, null, 4));
  202. });
  203. }