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