Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto-submit one-time password (OTP) after entering #2257

Merged
merged 10 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Parse-Dashboard/Authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ function initialize(app, options) {
otpCode: req.body.otpCode
});
if (!match.matchingUsername) {
return cb(null, false, { message: 'Invalid username or password' });
}
if (match.otpMissing) {
return cb(null, false, { message: 'Please enter your one-time password.' });
return cb(null, false, { message: JSON.stringify({ text: 'Invalid username or password' }) });
}
if (!match.otpValid) {
return cb(null, false, { message: 'Invalid one-time password.' });
return cb(null, false, { message: JSON.stringify({ text: 'Invalid one-time password.', otpLength: match.otpMissingLength || 6}) });
}
if (match.otpMissingLength) {
return cb(null, false, { message: JSON.stringify({ text: 'Please enter your one-time password.', otpLength: match.otpMissingLength || 6 })});
}
cb(null, match.matchingUsername);
})
Expand Down Expand Up @@ -91,7 +91,7 @@ function authenticate(userToTest, usernameOnly) {
let appsUserHasAccessTo = null;
let matchingUsername = null;
let isReadOnly = false;
let otpMissing = false;
let otpMissingLength = false;
let otpValid = true;

//they provided auth
Expand All @@ -104,17 +104,20 @@ function authenticate(userToTest, usernameOnly) {
let usernameMatches = userToTest.name == user.user;
if (usernameMatches && user.mfa && !usernameOnly) {
if (!userToTest.otpCode) {
otpMissing = true;
otpMissingLength = user.mfaDigits || 6;
} else {
const totp = new OTPAuth.TOTP({
algorithm: user.mfaAlgorithm || 'SHA1',
secret: OTPAuth.Secret.fromBase32(user.mfa)
secret: OTPAuth.Secret.fromBase32(user.mfa),
digits: user.mfaDigits,
period: user.mfaPeriod,
});
const valid = totp.validate({
token: userToTest.otpCode
});
if (valid === null) {
otpValid = false;
otpMissingLength = user.mfaDigits || 6;
}
}
}
Expand All @@ -132,7 +135,7 @@ function authenticate(userToTest, usernameOnly) {
return {
isAuthenticated,
matchingUsername,
otpMissing,
otpMissingLength,
otpValid,
appsUserHasAccessTo,
isReadOnly,
Expand Down
21 changes: 16 additions & 5 deletions Parse-Dashboard/CLI/mfa.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ const showInstructions = ({ app, username, passwordCopied, secret, url, encrypt,

if (secret) {
console.log(
`\n${getOrder()}. Open the authenticator app to scan the QR code above or enter this secret code:` +
`\n\n ${secret}` +
`\n${getOrder()}. Open the authenticator app to scan the QR code above or enter this secret code:` +
`\n\n ${secret}` +
'\n\n If the secret code generates incorrect one-time passwords, try this alternative:' +
`\n\n ${url}` +
`\n\n ${url}` +
`\n\n${getOrder()}. Destroy any records of the QR code and the secret code to secure the account.`
);
}

if (encrypt) {
console.log(
`\n${getOrder()}. Make sure that "useEncryptedPasswords" is set to "true" in your dashboard configuration.` +
Expand Down Expand Up @@ -189,6 +189,12 @@ module.exports = {
if (algorithm !== 'SHA1') {
data.mfaAlgorithm = algorithm;
}
if (digits !== 6) {
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
data.mfaDigits = digits;
}
if (period !== 30) {
data.mfaPeriod = period;
}
showQR(data.url);
}

Expand All @@ -214,12 +220,17 @@ module.exports = {

const { url, secret } = generateSecret({ app, username, algorithm, digits, period });
showQR(url);

// Compose config
const config = { mfa: secret };
if (algorithm !== 'SHA1') {
config.mfaAlgorithm = algorithm;
}
if (digits !== 6) {
config.mfaDigits = digits;
}
if (period !== 30) {
config.mfaPeriod = period;
}
showInstructions({ app, username, secret, url, config });
}
};
4 changes: 2 additions & 2 deletions src/lib/tests/Authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jest.dontMock('bcryptjs');

const Authentication = require('../../../Parse-Dashboard/Authentication');
const apps = [{appId: 'test123'}, {appId: 'test789'}];
const readOnlyApps = apps.map((app) => {
const readOnlyApps = apps.map((app) => {
app.readOnly = true;
return app;
});
Expand Down Expand Up @@ -55,7 +55,7 @@ function createAuthenticationResult(isAuthenticated, matchingUsername, appsUserH
matchingUsername,
appsUserHasAccessTo,
isReadOnly,
otpMissing: false,
otpMissingLength: false,
otpValid: true
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ export default class Login extends React.Component {
super();

let errorDiv = document.getElementById('login_errors');
let otpLength = 6;
if (errorDiv) {
this.errors = errorDiv.innerHTML;
try {
const json = JSON.parse(this.errors)
this.errors = json.text
otpLength = json.otpLength;
} catch (e) {
/* */
dblythy marked this conversation as resolved.
Show resolved Hide resolved
}
}

this.state = {
Expand All @@ -30,6 +38,7 @@ export default class Login extends React.Component {
this.inputRefUser = React.createRef();
this.inputRefPass = React.createRef();
this.inputRefMfa = React.createRef();
this.otpLength = otpLength;
}

componentDidMount() {
Expand All @@ -53,6 +62,15 @@ export default class Login extends React.Component {
const {path} = this.props;
const updateField = (field, e) => {
this.setState({[field]: e.target.value});
if (field === 'otp' && e.target.value.length >= this.otpLength) {
const input = document.querySelectorAll('input');
for (const field of input) {
if (field.type === 'submit') {
field.click();
break;
}
}
}
}
const formSubmit = () => {
sessionStorage.setItem('username', this.state.username);
Expand Down Expand Up @@ -96,6 +114,7 @@ export default class Login extends React.Component {
<input
name='otpCode'
type='number'
onChange={e => updateField('otp', e)}
ref={this.inputRefMfa}
/>
} />
Expand Down