Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes auth flow on web & electron version #7623

Merged
merged 19 commits into from
May 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ProvisionHandoff } from '@bfc/ui-shared';
import sortBy from 'lodash/sortBy';
import { NeutralColors } from '@uifabric/fluent-theme';
import { AzureTenant } from '@botframework-composer/types';
import jwtDecode from 'jwt-decode';

import TelemetryClient from '../../telemetry/TelemetryClient';
import { AuthClient } from '../../utils/authClient';
Expand Down Expand Up @@ -95,7 +96,7 @@ export const ManageService = (props: ManageServiceProps) => {
const [availableSubscriptions, setAvailableSubscriptions] = useState<Subscription[]>([]);
const [subscriptionsErrorMessage, setSubscriptionsErrorMessage] = useState<string>();
const [keys, setKeys] = useState<KeyRec[]>([]);

const [userProvidedTokens, setUserProvidedTokens] = useState<boolean>(false);
VamsiModem marked this conversation as resolved.
Show resolved Hide resolved
const [currentStep, setCurrentStep] = useState<Step>('intro');
const [outcomeDescription, setOutcomeDescription] = useState<string>('');
const [outcomeSummary, setOutcomeSummary] = useState<any>();
Expand Down Expand Up @@ -137,15 +138,23 @@ export const ManageService = (props: ManageServiceProps) => {
return [];
}
};
const decodeToken = (token: string) => {
try {
return jwtDecode<any>(token);
} catch (err) {
console.error('decode token error in ', err);
return null;
}
};

useEffect(() => {
if (!userShouldProvideTokens()) {
if (currentStep === 'subscription' && !userShouldProvideTokens()) {
AuthClient.getTenants()
.then((tenants) => {
setAllTenants(tenants);
if (tenants.length === 0) {
setTenantsErrorMessage(formatMessage('No Azure Directories were found.'));
} else if (tenants.length === 1) {
} else if (tenants.length >= 1) {
setTenantId(tenants[0].tenantId);
} else {
setTenantsErrorMessage(undefined);
Expand All @@ -159,7 +168,7 @@ export const ManageService = (props: ManageServiceProps) => {
);
});
}
}, []);
}, [currentStep]);

useEffect(() => {
if (tenantId) {
Expand Down Expand Up @@ -210,9 +219,21 @@ export const ManageService = (props: ManageServiceProps) => {
setShowAuthDialog(true);
}
newtoken = getTokenFromCache('accessToken');
}
if (newtoken) {
setToken(newtoken);
if (newtoken) {
const decoded = decodeToken(newtoken);
if (decoded) {
setToken(newtoken);
setUserProvidedTokens(true);
} else {
setTenantsErrorMessage(
formatMessage(
VamsiModem marked this conversation as resolved.
Show resolved Hide resolved
'There was a problem with the authentication access token. Close this dialog and try again. To be prompted to provide the access token again, clear it from application local storage.'
)
);
}
}
} else {
setUserProvidedTokens(false);
}
setCurrentStep('subscription');
};
Expand Down Expand Up @@ -619,7 +640,7 @@ export const ManageService = (props: ManageServiceProps) => {
);
};

const renderResourseCreatorStep = () => {
const renderResourceCreatorStep = () => {
return (
<div>
<div css={dialogBodyStyles}>
Expand Down Expand Up @@ -754,7 +775,7 @@ export const ManageService = (props: ManageServiceProps) => {
<div css={dialogBodyStyles}>
<p css={{ marginTop: 0 }}>
{formatMessage(
'Select your Azure directory, then choose the subscription where you’d like your new {service} resource.',
'Select your Azure directory, then choose the subscription where you’d like your new {service} resource. ',
{ service: props.serviceName }
)}
{props.learnMore ? (
Expand Down Expand Up @@ -799,7 +820,7 @@ export const ManageService = (props: ManageServiceProps) => {
{loading && <Spinner label={loading} labelPosition="right" styles={{ root: { float: 'left' } }} />}
<DefaultButton disabled={!!loading} text={formatMessage('Back')} onClick={() => setCurrentStep('intro')} />
<PrimaryButton
disabled={!!loading || !tenantId || !subscriptionId}
disabled={!!loading || (!userProvidedTokens && !tenantId) || !subscriptionId}
text={formatMessage('Next')}
onClick={() => setCurrentStep('resourceCreation')}
/>
Expand All @@ -820,7 +841,7 @@ export const ManageService = (props: ManageServiceProps) => {
return renderSubscriptionSelectionStep();
}
case 'resourceCreation':
return renderResourseCreatorStep();
return renderResourceCreatorStep();
case 'outcome':
return renderOutcomeStep();
default:
Expand Down