Skip to content

Commit

Permalink
feat: Use BI manage url to update contract synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleface authored and doubleface committed Oct 24, 2022
1 parent baf93d9 commit fad2994
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Expand Up @@ -43,7 +43,8 @@ const BIContractActivationWindow = ({
const result = await konnectorPolicy.fetchExtraOAuthUrlParams({
client,
account,
konnector
konnector,
manage: true
})
setExtraParams(result)
}
Expand Down Expand Up @@ -78,6 +79,7 @@ const BIContractActivationWindow = ({
intentsApi={intentsApi}
onSuccess={onPopupClosed}
onCancel={onPopupClosed}
manage={true}
/>
)}
</ListItem>
Expand Down
14 changes: 12 additions & 2 deletions packages/cozy-harvest-lib/src/components/OAuthWindow.jsx
Expand Up @@ -47,8 +47,15 @@ export class OAuthWindow extends PureComponent {
}

componentDidMount() {
const { client, konnector, redirectSlug, extraParams, reconnect, account } =
this.props
const {
client,
konnector,
redirectSlug,
extraParams,
reconnect = false,
manage = false,
account
} = this.props
this.realtime = new CozyRealtime({ client })
this.realtime.subscribe(
'notified',
Expand All @@ -62,6 +69,7 @@ export class OAuthWindow extends PureComponent {
redirectSlug,
extraParams,
reconnect,
manage,
account
)
this.setState({ oAuthStateKey, oAuthUrl, succeed: false })
Expand Down Expand Up @@ -195,6 +203,8 @@ OAuthWindow.propTypes = {
redirectSlug: PropTypes.string,
/** Is it a reconnection or not */
reconnect: PropTypes.bool,
/** Are we trying to edit an existing account (without reconnection) */
manage: PropTypes.bool,
/** Existing account */
account: PropTypes.object,
/** custom intents api. Can have fetchSessionCode, showInAppBrowser, closeInAppBrowser at the moment */
Expand Down
16 changes: 13 additions & 3 deletions packages/cozy-harvest-lib/src/helpers/oauth.js
Expand Up @@ -102,6 +102,7 @@ export const handleOAuthResponse = (options = {}) => {
* @param {string} nonce unique nonce string
* @param {Object} extraParams some extra parameters to add to the query string
* @param {Boolean} reconnect Are we trying to reconnect an existing account ?
* @param {Boolean} manage Are we trying to manage an existing account ?
* @param {io.cozy.accounts} account targeted account if any
* @returns {String} final OAuth url string
*/
Expand All @@ -113,13 +114,19 @@ export const getOAuthUrl = ({
nonce,
redirectSlug,
extraParams,
manage,
reconnect,
account
}) => {
const startOrReconnect = reconnect ? 'reconnect' : 'start'
const accountIdParam = reconnect ? account._id + '/' : ''
let command = 'start'
if (manage) {
command = 'manage'
} else if (reconnect) {
command = 'reconnect'
}
const accountIdParam = reconnect || manage ? account._id + '/' : ''
const oAuthUrl = new URL(
`${cozyUrl}/accounts/${accountType}/${accountIdParam}${startOrReconnect}`
`${cozyUrl}/accounts/${accountType}/${accountIdParam}${command}`
)
oAuthUrl.searchParams.set('state', oAuthStateKey)
oAuthUrl.searchParams.set('nonce', nonce)
Expand Down Expand Up @@ -160,6 +167,7 @@ const getAppSlug = client => {
* @param {string} redirectSlug The app we want to redirect the user on after the end of the flow
* @param {Object} extraParams some extra parameters to add to the query string
* @param {Boolean} reconnect Are we trying to reconnect an existing account ?
* @param {Boolean} manage Are we trying to manage an existing account ?
* @param {io.cozy.accounts} account targetted account if any
* @return {Object} Object containing: `oAuthUrl` (URL of cozy stack OAuth endpoint) and `oAuthStateKey` (localStorage key)
*/
Expand All @@ -169,6 +177,7 @@ export const prepareOAuth = (
redirectSlug,
extraParams,
reconnect = false,
manage = false,
account
) => {
const { oauth } = konnector
Expand All @@ -192,6 +201,7 @@ export const prepareOAuth = (
redirectSlug: redirectSlug || getAppSlug(client),
extraParams,
reconnect,
manage,
account
})

Expand Down
18 changes: 13 additions & 5 deletions packages/cozy-harvest-lib/src/services/biWebView.js
Expand Up @@ -201,14 +201,19 @@ export const onBIAccountCreation = async ({
}

/**
* Create OAuth extra parameters specific to reconnect webview
* Create OAuth extra parameters specific to reconnect or manage webview (which need the same parameters)
*
* @param {object} options
* @param {Array<String>} options.biBankIds - connector bank ids (for webview connectors)
* @param {String} options.token - BI temporary token
* @param {Number} options.connId - BI bi connection id
* @return {Object}
*/
const getReconnectExtraOAuthUrlParams = ({ biBankIds, token, connId }) => {
const getReconnectOrManageExtraOAuthUrlParams = ({
biBankIds,
token,
connId
}) => {
return {
id_connector: biBankIds,
code: token,
Expand All @@ -218,18 +223,21 @@ const getReconnectExtraOAuthUrlParams = ({ biBankIds, token, connId }) => {

/**
* Create OAuth extra parameters
*
* @param {object} options
* @param {CozyClient} options.client - CozyClient instance
* @param {KonnectorManifest} options.konnector konnector manifest content
* @param {IoCozyAccount} options.account The account content
* @param {Boolean} options.reconnect If this is a reconnection
* @param {Boolean} options.manage If this is a manage
* @return {Promise<Object>}
*/
export const fetchExtraOAuthUrlParams = async ({
client,
konnector,
account,
reconnect = false
reconnect = false,
manage = false
}) => {
const {
code: token,
Expand All @@ -243,8 +251,8 @@ export const fetchExtraOAuthUrlParams = async ({

const connId = getBIConnectionIdFromAccount(account)

if (reconnect) {
return getReconnectExtraOAuthUrlParams({
if (reconnect || manage) {
return getReconnectOrManageExtraOAuthUrlParams({
biBankIds,
token,
connId
Expand Down
14 changes: 9 additions & 5 deletions packages/cozy-harvest-lib/src/services/biWebView.spec.js
Expand Up @@ -355,7 +355,7 @@ describe('fetchExtraOAuthUrlParams', () => {
expect(result).toMatchObject({ id_connector: [2] })
})

it('should fetch reconnect params if any connection id in the account', async () => {
it('should fetch reconnect params', async () => {
const client = new CozyClient({
uri: 'http://testcozy.mycozy.cloud'
})
Expand All @@ -378,23 +378,27 @@ describe('fetchExtraOAuthUrlParams', () => {
})
})

it('should not add connection_id param if no connection_id', async () => {
it('should fetch manage params', async () => {
const client = new CozyClient({
uri: 'http://testcozy.mycozy.cloud'
})
client.query = jest.fn().mockResolvedValue({
data: {
timestamp: Date.now(),
code: 'bi-temporary-access-token-12',
code: 'bi-temporary-access-token-12-manage',
biMapping: { [TEST_BANK_COZY_ID]: 2 }
}
})
const result = await fetchExtraOAuthUrlParams({
client,
konnector,
account
account: { ...account, data: { auth: { bi: { connId: 15 } } } },
manage: true
})
expect(result).toMatchObject({
connection_id: 15,
code: 'bi-temporary-access-token-12-manage'
})
expect(result).not.toHaveProperty('connection_id')
})

it('should get bi connection bank id if multiple biBankIds are in the mapping', async () => {
Expand Down

0 comments on commit fad2994

Please sign in to comment.