feat: oauth for chrome extension (#4870)

Previously we had to create a separate API key to give access to chrome
extension so we can make calls to the DB. This PR includes logic to
initiate a oauth flow with PKCE method which redirects to the
`Authorise` screen to give access to server tokens.

Implemented in this PR- 
1. make `redirectUrl` a non-nullable parameter 
2. Add `NODE_ENV` to environment variable service
3. new env variable `CHROME_EXTENSION_REDIRECT_URL` on server side
4. strict checks for redirectUrl
5. try catch blocks on utils db query methods
6. refactor Apollo Client to handle `unauthorized` condition
7. input field to enter server url (for self-hosting)
8. state to show user if its already connected
9. show error if oauth flow is cancelled by user

Follow up PR -
Renew token logic

---------

Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
Aditya Pimpalkar
2024-04-24 10:45:16 +01:00
committed by GitHub
parent 0a7f82333b
commit c63ee519ea
33 changed files with 18564 additions and 15049 deletions

View File

@ -0,0 +1,26 @@
import {
ExchangeAuthCodeInput,
ExchangeAuthCodeResponse,
Tokens,
} from '~/db/types/auth.types';
import { EXCHANGE_AUTHORIZATION_CODE } from '~/graphql/auth/mutations';
import { isDefined } from '~/utils/isDefined';
import { callMutation } from '~/utils/requestDb';
export const exchangeAuthorizationCode = async (
exchangeAuthCodeInput: ExchangeAuthCodeInput,
): Promise<Tokens | null> => {
const data = await callMutation<ExchangeAuthCodeResponse>(
EXCHANGE_AUTHORIZATION_CODE,
exchangeAuthCodeInput,
);
if (isDefined(data?.exchangeAuthorizationCode))
return data.exchangeAuthorizationCode;
else return null;
};
// export const RenewToken = async (appToken: string): Promise<Tokens | null> => {
// const data = await callQuery<Tokens>(RENEW_TOKEN, { appToken });
// if (isDefined(data)) return data;
// else return null;
// };

View File

@ -13,35 +13,29 @@ import { callMutation, callQuery } from '../utils/requestDb';
export const fetchCompany = async (
companyfilerInput: CompanyFilterInput,
): Promise<Company | null> => {
try {
const data = await callQuery<FindCompanyResponse>(FIND_COMPANY, {
filter: {
...companyfilerInput,
},
});
if (isDefined(data?.companies.edges)) {
return data?.companies.edges.length > 0
? data?.companies.edges[0].node
: null;
}
return null;
} catch (error) {
return null;
const data = await callQuery<FindCompanyResponse>(FIND_COMPANY, {
filter: {
...companyfilerInput,
},
});
if (isDefined(data?.companies.edges)) {
return data.companies.edges.length > 0
? isDefined(data.companies.edges[0].node)
? data.companies.edges[0].node
: null
: null;
}
return null;
};
export const createCompany = async (
company: CompanyInput,
): Promise<string | null> => {
try {
const data = await callMutation<CreateCompanyResponse>(CREATE_COMPANY, {
input: company,
});
if (isDefined(data)) {
return data.createCompany.id;
}
return null;
} catch (error) {
return null;
const data = await callMutation<CreateCompanyResponse>(CREATE_COMPANY, {
input: company,
});
if (isDefined(data)) {
return data.createCompany.id;
}
return null;
};

View File

@ -13,33 +13,29 @@ import { callMutation, callQuery } from '../utils/requestDb';
export const fetchPerson = async (
personFilterData: PersonFilterInput,
): Promise<Person | null> => {
try {
const data = await callQuery<FindPersonResponse>(FIND_PERSON, {
filter: {
...personFilterData,
},
});
if (isDefined(data?.people.edges)) {
return data?.people.edges.length > 0 ? data?.people.edges[0].node : null;
}
return null;
} catch (error) {
return null;
const data = await callQuery<FindPersonResponse>(FIND_PERSON, {
filter: {
...personFilterData,
},
});
if (isDefined(data?.people.edges)) {
return data.people.edges.length > 0
? isDefined(data.people.edges[0].node)
? data.people.edges[0].node
: null
: null;
}
return null;
};
export const createPerson = async (
person: PersonInput,
): Promise<string | null> => {
try {
const data = await callMutation<CreatePersonResponse>(CREATE_PERSON, {
input: person,
});
if (isDefined(data?.createPerson)) {
return data.createPerson.id;
}
return null;
} catch (error) {
return null;
const data = await callMutation<CreatePersonResponse>(CREATE_PERSON, {
input: person,
});
if (isDefined(data?.createPerson)) {
return data.createPerson.id;
}
return null;
};

View File

@ -0,0 +1,20 @@
export type AuthToken = {
token: string;
expiresAt: Date;
};
export type ExchangeAuthCodeInput = {
authorizationCode: string;
codeVerifier?: string;
clientSecret?: string;
};
export type Tokens = {
loginToken: AuthToken;
accessToken: AuthToken;
refreshToken: AuthToken;
};
export type ExchangeAuthCodeResponse = {
exchangeAuthorizationCode: Tokens;
};