1 import type { ApiEnvironmentConfig } from '@proton/shared/lib/interfaces';
3 import { ImportProvider, OAUTH_PROVIDER } from '../interface';
4 import { G_OAUTH_REDIRECT_PATH, O_OAUTH_REDIRECT_PATH, Z_OAUTH_REDIRECT_PATH } from '../path';
6 const generateGoogleOAuthUrl = (params: URLSearchParams, config: ApiEnvironmentConfig, loginHint?: string) => {
7 params.append('access_type', 'offline');
8 params.append('client_id', config['oauth.google.client_id']);
10 params.append('login_hint', loginHint);
12 return `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
15 const generateOutlookOAuthUrl = (params: URLSearchParams, config: ApiEnvironmentConfig) => {
16 params.append('client_id', config['oauth.outlook.client_id']);
17 return `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?${params.toString()}`;
20 const generateZoomOAuthUrl = (params: URLSearchParams, config: ApiEnvironmentConfig) => {
21 params.append('client_id', config['oauth.zoom.client_id']);
22 return `https://zoom.us/oauth/authorize?${params.toString()}`;
25 export const getOAuthRedirectURL = (provider: ImportProvider | OAUTH_PROVIDER) => {
26 const { protocol, host } = window.location;
28 if (provider === ImportProvider.GOOGLE || provider === OAUTH_PROVIDER.GOOGLE) {
29 return `${protocol}//${host}${G_OAUTH_REDIRECT_PATH}`;
32 if (provider === ImportProvider.OUTLOOK || provider === OAUTH_PROVIDER.OUTLOOK) {
33 return `${protocol}//${host}${O_OAUTH_REDIRECT_PATH}`;
36 if (provider === OAUTH_PROVIDER.ZOOM) {
37 return `${protocol}//${host}${Z_OAUTH_REDIRECT_PATH}`;
40 throw new Error('Provider does not exist');
43 export const getOAuthAuthorizationUrl = ({
50 provider: ImportProvider | OAUTH_PROVIDER;
52 config: ApiEnvironmentConfig;
54 consentExperiment: boolean;
56 const params = new URLSearchParams();
58 params.append('redirect_uri', getOAuthRedirectURL(provider));
59 params.append('response_type', 'code');
60 params.append('scope', scope);
62 if (provider === ImportProvider.GOOGLE || provider === OAUTH_PROVIDER.GOOGLE) {
63 // force user to consent again so that we can always get a refresh token
64 params.append('prompt', 'consent');
65 return generateGoogleOAuthUrl(params, config, loginHint);
68 if (provider === ImportProvider.OUTLOOK || provider === OAUTH_PROVIDER.OUTLOOK) {
69 // The flag is present to control if we add the prompt params
70 // The flag mustbe off to add the consent params
71 if (!consentExperiment) {
72 params.append('prompt', 'consent');
75 return generateOutlookOAuthUrl(params, config);
78 if (provider === OAUTH_PROVIDER.ZOOM) {
79 return generateZoomOAuthUrl(params, config);
82 throw new Error('Provider does not exist');
85 export const getProviderNumber = (provider: ImportProvider | OAUTH_PROVIDER) => {
87 case ImportProvider.GOOGLE:
88 case OAUTH_PROVIDER.GOOGLE:
89 return OAUTH_PROVIDER.GOOGLE;
90 case ImportProvider.OUTLOOK:
91 case OAUTH_PROVIDER.OUTLOOK:
92 return OAUTH_PROVIDER.OUTLOOK;
93 case OAUTH_PROVIDER.ZOOM:
94 return OAUTH_PROVIDER.ZOOM;
96 throw new Error('Provider does not exist');