Skip to content

Commit

Permalink
Store OAuth2 authorization information
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrygamat committed Apr 14, 2024
1 parent 582947f commit 857c459
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { saveCollectionRoot, sendCollectionOauth2Request } from 'providers/Redux
import StyledWrapper from './StyledWrapper';
import { inputsConfig } from './inputsConfig';
import { updateCollectionAuth } from 'providers/ReduxStore/slices/collections/index';
import { clearOauth2Cache } from 'utils/network';
import toast from 'react-hot-toast';

const OAuth2ClientCredentials = ({ collection }) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -39,6 +41,16 @@ const OAuth2ClientCredentials = ({ collection }) => {
);
};

const handleClearCache = (e) => {
clearOauth2Cache(collection?.uid)
.then(() => {
toast.success('cleared cache successfully');
})
.catch((err) => {
toast.error(err.message);
});
};

return (
<StyledWrapper className="mt-2 flex w-full gap-4 flex-col">
{inputsConfig.map((input) => {
Expand All @@ -59,9 +71,14 @@ const OAuth2ClientCredentials = ({ collection }) => {
</div>
);
})}
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<div className="flex flex-row gap-4">
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<button onClick={handleClearCache} className="submit btn btn-sm btn-secondary w-fit">
Clear Cache
</button>
</div>
</StyledWrapper>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { saveCollectionRoot, sendCollectionOauth2Request } from 'providers/Redux
import StyledWrapper from './StyledWrapper';
import { inputsConfig } from './inputsConfig';
import { updateCollectionAuth } from 'providers/ReduxStore/slices/collections/index';
import { clearOauth2Cache } from 'utils/network';
import toast from 'react-hot-toast';

const OAuth2AuthorizationCode = ({ item, collection }) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -41,6 +43,16 @@ const OAuth2AuthorizationCode = ({ item, collection }) => {
);
};

const handleClearCache = (e) => {
clearOauth2Cache(collection?.uid)
.then(() => {
toast.success('cleared cache successfully');
})
.catch((err) => {
toast.error(err.message);
});
};

return (
<StyledWrapper className="mt-2 flex w-full gap-4 flex-col">
{inputsConfig.map((input) => {
Expand All @@ -61,9 +73,14 @@ const OAuth2AuthorizationCode = ({ item, collection }) => {
</div>
);
})}
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<div className="flex flex-row gap-4">
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<button onClick={handleClearCache} className="submit btn btn-sm btn-secondary w-fit">
Clear Cache
</button>
</div>
</StyledWrapper>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { updateAuth } from 'providers/ReduxStore/slices/collections';
import { saveRequest, sendRequest } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
import { inputsConfig } from './inputsConfig';
import { clearOauth2Cache } from 'utils/network';
import toast from 'react-hot-toast';

const OAuth2ClientCredentials = ({ item, collection }) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -40,6 +42,16 @@ const OAuth2ClientCredentials = ({ item, collection }) => {
);
};

const handleClearCache = (e) => {
clearOauth2Cache(collection?.uid)
.then(() => {
toast.success('cleared cache successfully');
})
.catch((err) => {
toast.error(err.message);
});
};

return (
<StyledWrapper className="mt-2 flex w-full gap-4 flex-col">
{inputsConfig.map((input) => {
Expand All @@ -60,9 +72,14 @@ const OAuth2ClientCredentials = ({ item, collection }) => {
</div>
);
})}
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<div className="flex flex-row gap-4">
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<button onClick={handleClearCache} className="submit btn btn-sm btn-secondary w-fit">
Clear Cache
</button>
</div>
</StyledWrapper>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { updateAuth } from 'providers/ReduxStore/slices/collections';
import { saveRequest, sendRequest } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
import { inputsConfig } from './inputsConfig';
import { clearOauth2Cache } from 'utils/network';
import toast from 'react-hot-toast';

const OAuth2AuthorizationCode = ({ item, collection }) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -42,6 +44,16 @@ const OAuth2AuthorizationCode = ({ item, collection }) => {
);
};

const handleClearCache = (e) => {
clearOauth2Cache(collection?.uid)
.then(() => {
toast.success('cleared cache successfully');
})
.catch((err) => {
toast.error(err.message);
});
};

