cas_client.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function addParameterToURL(url, param){
  2. var urlSplit = url.split('?');
  3. return url+(urlSplit.length>0 ? '?':'&') + param;
  4. }
  5. Meteor.initCas = function(callback) {
  6. const casTokenMatch = window.location.href.match(/[?&]casToken=([^&]+)/);
  7. if (casTokenMatch == null) {
  8. return;
  9. }
  10. window.history.pushState('', document.title, window.location.href.replace(/([&?])casToken=[^&]+[&]?/, '$1').replace(/[?&]+$/g, ''));
  11. Accounts.callLoginMethod({
  12. methodArguments: [{ cas: { credentialToken: casTokenMatch[1] } }],
  13. userCallback: function(err){
  14. if (err == null) {
  15. // should we do anything on success?
  16. }
  17. if (callback != null) {
  18. callback(err);
  19. }
  20. }
  21. });
  22. }
  23. Meteor.loginWithCas = function(options, callback) {
  24. var credentialToken = Random.id();
  25. if (!Meteor.settings.public &&
  26. !Meteor.settings.public.cas &&
  27. !Meteor.settings.public.cas.loginUrl) {
  28. return;
  29. }
  30. var settings = Meteor.settings.public.cas;
  31. var backURL = window.location.href.replace('#', '');
  32. if (options != null && options.redirectUrl != null)
  33. backURL = options.redirectUrl;
  34. var serviceURL = addParameterToURL(backURL, 'casToken='+credentialToken);
  35. var loginUrl = settings.loginUrl +
  36. "?" + (settings.serviceParam || "service") + "=" +
  37. encodeURIComponent(serviceURL)
  38. if (settings.popup == false) {
  39. window.location = loginUrl;
  40. return;
  41. }
  42. var popup = openCenteredPopup(
  43. loginUrl,
  44. settings.width || 800,
  45. settings.height || 600
  46. );
  47. var checkPopupOpen = setInterval(function() {
  48. try {
  49. if(popup && popup.document && popup.document.getElementById('popupCanBeClosed')) {
  50. popup.close();
  51. }
  52. // Fix for #328 - added a second test criteria (popup.closed === undefined)
  53. // to humour this Android quirk:
  54. // http://code.google.com/p/android/issues/detail?id=21061
  55. var popupClosed = popup.closed || popup.closed === undefined;
  56. } catch (e) {
  57. // For some unknown reason, IE9 (and others?) sometimes (when
  58. // the popup closes too quickly?) throws "SCRIPT16386: No such
  59. // interface supported" when trying to read 'popup.closed'. Try
  60. // again in 100ms.
  61. return;
  62. }
  63. if (popupClosed) {
  64. clearInterval(checkPopupOpen);
  65. // check auth on server.
  66. Accounts.callLoginMethod({
  67. methodArguments: [{ cas: { credentialToken: credentialToken } }],
  68. userCallback: err => {
  69. // Fix redirect bug after login successfully
  70. if (!err) {
  71. window.location.href = '/';
  72. }
  73. }
  74. });
  75. }
  76. }, 100);
  77. };
  78. var openCenteredPopup = function(url, width, height) {
  79. var screenX = typeof window.screenX !== 'undefined'
  80. ? window.screenX : window.screenLeft;
  81. var screenY = typeof window.screenY !== 'undefined'
  82. ? window.screenY : window.screenTop;
  83. var outerWidth = typeof window.outerWidth !== 'undefined'
  84. ? window.outerWidth : document.body.clientWidth;
  85. var outerHeight = typeof window.outerHeight !== 'undefined'
  86. ? window.outerHeight : (document.body.clientHeight - 22);
  87. // XXX what is the 22?
  88. // Use `outerWidth - width` and `outerHeight - height` for help in
  89. // positioning the popup centered relative to the current window
  90. var left = screenX + (outerWidth - width) / 2;
  91. var top = screenY + (outerHeight - height) / 2;
  92. var features = ('width=' + width + ',height=' + height +
  93. ',left=' + left + ',top=' + top + ',scrollbars=yes');
  94. var newwindow = window.open(url, '_blank', features);
  95. if (newwindow.focus)
  96. newwindow.focus();
  97. return newwindow;
  98. };