Skip to content

Commit

Permalink
fix(harvest): Handle Two FA code retries correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
CPatchane committed May 6, 2019
1 parent 701c610 commit 24906ec
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions packages/cozy-harvest-lib/src/helpers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import manifest from './manifest'

const DEFAULT_TWOFA_CODE_PROVIDER_TYPE = 'default'

const TWOFA_NEEDED_STATUSES = {
[DEFAULT_TWOFA_CODE_PROVIDER_TYPE]: 'TWOFA_NEEDED',
email: 'TWOFA_NEEDED.EMAIL',
sms: 'TWOFA_NEEDED.SMS'
const PROVIDERS = {
EMAIL: 'email',
SMS: 'sms'
}

const TWOFA_RETRY_STATUS = 'TWOFA_NEEDED_RETRY'
const TWOFA_NEEDED_STATUS = 'TWOFA_NEEDED'
const TWOFA_NEEDED_RETRY_STATUS = 'TWOFA_NEEDED_RETRY'
const RESET_SESSION_STATE = 'RESET_SESSION'

/**
Expand All @@ -21,16 +21,13 @@ const RESET_SESSION_STATE = 'RESET_SESSION'
* @return {Boolean}
*/
export const isTwoFANeeded = status => {
for (let s in TWOFA_NEEDED_STATUSES) {
if (TWOFA_NEEDED_STATUSES[s] === status) {
return true
}
}
return false
if (!status) return false
return status.split('.')[0] === TWOFA_NEEDED_STATUS
}

export const isTwoFARetry = status => {
return status === TWOFA_RETRY_STATUS
if (!status) return false
return status.split('.')[0] === TWOFA_NEEDED_RETRY_STATUS
}

/**
Expand All @@ -40,9 +37,12 @@ export const isTwoFARetry = status => {
*/
export const getTwoFACodeProvider = account => {
if (!account || !account.state) return DEFAULT_TWOFA_CODE_PROVIDER_TYPE
return Object.keys(TWOFA_NEEDED_STATUSES).find(
s => TWOFA_NEEDED_STATUSES[s] === account.state
)
const codeParts = account.state ? account.state.split('.') : []
if (codeParts.length > 1) {
return PROVIDERS[codeParts[1]] || DEFAULT_TWOFA_CODE_PROVIDER_TYPE
} else {
return DEFAULT_TWOFA_CODE_PROVIDER_TYPE
}
}

/**
Expand Down

0 comments on commit 24906ec

Please sign in to comment.