cas_client_cordova.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. Meteor.loginWithCas = function(callback) {
  2. var credentialToken = Random.id();
  3. if (!Meteor.settings.public &&
  4. !Meteor.settings.public.cas &&
  5. !Meteor.settings.public.cas.loginUrl) {
  6. return;
  7. }
  8. var settings = Meteor.settings.public.cas;
  9. var loginUrl = settings.loginUrl +
  10. "?" + (settings.service || "service") + "=" +
  11. Meteor.absoluteUrl('_cas/') +
  12. credentialToken;
  13. var fail = function (err) {
  14. Meteor._debug("Error from OAuth popup: " + JSON.stringify(err));
  15. };
  16. // When running on an android device, we sometimes see the
  17. // `pageLoaded` callback fire twice for the final page in the OAuth
  18. // popup, even though the page only loads once. This is maybe an
  19. // Android bug or maybe something intentional about how onPageFinished
  20. // works that we don't understand and isn't well-documented.
  21. var oauthFinished = false;
  22. var pageLoaded = function (event) {
  23. if (oauthFinished) {
  24. return;
  25. }
  26. if (event.url.indexOf(Meteor.absoluteUrl('_cas')) === 0) {
  27. oauthFinished = true;
  28. // On iOS, this seems to prevent "Warning: Attempt to dismiss from
  29. // view controller <MainViewController: ...> while a presentation
  30. // or dismiss is in progress". My guess is that the last
  31. // navigation of the OAuth popup is still in progress while we try
  32. // to close the popup. See
  33. // https://issues.apache.org/jira/browse/CB-2285.
  34. //
  35. // XXX Can we make this timeout smaller?
  36. setTimeout(function () {
  37. popup.close();
  38. // check auth on server.
  39. Accounts.callLoginMethod({
  40. methodArguments: [{ cas: { credentialToken: credentialToken } }],
  41. userCallback: callback
  42. });
  43. }, 100);
  44. }
  45. };
  46. var onExit = function () {
  47. popup.removeEventListener('loadstop', pageLoaded);
  48. popup.removeEventListener('loaderror', fail);
  49. popup.removeEventListener('exit', onExit);
  50. };
  51. var popup = window.open(loginUrl, '_blank', 'location=no,hidden=no');
  52. popup.addEventListener('loadstop', pageLoaded);
  53. popup.addEventListener('loaderror', fail);
  54. popup.addEventListener('exit', onExit);
  55. popup.show();
  56. };