summaryrefslogtreecommitdiff
path: root/ikiwiki/openid/openid-jquery.js
blob: f22f5776a17713b7a5d05b93a2040b6eb393ef49 (plain)
  1. /*
  2. Simple OpenID Plugin
  3. http://code.google.com/p/openid-selector/
  4. This code is licenced under the New BSD License.
  5. */
  6. var providers_large = {
  7. google: {
  8. name: 'Google',
  9. icon: 'http://google.com/favicon.ico',
  10. url: 'https://www.google.com/accounts/o8/id'
  11. },
  12. yahoo: {
  13. name: 'Yahoo',
  14. icon: 'http://yahoo.com/favicon.ico',
  15. url: 'http://me.yahoo.com/'
  16. },
  17. openid: {
  18. name: 'OpenID',
  19. icon: 'wikiicons/openidlogin-bg.gif',
  20. label: 'Enter your OpenID:',
  21. url: null
  22. },
  23. };
  24. var providers_small = {
  25. livejournal: {
  26. name: 'LiveJournal',
  27. icon: 'http://livejournal.com/favicon.ico',
  28. label: 'Enter your Livejournal username:',
  29. url: 'http://{username}.livejournal.com/'
  30. },
  31. flickr: {
  32. name: 'Flickr',
  33. icon: 'http://flickr.com/favicon.ico',
  34. label: 'Enter your Flickr username:',
  35. url: 'http://flickr.com/photos/{username}/'
  36. },
  37. wordpress: {
  38. name: 'Wordpress',
  39. icon: 'https://ddgw.s3.amazonaws.com/wordpress.org.ico',
  40. label: 'Enter your Wordpress.com username:',
  41. url: 'http://{username}.wordpress.com/'
  42. },
  43. myopenid: {
  44. name: 'MyOpenID',
  45. icon: 'http://myopenid.com/favicon.ico',
  46. label: 'Enter your MyOpenID username:',
  47. url: 'http://{username}.myopenid.com/'
  48. },
  49. claimid: {
  50. name: 'ClaimID',
  51. icon: 'http://claimid.com/favicon.ico',
  52. label: 'Enter your ClaimID username:',
  53. url: 'http://claimid.com/{username}'
  54. },
  55. aol: {
  56. name: 'AOL',
  57. icon: 'http://aol.com/favicon.ico',
  58. label: 'Enter your AOL username:',
  59. url: 'http://openid.aol.com/{username}'
  60. },
  61. verisign: {
  62. name: 'Verisign',
  63. icon: 'http://verisign.com/favicon.ico',
  64. label: 'Enter your Verisign username:',
  65. url: 'http://{username}.pip.verisignlabs.com/'
  66. },
  67. };
  68. var providers = $.extend({}, providers_large, providers_small);
  69. var openid = {
  70. demo: false,
  71. ajaxHandler: null,
  72. cookie_expires: 6*30, // 6 months.
  73. cookie_name: 'openid_provider',
  74. cookie_path: '/',
  75. img_path: 'images/',
  76. input_id: null,
  77. provider_url: null,
  78. provider_id: null,
  79. localsignin_id: null,
  80. init: function(input_id, localsignin_id) {
  81. var openid_btns = $('#openid_btns');
  82. this.input_id = input_id;
  83. $('#openid_choice').show();
  84. $('#openid_input_area').empty();
  85. // add box for each provider
  86. for (id in providers_large) {
  87. openid_btns.append(this.getBoxHTML(providers_large[id], 'large'));
  88. }
  89. if (providers_small) {
  90. openid_btns.append('<br/>');
  91. for (id in providers_small) {
  92. openid_btns.append(this.getBoxHTML(providers_small[id], 'small'));
  93. }
  94. }
  95. if (localsignin_id != "") {
  96. this.localsignin_id=localsignin_id;
  97. openid_btns.append(
  98. '<a href="javascript: openid.signin(\'localsignin\');"' +
  99. ' style="background: #FFF" ' +
  100. 'class="localsignin openid_small_btn">' +
  101. '<img alt="" width="16" height="16" src="favicon.ico" />' +
  102. ' other' +
  103. '</a>'
  104. );
  105. $('#'+this.localsignin_id).hide();
  106. }
  107. $('#openid_form').submit(this.submit);
  108. var box_id = this.readCookie();
  109. if (box_id) {
  110. this.signin(box_id, true);
  111. }
  112. },
  113. getBoxHTML: function(provider, box_size) {
  114. var label="";
  115. var title=""
  116. if (box_size == 'large') {
  117. label=' ' + provider["name"];
  118. }
  119. else {
  120. title=' title="'+provider["name"]+'"';
  121. }
  122. var box_id = provider["name"].toLowerCase();
  123. return '<a' + title +' href="javascript: openid.signin(\''+ box_id +'\');"' +
  124. ' style="background: #FFF" ' +
  125. 'class="' + box_id + ' openid_' + box_size + '_btn">' +
  126. '<img alt="" width="16" height="16" src="' + provider["icon"] + '" />' +
  127. label +
  128. '</a>';
  129. },
  130. /* Provider image click */
  131. signin: function(box_id, onload) {
  132. if (box_id == 'localsignin') {
  133. this.highlight(box_id);
  134. $('#openid_input_area').empty();
  135. $('#'+this.localsignin_id).show();
  136. this.setCookie(box_id);
  137. return;
  138. }
  139. else {
  140. if (this.localsignin_id) {
  141. $('#'+this.localsignin_id).hide();
  142. }
  143. }
  144. var provider = providers[box_id];
  145. if (! provider) {
  146. return;
  147. }
  148. this.highlight(box_id);
  149. this.provider_id = box_id;
  150. this.provider_url = provider['url'];
  151. // prompt user for input?
  152. if (provider['label']) {
  153. this.setCookie(box_id);
  154. this.useInputBox(provider);
  155. } else {
  156. this.setCookie('');
  157. $('#openid_input_area').empty();
  158. if (! onload) {
  159. $('#openid_form').submit();
  160. }
  161. }
  162. },
  163. /* Sign-in button click */
  164. submit: function() {
  165. var url = openid.provider_url;
  166. if (url) {
  167. url = url.replace('{username}', $('#openid_username').val());
  168. openid.setOpenIdUrl(url);
  169. }
  170. if(openid.ajaxHandler) {
  171. openid.ajaxHandler(openid.provider_id, document.getElementById(openid.input_id).value);
  172. return false;
  173. }
  174. if(openid.demo) {
  175. alert("In client demo mode. Normally would have submitted OpenID:\r\n" + document.getElementById(openid.input_id).value);
  176. return false;
  177. }
  178. return true;
  179. },
  180. setOpenIdUrl: function (url) {
  181. var hidden = $('#'+this.input_id);
  182. if (hidden.length > 0) {
  183. hidden.value = url;
  184. } else {
  185. $('#openid_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
  186. }
  187. },
  188. highlight: function (box_id) {
  189. // remove previous highlight.
  190. var highlight = $('#openid_highlight');
  191. if (highlight) {
  192. highlight.replaceWith($('#openid_highlight a')[0]);
  193. }
  194. // add new highlight.
  195. $('.'+box_id).wrap('<div id="openid_highlight"></div>');
  196. },
  197. setCookie: function (value) {
  198. var date = new Date();
  199. date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
  200. var expires = "; expires="+date.toGMTString();
  201. document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
  202. },
  203. readCookie: function () {
  204. var nameEQ = this.cookie_name + "=";
  205. var ca = document.cookie.split(';');
  206. for(var i=0;i < ca.length;i++) {
  207. var c = ca[i];
  208. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  209. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  210. }
  211. return null;
  212. },
  213. useInputBox: function (provider) {
  214. var input_area = $('#openid_input_area');
  215. var html = '';
  216. var id = 'openid_username';
  217. var value = '';
  218. var label = provider['label'];
  219. var style = '';
  220. if (provider['name'] == 'OpenID') {
  221. id = this.input_id;
  222. value = '';
  223. style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
  224. }
  225. if (label) {
  226. html = '<label for="'+ id +'" class="block">' + label + '</label>';
  227. }
  228. html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' +
  229. '<input id="openid_submit" type="submit" value="Login"/>';
  230. input_area.empty();
  231. input_area.append(html);
  232. $('#'+id).focus();
  233. },
  234. setDemoMode: function (demoMode) {
  235. this.demo = demoMode;
  236. },
  237. setAjaxHandler: function (ajaxFunction) {
  238. this.ajaxHandler = ajaxFunction;
  239. }
  240. };