return (
<StyledWrapper className="mt-2 flex w-full gap-4 flex-col">
{inputsConfig.map((input) => {
Expand All @@ -62,9 +74,14 @@ const OAuth2AuthorizationCode = ({ item, collection }) => {
</div>
);
})}
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<div className="flex flex-row gap-4">
<button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit">
Get Access Token
</button>
<button onClick={handleClearCache} className="submit btn btn-sm btn-secondary w-fit">
Clear Cache
</button>
</div>
</StyledWrapper>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ const authorizeUserInWindowImplicit = ({ authorizeUrl, session }) => {
if (finalUrl) {
try {
const uriFragmentWithToken = new URLSearchParams(new URL(finalUrl).hash.slice(1));
const accessToken = uriFragmentWithToken.get('access_token');
return resolve({ accessToken });
const oauth2Data = {
access_token: uriFragmentWithToken.get('access_token'),
token_type: uriFragmentWithToken.get('token_type'),
expires_in: uriFragmentWithToken.get('expires_in')
};
return resolve({ oauth2Data });
} catch (error) {
return reject(error);
}
Expand Down
24 changes: 12 additions & 12 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../../utils/proxy-ut
const { chooseFileToSave, writeBinaryFile } = require('../../utils/filesystem');
const { getCookieStringForUrl, addCookieToJar, getDomainsWithCookies } = require('../../utils/cookies');
const {
getOAuth2AuthorizationCodeAccessToken,
getOAuth2ClientCredentialsAccessToken,
getOAuth2PasswordCredentialsAccessToken,
getOAuth2ImplicitAccessToken
getOAuth2DataAuthorizationCode,
getOAuth2DataClientCredentials,
getOAuth2DataPasswordCredentials,
getOAuth2DataImplicit
} = require('./oauth2-helper');
const Oauth2Store = require('../../store/oauth2');

Expand Down Expand Up @@ -207,26 +207,26 @@ const configureRequest = async (
switch (request?.oauth2?.grantType) {
case 'authorization_code': {
interpolateVars(requestCopy, envVars, collectionVariables, processEnvVars);
const { accessToken } = await getOAuth2AuthorizationCodeAccessToken(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${accessToken}`;
const { oauth2Data } = await getOAuth2DataAuthorizationCode(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${oauth2Data.access_token}`;
break;
}
case 'client_credentials': {
interpolateVars(requestCopy, envVars, collectionVariables, processEnvVars);
const { accessToken } = await getOAuth2ClientCredentialsAccessToken(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${accessToken}`;
const { oauth2Data } = await getOAuth2DataClientCredentials(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${oauth2Data.access_token}`;
break;
}
case 'password': {
interpolateVars(requestCopy, envVars, collectionVariables, processEnvVars);
const { accessToken } = await getOAuth2PasswordCredentialsAccessToken(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${accessToken}`;
const { oauth2Data } = await getOAuth2DataPasswordCredentials(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${oauth2Data.access_token}`;
break;
}
case 'implicit': {
interpolateVars(requestCopy, envVars, collectionVariables, processEnvVars);
const { accessToken } = await getOAuth2ImplicitAccessToken(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${accessToken}`;
const { oauth2Data } = await getOAuth2DataImplicit(requestCopy, collectionUid);
request.headers['Authorization'] = `Bearer ${oauth2Data.access_token}`;
break;
}
}
Expand Down
14 changes: 0 additions & 14 deletions packages/bruno-electron/src/ipc/network/interpolate-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
request.oauth2.clientId = clientId;
request.oauth2.clientSecret = clientSecret;
request.oauth2.scope = scope;
request.data = {
grant_type: 'password',
username,
password,
client_id: clientId,
client_secret: clientSecret,
scope
};
break;
case 'authorization_code':
request.oauth2.callbackUrl = _interpolate(request.oauth2.callbackUrl) || '';
Expand All @@ -149,12 +141,6 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
request.oauth2.clientId = clientId;
request.oauth2.clientSecret = clientSecret;
request.oauth2.scope = scope;
request.data = {
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope
};
break;
case 'implicit':
request.oauth2.callbackUrl = _interpolate(request.oauth2.callbackUrl) || '';
Expand Down
Loading

0 comments on commit 857c459

Please sign in to comment.