Skip to content

Commit

Permalink
Merge pull request #539 from cozy/fix--add-protocol-to-instanceUrl-in…
Browse files Browse the repository at this point in the history
…-passphrase

fix: provide instance URL to passphrase function
  • Loading branch information
acezard authored Dec 17, 2022
2 parents f97dbf0 + 1f04637 commit dca41c1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
38 changes: 27 additions & 11 deletions src/actions/passphrase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cozyFetch } from 'actions'
import { WebVaultClient } from 'cozy-keys-lib'
import { getStackDomain } from 'actions/domUtils'

export const UPDATE_PASSPHRASE = 'UPDATE_PASSPHRASE'
export const UPDATE_PASSPHRASE_SUCCESS = 'UPDATE_PASSPHRASE_SUCCESS'
Expand All @@ -21,10 +20,6 @@ export const UPDATE_HINT = 'UPDATE_HINT'
export const UPDATE_HINT_SUCCESS = 'UPDATE_HINT_SUCCESS'
export const UPDATE_HINT_FAILURE = 'UPDATE_HINT_FAILURE'

const getInstanceURL = () => {
return getStackDomain()
}

const invalidPassphraseErrorAction = {
type: UPDATE_PASSPHRASE_FAILURE,
errors: {
Expand Down Expand Up @@ -61,8 +56,17 @@ const updatePassphraseFailure = error => {
return defaultErrorAction
}

export const updatePassphrase = (currentPassphrase, newPassphrase) => {
const instanceURL = getInstanceURL()
/**
* @param {string} currentPassphrase
* @param {string} newPassphrase
* @param {string} instanceURL - Must be a string usable in the `URL()` constructor
* @returns {Promise<void>}
*/
export const updatePassphrase = (
currentPassphrase,
newPassphrase,
instanceURL
) => {
const vaultClient = new WebVaultClient(instanceURL)

return async dispatch => {
Expand Down Expand Up @@ -91,8 +95,12 @@ export const updatePassphrase = (currentPassphrase, newPassphrase) => {
}
}

export const updatePassphrase2FAFirst = currentPassphrase => {
const instanceURL = getInstanceURL()
/**
* @param {string} currentPassphrase
* @param {string} instanceURL - Must be a string usable in the `URL()` constructor
* @returns {Promise<void>}
*/
export const updatePassphrase2FAFirst = (currentPassphrase, instanceURL) => {
const vaultClient = new WebVaultClient(instanceURL)

return async dispatch => {
Expand All @@ -117,17 +125,25 @@ export const updatePassphrase2FAFirst = currentPassphrase => {
}
}

/**
* @param {string} currentPassphrase
* @param {string} newPassphrase
* @param {string} twoFactorCode
* @param {string} twoFactorToken
* @param {string} instanceURL - Must be a string usable in the `URL()` constructor
* @returns {Promise<void>}
*/
export const updatePassphrase2FASecond = (
currentPassphrase,
newPassphrase,
twoFactorCode,
twoFactorToken
twoFactorToken,
instanceURL
) => {
return async dispatch => {
dispatch({ type: UPDATE_PASSPHRASE_2FA_2 })

try {
const instanceURL = getInstanceURL()
const vaultClient = new WebVaultClient(instanceURL)

await vaultClient.login(currentPassphrase)
Expand Down
20 changes: 17 additions & 3 deletions src/containers/Passphrase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { fetchInfos } from 'actions'
import PassphraseView from 'components/PassphraseView'
import { useLocation } from 'react-router-dom'
import { withClient } from 'cozy-client'

const mapStateToProps = state => ({
fields: state.fields,
Expand Down Expand Up @@ -57,7 +58,13 @@ const mapDispatchToProps = (dispatch, ownProps) => ({
onPassphraseSimpleSubmit: async (currentPassphrase, newPassphrase, hint) => {
try {
await dispatch(updateHint(hint))
await dispatch(updatePassphrase(currentPassphrase, newPassphrase))
await dispatch(
updatePassphrase(
currentPassphrase,
newPassphrase,
ownProps.client.getStackClient().uri
)
)
showSuccessThenReload(ownProps.t, ownProps.location)
} catch (e) {
// eslint-disable-next-line no-console
Expand All @@ -66,7 +73,12 @@ const mapDispatchToProps = (dispatch, ownProps) => ({
},
onPassphrase2FAStep1: async currentPassphrase => {
try {
await dispatch(updatePassphrase2FAFirst(currentPassphrase))
await dispatch(
updatePassphrase2FAFirst(
currentPassphrase,
ownProps.client.getStackClient().uri
)
)
} catch (e) {
// eslint-disable-next-line no-console
console.error(e)
Expand All @@ -86,7 +98,8 @@ const mapDispatchToProps = (dispatch, ownProps) => ({
currentPassphrase,
newPassphrase,
twoFactorCode,
twoFactorToken
twoFactorToken,
ownProps.client.getStackClient().uri
)
)
showSuccessThenReload(ownProps.t, ownProps.location)
Expand All @@ -100,6 +113,7 @@ const mapDispatchToProps = (dispatch, ownProps) => ({

const ComposedPassphrase = compose(
translate(),
withClient,
connect(mapStateToProps, mapDispatchToProps)
)(PassphraseView)

Expand Down

0 comments on commit dca41c1

Please sign in to comment.