Repository URL to install this package:
|
Version:
1.7.2-rc.1 ▾
|
import { generateRandomOAuthAntiCSRFToken } from './OAuthAntiCSRFTokenHelper';
import storage from '../state/storage';
const Type = {
CALENDAR: 'calendar',
ADDRESS_BOOK: 'contacts',
};
const generateRandomStr = () => {
const length = 32;
let text = '';
const possibilities = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
text += possibilities.charAt(Math.floor(Math.random() * possibilities.length));
}
return text;
};
const generateUrl = (type, provider, token, redirectUri, clientId, branch) => {
const clientBaseUrl =
provider === 'google'
? 'https://accounts.google.com/o/oauth2/auth'
: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
const scope = type === 'calendar' ? 'CALENDARS' : 'CONTACTS';
if (provider === 'google') {
const scopeUrl =
type === 'calendar' ? 'https://www.googleapis.com/auth/calendar' : 'https://www.google.com/m8/feeds/';
const state = encodeURIComponent(
JSON.stringify({
doodleScope: [scope],
callbackUrl: redirectUri,
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') {
const nonce = generateRandomStr();
const state = encodeURIComponent(JSON.stringify({ type: scope, 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}`;
}
};
const open = (provider, type, url) => {
const width = 700;
const height = 400;
const left = screen.width / 2 - width / 2;
const top = screen.height / 2 - height / 2;
const 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}`
);
};
const checkIfWindowClosed = win =>
new Promise((resolve, reject) => {
const closingTimerId = setTimeout(reject, 180000);
const timer = setInterval(() => {
if (win.closed) {
window.clearInterval(timer);
window.clearInterval(closingTimerId);
resolve();
}
}, 100);
});
const checkIfConnectedSuccessfully = (provider, oldCalendarsList, newCalendarsList) => {
const addedCalendar = newCalendarsList.filter(calendar => !oldCalendarsList.some(cal => calendar.id === cal.id));
return addedCalendar.length > 0;
};
const generateUrlData = (provider, options) => {
const CSRFTokenFromCookie = storage.getCookie('oauth_anti_csrf_token_cookie');
const data = {
randomCSRFToken: CSRFTokenFromCookie || generateRandomOAuthAntiCSRFToken(),
clientId: provider === 'google' ? options.googleClientId : options.outlookClientId,
redirectUri: provider === 'google' ? options.googleCalendarRedirectUri : options.outlookRedirectUri,
branch: options.oAuthProxyTarget,
};
if (!CSRFTokenFromCookie) {
storage.setOAuthAntiCSRFTokenCookie(data.randomCSRFToken, options.cookieDomain);
}
return data;
};
export { generateUrl, open, checkIfConnectedSuccessfully, generateUrlData, checkIfWindowClosed, Type };