summaryrefslogtreecommitdiff
path: root/underlays/openid-selector/ikiwiki/openid/openid-jquery.js
blob: 4eaa613d978e39dda431c20088629c5be3dc9420 (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. }
  106. $('#openid_form').submit(this.submit);
  107. var box_id = this.readCookie();
  108. if (box_id) {
  109. this.signin(box_id, true);
  110. }
  111. },
  112. getBoxHTML: function(provider, box_size) {
  113. var label="";
  114. var title=""
  115. if (box_size == 'large') {
  116. label=' ' + provider["name"];
  117. }
  118. else {
  119. title=' title="'+provider["name"]+'"';
  120. }
  121. var box_id = provider["name"].toLowerCase();
  122. return '<a' + title +' href="javascript: openid.signin(\''+ box_id +'\');"' +
  123. ' style="background: #FFF" ' +
  124. 'class="' + box_id + ' openid_' + box_size + '_btn">' +
  125. '<img alt="" width="16" height="16" src="' + provider["icon"] + '" />' +
  126. label +
  127. '</a>';
  128. },
  129. /* Provider image click */
  130. signin: function(box_id, onload) {
  131. if (box_id == 'localsignin') {
  132. this.highlight(box_id);
  133. $('#openid_input_area').empty();
  134. $('#'+this.localsignin_id).show();
  135. this.setCookie(box_id);
  136. return;
  137. }
  138. else {
  139. if (this.localsignin_id) {
  140. $('#'+this.localsignin_id).hide();
  141. }
  142. }
  143. var provider = providers[box_id];
  144. if (! provider) {
  145. return;
  146. }
  147. this.highlight(box_id);
  148. this.provider_id = box_id;
  149. this.provider_url = provider['url'];
  150. // prompt user for input?
  151. if (provider['label']) {
  152. this.setCookie(box_id);
  153. this.useInputBox(provider);
  154. } else {
  155. this.setCookie('');
  156. $('#openid_input_area').empty();
  157. if (! onload) {
  158. $('#openid_form').submit();
  159. }
  160. }
  161. },
  162. /* Sign-in button click */
  163. submit: function() {
  164. var url = openid.provider_url;
  165. if (url) {
  166. url = url.replace('{username}', $('#openid_username').val());
  167. openid.setOpenIdUrl(url);
  168. }
  169. if(openid.ajaxHandler) {
  170. openid.ajaxHandler(openid.provider_id, document.getElementById(openid.input_id).value);
  171. return false;
  172. }
  173. if(openid.demo) {
  174. alert("In client demo mode. Normally would have submitted OpenID:\r\n" + document.getElementById(openid.input_id).value);
  175. return false;
  176. }
  177. return true;
  178. },
  179. setOpenIdUrl: function (url) {
  180. var hidden = $('#'+this.input_id);
  181. if (hidden.length > 0) {
  182. hidden.value = url;
  183. } else {
  184. $('#openid_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
  185. }
  186. },
  187. highlight: function (box_id) {
  188. // remove previous highlight.
  189. var highlight = $('#openid_highlight');
  190. if (highlight) {
  191. highlight.replaceWith($('#openid_highlight a')[0]);
  192. }
  193. // add new highlight.
  194. $('.'+box_id).wrap('<div id="openid_highlight"></div>');
  195. },
  196. setCookie: function (value) {
  197. var date = new Date();
  198. date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
  199. var expires = "; expires="+date.toGMTString();
  200. document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
  201. },
  202. readCookie: function () {
  203. var nameEQ = this.cookie_name + "=";
  204. var ca = document.cookie.split(';');
  205. for(var i=0;i < ca.length;i++) {
  206. var c = ca[i];
  207. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  208. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  209. }
  210. return null;
  211. },
  212. useInputBox: function (provider) {
  213. var input_area = $('#openid_input_area');
  214. var html = '';
  215. var id = 'openid_username';
  216. var value = '';
  217. var label = provider['label'];
  218. var style = '';
  219. if (provider['name'] == 'OpenID') {
  220. id = this.input_id;
  221. value = '';
  222. style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
  223. }
  224. if (label) {
  225. html = '<label for="'+ id +'" class="block">' + label + '</label>';
  226. }
  227. html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' +
  228. '<input id="openid_submit" type="submit" value="Login"/>';
  229. input_area.empty();
  230. input_area.append(html);
  231. $('#'+id).focus();
  232. },
  233. setDemoMode: function (demoMode) {
  234. this.demo = demoMode;
  235. },
  236. setAjaxHandler: function (ajaxFunction) {
  237. this.ajaxHandler = ajaxFunction;
  238. }
  239. };