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

[ENG-6082] Fix two-factor authentication after Python 3.12 upgrade #81

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,16 @@ protected final AuthenticationHandlerExecutionResult authenticateOsfPostgresInte
if (oneTimePassword == null) {
throw new OneTimePasswordRequiredException("2FA TOTP required for user [" + username + "]");
}
final long transformedOneTimePassword = Long.parseLong(oneTimePassword);
boolean checkPassed;
try {
final long transformedOneTimePassword = Long.parseLong(oneTimePassword);
if (!TotpUtils.checkCode(osfTotp.getTotpSecretBase32(), transformedOneTimePassword)) {
throw new InvalidOneTimePasswordException("Invalid 2FA TOTP for user [" + username + "] (Type 1)");
}
} catch (final Exception e) {
checkPassed = TotpUtils.checkCode(osfTotp.getTotpSecretBase32(), transformedOneTimePassword);
} catch (final Exception e){
throw new InvalidOneTimePasswordException("Invalid 2FA TOTP for user [" + username + "] (Type 2)");
}
if (!checkPassed) {
throw new InvalidOneTimePasswordException("Invalid 2FA TOTP for user [" + username + "] (Type 1)");
}
}

if (!osfUser.isTermsOfServiceAccepted() && !isTermsOfServiceChecked) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/io/cos/cas/osf/model/OsfTotp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.codec.binary.Base32;

Expand All @@ -28,6 +29,7 @@
@NoArgsConstructor
@Getter
@ToString
@Slf4j
public class OsfTotp extends AbstractOsfModel {

@OneToOne
Expand All @@ -50,8 +52,14 @@ private boolean isDeleted() {
}

public String getTotpSecretBase32() {
final byte[] bytes = DatatypeConverter.parseHexBinary(totpSecret);
return new Base32().encodeAsString(bytes);
try {
// Handle totpSecret generated before OSF Python 3.12 upgrade
final byte[] bytes = DatatypeConverter.parseHexBinary(totpSecret);
return new Base32().encodeAsString(bytes);
} catch (final IllegalArgumentException e) {
// Handle totpSecret generated after OSF Python 3.12 upgrade
return new Base32().encodeAsString(totpSecret.getBytes());
}
}

public boolean isActive() {
Expand Down
Loading