webssh2-server
Version:
A Websocket to SSH2 gateway using xterm.js, socket.io, ssh2
80 lines (79 loc) • 2.12 kB
JavaScript
/**
* Authentication state reducer
*/
const resetStatusActions = new Set(['CONNECTION_ERROR', 'CONNECTION_CLOSED']);
const noOpActions = new Set([
'CONNECTION_START',
'CONNECTION_ESTABLISHED',
'CONNECTION_ACTIVITY',
'TERMINAL_INIT',
'TERMINAL_RESIZE',
'TERMINAL_SET_TERM',
'TERMINAL_SET_ENV',
'TERMINAL_UPDATE_ENV',
'TERMINAL_SET_CWD',
'TERMINAL_DESTROY',
'METADATA_SET_CLIENT',
'METADATA_UPDATE',
'METADATA_UPDATE_TIMESTAMP',
'SESSION_RESET',
'SESSION_END'
]);
export const authReducer = (state, action) => {
if (isAuthAction(action, 'AUTH_REQUEST')) {
return {
...state,
status: 'pending',
method: action.payload.method,
username: action.payload.username ?? state.username,
errorMessage: null
};
}
if (isAuthAction(action, 'AUTH_SUCCESS')) {
return {
status: 'authenticated',
method: action.payload.method,
username: action.payload.username,
timestamp: Date.now(),
errorMessage: null
};
}
if (isAuthAction(action, 'AUTH_FAILURE')) {
return {
...state,
status: 'failed',
method: action.payload.method,
errorMessage: action.payload.error,
timestamp: Date.now()
};
}
if (isAuthAction(action, 'AUTH_LOGOUT')) {
return {
status: 'pending',
method: null,
username: null,
timestamp: Date.now(),
errorMessage: null
};
}
if (isAuthAction(action, 'AUTH_CLEAR_ERROR')) {
return {
...state,
errorMessage: null
};
}
if (resetStatusActions.has(action.type)) {
const status = state.status === 'authenticated' ? 'pending' : state.status;
return {
...state,
status
};
}
if (noOpActions.has(action.type)) {
return state;
}
return state;
};
function isAuthAction(action, type) {
return action.type === type;
}