Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/users-api-connector / helpers / connectCalendarHelper.js
Size: Mime:
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Type = exports.checkIfWindowClosed = exports.generateUrlData = exports.checkIfConnectedSuccessfully = exports.open = exports.generateUrl = undefined;

var _OAuthAntiCSRFTokenHelper = require('./OAuthAntiCSRFTokenHelper');

var _storage = require('../state/storage');

var _storage2 = _interopRequireDefault(_storage);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Type = {
  CALENDAR: 'calendar',
  ADDRESS_BOOK: 'contacts'
};
var generateRandomStr = function generateRandomStr() {
  var length = 32;
  var text = '';
  var possibilities = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  for (var i = 0; i < length; i++) {
    text += possibilities.charAt(Math.floor(Math.random() * possibilities.length));
  }
  return text;
};

var generateUrl = function generateUrl(type, provider, token, redirectUri, clientId, branch) {
  var clientBaseUrl = provider === 'google' ? 'https://accounts.google.com/o/oauth2/auth' : 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
  var scope = type === 'calendar' ? 'CALENDARS' : 'CONTACTS';
  if (provider === 'google') {
    var scopeUrl = type === 'calendar' ? 'https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly' : 'https://www.googleapis.com/auth/contacts.readonly';
    var state = encodeURIComponent(JSON.stringify({
      doodleScope: [scope],
      callbackUrl: redirectUri,
      branch: branch,
      oauth_anti_csrf_token_cookie: token
    }));
    return clientBaseUrl + '?access_type=offline&approval_prompt=force&client_id=' + clientId + '.apps.googleusercontent.com&redirect_uri=' + redirectUri + '&response_type=code&scope=' + scopeUrl + '&state=' + state;
  }
  if (provider === 'outlookcom') {
    var nonce = generateRandomStr();
    var _state = encodeURIComponent(JSON.stringify({ type: scope, branch: branch, oauth_anti_csrf_token_cookie: token }));
    return clientBaseUrl + '?client_id=' + clientId + '&response_type=id_token+code&response_mode=form_post&scope=openid+offline_access+User.Read+Calendars.ReadWrite+Contacts.Read&nonce=' + nonce + '&redirect_uri=' + encodeURIComponent(redirectUri) + '&state=' + _state;
  }
};

var open = function open(provider, type, url) {
  var width = 700;
  var height = 400;
  var left = screen.width / 2 - width / 2;
  var top = screen.height / 2 - height / 2;
  var windowName = 'connect-' + provider + '-' + type;
  return window.open(url, windowName, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
};

var checkIfWindowClosed = function checkIfWindowClosed(win) {
  return new Promise(function (resolve, reject) {
    var closingTimerId = setTimeout(reject, 180000);
    var timer = setInterval(function () {
      if (win.closed) {
        window.clearInterval(timer);
        window.clearInterval(closingTimerId);
        resolve();
      }
    }, 100);
  });
};

var checkIfConnectedSuccessfully = function checkIfConnectedSuccessfully(provider, oldCalendarsList, newCalendarsList) {
  var addedCalendar = newCalendarsList.filter(function (calendar) {
    return !oldCalendarsList.some(function (cal) {
      return calendar.id === cal.id;
    });
  });
  return addedCalendar.length > 0;
};

var generateUrlData = function generateUrlData(provider, options) {
  var CSRFTokenFromCookie = _storage2.default.getCookie('oauth_anti_csrf_token_cookie');
  var data = {
    randomCSRFToken: CSRFTokenFromCookie || (0, _OAuthAntiCSRFTokenHelper.generateRandomOAuthAntiCSRFToken)(),
    clientId: provider === 'google' ? options.googleClientId : options.outlookClientId,
    redirectUri: provider === 'google' ? options.googleCalendarRedirectUri : options.outlookRedirectUri,
    branch: options.oAuthProxyTarget
  };
  if (!CSRFTokenFromCookie) {
    _storage2.default.setOAuthAntiCSRFTokenCookie(data.randomCSRFToken, options.cookieDomain);
  }
  return data;
};

exports.generateUrl = generateUrl;
exports.open = open;
exports.checkIfConnectedSuccessfully = checkIfConnectedSuccessfully;
exports.generateUrlData = generateUrlData;
exports.checkIfWindowClosed = checkIfWindowClosed;
exports.Type = Type;