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

feat: Add BI webview reconnection #1656

Merged
merged 4 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -95,8 +95,10 @@ const DumbEditAccountModal = withRouter(
showError={true}
onVaultDismiss={redirectToAccount}
fieldOptions={fieldOptions}
reconnect={fromReconnect}
/>
)}
<div className="u-mb-2" />
</DialogContent>
</Dialog>
)
Expand Down
11 changes: 6 additions & 5 deletions packages/cozy-harvest-lib/src/components/OAuthForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,31 @@ export class OAuthForm extends PureComponent {
}

render() {
const { konnector, t, flowState } = this.props
const { initialValues, showOAuthWindow, needExtraParams, extraParams } =
this.state
const { konnector, t, flowState, reconnect, account } = this.props
doubleface marked this conversation as resolved.
Show resolved Hide resolved
const { showOAuthWindow, needExtraParams, extraParams } = this.state
const isBusy =
showOAuthWindow === true ||
flowState.running ||
(needExtraParams && !extraParams)

return initialValues ? null : (
doubleface marked this conversation as resolved.
Show resolved Hide resolved
return (
<>
<Button
className="u-mt-1"
busy={isBusy}
disabled={isBusy}
extension="full"
label={t('oauth.connect.label')}
label={t(`oauth.${reconnect ? 'reconnect' : 'connect'}.label`)}
doubleface marked this conversation as resolved.
Show resolved Hide resolved
onClick={this.handleConnect}
/>
{showOAuthWindow && (
<OAuthWindow
extraParams={extraParams}
konnector={konnector}
reconnect={reconnect}
onSuccess={this.handleAccountId}
onCancel={this.handleOAuthCancel}
account={account}
/>
)}
</>
Expand Down
5 changes: 3 additions & 2 deletions packages/cozy-harvest-lib/src/components/OAuthForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ describe('OAuthForm', () => {
expect(component).toMatchSnapshot()
})

it('should not render button when update', () => {
it('should render reconnect button when update', () => {
doubleface marked this conversation as resolved.
Show resolved Hide resolved
const component = shallow(
<OAuthForm
flowState={{}}
account={{ oauth: { access_token: '1234abcd' } }}
konnector={fixtures.konnector}
reconnect={true}
t={t}
/>
).getElement()
expect(component).toBeNull()
expect(component).toMatchSnapshot()
})
it('should call policy fetchExtraOAuthUrlParams with proper params', () => {
shallow(
Expand Down
6 changes: 4 additions & 2 deletions packages/cozy-harvest-lib/src/components/OAuthWindow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class OAuthWindow extends PureComponent {
}

componentDidMount() {
const { client, konnector, redirectSlug, extraParams } = this.props
const { client, konnector, redirectSlug, extraParams, reconnect, account } = this.props
doubleface marked this conversation as resolved.
Show resolved Hide resolved
this.realtime = new CozyRealtime({ client })
this.realtime.subscribe(
'notified',
Expand All @@ -57,7 +57,9 @@ export class OAuthWindow extends PureComponent {
client,
konnector,
redirectSlug,
extraParams
extraParams,
reconnect,
account
)
this.setState({ oAuthStateKey, oAuthUrl, succeed: false })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ export class DumbTriggerManager extends Component {
flow,
flowState,
client,
OAuthFormWrapperComp
OAuthFormWrapperComp,
reconnect
doubleface marked this conversation as resolved.
Show resolved Hide resolved
} = this.props

const submitting = flowState.running
Expand All @@ -345,6 +346,7 @@ export class DumbTriggerManager extends Component {
client={client}
flow={flow}
account={account}
reconnect={reconnect}
konnector={konnector}
onSuccess={this.handleOAuthAccountId}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ exports[`OAuthForm should render 1`] = `
/>
</React.Fragment>
`;

exports[`OAuthForm should render reconnect button when update 1`] = `
<React.Fragment>
<DefaultButton
busy={true}
className="u-mt-1"
disabled={true}
extension="full"
label="oauth.reconnect.label"
onClick={[Function]}
/>
</React.Fragment>
`;
21 changes: 17 additions & 4 deletions packages/cozy-harvest-lib/src/helpers/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ export const getOAuthUrl = ({
oAuthConf = {},
nonce,
redirectSlug,
extraParams
extraParams,
reconnect,
account
doubleface marked this conversation as resolved.
Show resolved Hide resolved
}) => {
let oAuthUrl = `${cozyUrl}/accounts/${accountType}/start?state=${oAuthStateKey}&nonce=${nonce}`
const startOrReconnect = reconnect ? 'reconnect' : 'start'
const accountIdParam = reconnect ? account._id + '/' : ''
let oAuthUrl = `${cozyUrl}/accounts/${accountType}/${accountIdParam}${startOrReconnect}?state=${oAuthStateKey}&nonce=${nonce}`
if (
oAuthConf.scope !== undefined &&
oAuthConf.scope !== null &&
Expand Down Expand Up @@ -148,7 +152,14 @@ const getAppSlug = client => {
* @return {Object} Object containing: `oAuthUrl` (URL of cozy stack
* OAuth endpoint) and `oAuthStateKey` (localStorage key)
*/
export const prepareOAuth = (client, konnector, redirectSlug, extraParams) => {
export const prepareOAuth = (
client,
konnector,
redirectSlug,
extraParams,
reconnect = false,
account
doubleface marked this conversation as resolved.
Show resolved Hide resolved
) => {
const { oauth } = konnector
const accountType = konnectors.getAccountType(konnector)

Expand All @@ -168,7 +179,9 @@ export const prepareOAuth = (client, konnector, redirectSlug, extraParams) => {
oAuthConf: oauth,
nonce: Date.now(),
redirectSlug: redirectSlug || getAppSlug(client),
extraParams
extraParams,
reconnect,
account
})

return { oAuthStateKey, oAuthUrl }
Expand Down
15 changes: 15 additions & 0 deletions packages/cozy-harvest-lib/src/helpers/oauth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ describe('Oauth helper', () => {
'https://cozyurl/accounts/testslug/start?state=statekey&nonce=1234&token=thetoken&id_connector=40'
)
})
it('should return reconnect url with account id if reconnect', () => {
const url = getOAuthUrl({
...defaultConf,
oAuthConf: {},
account: { _id: 'accountid' },
reconnect: true,
extraParams: {
code: 'thecode',
connection_id: 12345
}
})
expect(url).toEqual(
'https://cozyurl/accounts/testslug/accountid/reconnect?state=statekey&nonce=1234&code=thecode&connection_id=12345'
)
})
})
describe('handleOAuthResponse', () => {
let originalLocation
Expand Down
3 changes: 3 additions & 0 deletions packages/cozy-harvest-lib/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@
}
},
"oauth": {
"reconnect": {
"label": "Reconnect"
},
"connect": {
"label": "Connect"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/cozy-harvest-lib/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@
}
},
"oauth": {
"reconnect": {
"label": "Se reconnecter"
},
"connect": {
"label": "Connecter"
},
Expand Down
34 changes: 32 additions & 2 deletions packages/cozy-harvest-lib/src/services/biWebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
saveBIConfig,
findAccountWithBiConnection,
convertBIErrortoKonnectorJobError,
isBudgetInsightConnector
isBudgetInsightConnector,
getBIConnectionIdFromAccount
} from './budget-insight'
import { KonnectorJobError } from '../helpers/konnectors'
import { waitForRealtimeEvent } from './jobUtils'
Expand Down Expand Up @@ -189,6 +190,19 @@ export const onBIAccountCreation = async ({
return updatedAccount
}

const getReconnectExtraOAuthUrlParams = async ({
biBankId,
biBankIds,
token,
connId
}) => {
doubleface marked this conversation as resolved.
Show resolved Hide resolved
return {
id_connector: biBankId || biBankIds,
code: token,
connection_id: connId
}
}

export const fetchExtraOAuthUrlParams = async ({
client,
konnector,
Expand All @@ -204,7 +218,23 @@ export const fetchExtraOAuthUrlParams = async ({
account
})

return { id_connector: biBankId || biBankIds, token }
const connId = getBIConnectionIdFromAccount(account)

const isReconnect = Boolean(connId)

if (isReconnect) {
return getReconnectExtraOAuthUrlParams({
biBankId,
biBankIds,
token,
connId
})
} else {
return {
id_connector: biBankId || biBankIds,
token
}
}
}

/**
Expand Down
Loading