Last active
July 9, 2020 23:06
-
-
Save dstockto/409f11349ef5c1322be63e2b7818112c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const hasCredentials = (ctx, evt) => { | |
return ctx.username && ctx.password; | |
} | |
const hasMfaCredentials = (ctx, evt) => { | |
return hasCredentials(ctx, evt) && ctx.mfaCode; | |
} | |
const badMfaLimit = (ctx) => { | |
return ctx.badMfaCount >= 3; | |
} | |
const badLoginLimit = (ctx) => { | |
return ctx.badPasswordCount >= 3; | |
} | |
const fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'loggedOut', | |
context: { | |
username: null, | |
password: null, | |
mfaCode: null, | |
badPasswordCount: 0, | |
badMfaCount: 0, | |
}, | |
states: { | |
loggedOut: { | |
on: { | |
SUBMIT_CREDENTIALS: { | |
cond: hasCredentials, | |
target: 'checkCredentials' | |
}, | |
PROVIDE_USERNAME: { | |
target: 'loggedOut', | |
actions: assign({username: 'dave'}) | |
}, | |
PROVIDE_PASSWORD: { | |
target: 'loggedOut', | |
actions: assign({password: 'password'}) | |
}, | |
I_HAVE_MFA: { | |
target: 'getMfaCode', | |
cond: hasCredentials | |
} | |
} | |
}, | |
checkCredentials: { | |
on: { | |
LOGIN_SUCCESS: 'loggedIn', | |
LOGIN_FAILURE: { | |
target: 'loggedOut', | |
actions: assign(ctx => ({badPasswordCount: ctx.badPasswordCount+1})) | |
}, | |
MFA_REQUIRED: 'getMfaCode', | |
"":{ | |
target: 'lockedOut', | |
cond: badLoginLimit, | |
} | |
}, | |
}, | |
getMfaCode: { | |
on: { | |
LOGIN_MFA: { | |
target: 'checkMfaLogin', | |
cond: hasMfaCredentials, | |
}, | |
I_HAVE_NO_MFA: { | |
target: 'loggedOut', | |
actions: assign({mfaCode: ''}) | |
}, | |
PROVIDE_MFA_CODE: { | |
actions: assign({mfaCode:'123456'}) | |
} | |
} | |
}, | |
checkMfaLogin: { | |
on: { | |
MFA_SUCCESS: { | |
target: 'loggedIn' | |
}, | |
MFA_FAIL: { | |
target: 'getMfaCode', | |
actions: assign((ctx) => ({badMfaCount: ctx.badMfaCount+1})) | |
}, | |
"": { | |
target: 'lockedOut', | |
cond: badMfaLimit, | |
} | |
} | |
}, | |
loggedIn: { | |
on: { | |
LOGOUT: { | |
actions: assign({ | |
username: '', | |
password: '', | |
mfaCode: '', | |
}), | |
target: 'loggedOut' | |
} | |
}, | |
entry: | |
assign({ | |
badPasswordCount: 0, | |
badMfaCount: 0 | |
}) | |
}, | |
lockedOut: { | |
type:'final' | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment