Created
July 23, 2019 22:45
-
-
Save felippenardi/4df1a5570116e394475b0220b39c8e89 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
const twoFactorMachine = { | |
two_factor: { | |
id: 'two_factor', | |
onEntry: ['checkTwoFactorSettings'], | |
initial: 'unknown', | |
states: { | |
unknown: { | |
on: { | |
'': [ | |
{ target: 'setup', cond: 'needsToSetupTwoFactor' }, | |
{ | |
target: 'validate_sms', | |
cond: 'hasSmsTwoFactor', | |
}, | |
{ target: 'validate_app' }, | |
], | |
}, | |
}, | |
setup: { | |
id: 'two_factor_setup', | |
initial: 'options_screen', | |
states: { | |
options_screen: { | |
on: { | |
CHOOSE_SMS: 'add_phone', | |
CHOOSE_APP: 'get_app', | |
}, | |
}, | |
add_phone: { | |
on: { | |
SMS_SET_UP: '#validate_sms', | |
}, | |
}, | |
qrcode: { | |
on: { | |
APP_SET_UP: '#validate_app', | |
}, | |
}, | |
get_app: { | |
on: { | |
CONTINUE: 'qrcode', | |
}, | |
}, | |
}, | |
}, | |
validate_app: { | |
id: 'validate_app', | |
on: { | |
APP_VERIFICATION_DONE: '#redirecting', | |
}, | |
}, | |
validate_sms: { | |
id: 'validate_sms', | |
on: { | |
SMS_VERIFICATION_DONE: '#redirecting', | |
}, | |
}, | |
}, | |
}, | |
} | |
const twoFactorGuards = { | |
needsToSetupTwoFactor: ({ userHasTwoFactor } = {}) => !userHasTwoFactor, | |
hasSmsTwoFactor: ({ twoFactorType }) => twoFactorType === 'SMS', | |
} | |
const twoFactorActions = { | |
checkTwoFactorSettings: assign((ctx, { details } = {}) => { | |
return { | |
userHasTwoFactor: details && details.userHasTwoFactor, | |
twoFactorType: details && details.twoFactorType, | |
} | |
}), | |
} | |
const loginMachine = Machine( | |
{ | |
id: 'loginMachine', | |
initial: 'unknown', | |
states: { | |
unknown: { | |
on: { | |
'': [ | |
{ | |
target: 'logged_user_redirect', | |
cond: 'isUserLoggedIn', | |
}, | |
{ | |
target: 'oauth_redirecting', | |
cond: 'hasOnlyOauth', | |
}, | |
{ | |
target: 'saml_redirecting', | |
cond: 'hasOnlySaml', | |
}, | |
{ | |
target: 'identification.identified_users', | |
cond: 'isUserIdentified', | |
}, | |
{ target: 'identification' }, | |
], | |
}, | |
}, | |
logged_user_redirect: { | |
}, | |
saml_redirecting: { | |
id: 'saml_redirecting', | |
}, | |
oauth_redirecting: { | |
id: 'oauth_redirecting', | |
on: { | |
OAUTH_LOGIN_FAILURE: '#identification', | |
}, | |
}, | |
user_managing: { | |
id: 'user_managing', | |
on: { | |
EDITING_DONE: [ | |
{ | |
target: '#unidentified_user', | |
cond: 'hasNoIdentifiedUsers', | |
}, | |
{ target: '#identified_users' }, | |
], | |
}, | |
}, | |
identification: { | |
id: 'identification', | |
on: { | |
OAUTH_LOGIN: '#oauth_redirecting', | |
SAML_LOGIN: '#saml_redirecting', | |
TOKEN_PREFERENCE: 'token_login', | |
PASSWORD_PREFERENCE: 'password_login', | |
FIRST_LOGIN: 'default_login', | |
}, | |
initial: 'unidentified_user', | |
states: { | |
identified_users: { | |
id: 'identified_users', | |
on: { | |
NOT_ME: '#unidentified_user', | |
MANAGE_USERS: '#user_managing', | |
}, | |
}, | |
unidentified_user: { | |
id: 'unidentified_user', | |
on: {}, | |
}, | |
}, | |
}, | |
password_login: { | |
on: { | |
FORGOT_PASSWORD: 'default_login', | |
BACK: 'identification', | |
PASSWORD_EXPIRED: 'update_expired_password', | |
LOGGED_WITH_PASSWORD: 'redirecting', | |
TWO_FACTOR_REQUIRED: 'two_factor', | |
}, | |
}, | |
token_login: { | |
on: { | |
CHANGE_EMAIL: 'identification', | |
LOGGED_WITH_TOKEN: 'redirecting', | |
SWITCH_TO_PASSWORD: '#set_password', | |
}, | |
}, | |
default_login: { | |
id: 'default_login', | |
initial: 'token_confirmation', | |
states: { | |
token_confirmation: { | |
on: { | |
TOKEN_CONFIRMED: 'choose_action', | |
CHANGE_EMAIL: '#identification', | |
}, | |
}, | |
choose_action: { | |
on: { | |
CHOOSE_CONTINUE_WITH_TOKEN: '#redirecting', | |
CHOOSE_CONTINUE_WITH_PASSWORD: '#set_password', | |
}, | |
}, | |
}, | |
}, | |
...twoFactorMachine, | |
update_expired_password: { | |
on: { | |
PASSWORD_UPDATED: 'redirecting', | |
TWO_FACTOR_REQUIRED: 'two_factor', | |
}, | |
}, | |
set_password: { | |
id: 'set_password', | |
on: { | |
PASSWORD_SET: { | |
target: '#redirecting', | |
}, | |
}, | |
}, | |
redirecting: { | |
id: 'redirecting', | |
type: 'final', | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
...twoFactorActions, | |
}, | |
guards: { | |
...twoFactorGuards, | |
isUserLoggedIn: ({ isUserAuthenticated } = {}) => isUserAuthenticated, | |
isUserIdentified: ({ isUserIdentified } = {}) => isUserIdentified, | |
hasOnlyOauth: ({ | |
identityProviders: { | |
samlProviders = [], | |
googleOAuth, | |
password, | |
accessKey, | |
} = {}, | |
} = {}) => { | |
return ( | |
!password && !accessKey && samlProviders.length === 0 && googleOAuth | |
) | |
}, | |
hasOnlySaml: ({ | |
identityProviders: { | |
samlProviders = [], | |
googleOAuth, | |
password, | |
accessKey, | |
} = {}, | |
} = {}) => { | |
return ( | |
!password && !accessKey && samlProviders.length > 0 && !googleOAuth | |
) | |
}, | |
hasNoIdentifiedUsers: (ctx, { details } = {}) => | |
details && details.cachedUsersLength === 0, | |
}, | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment