Skip to content

Commit 2d48235

Browse files
authored
Merge pull request #81 from cslzchen/feature/fix-totp-secret
[ENG-6082] Fix two-factor authentication after Python 3.12 upgrade
2 parents 5897a66 + 06b4100 commit 2d48235

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/main/java/io/cos/cas/osf/authentication/handler/support/OsfPostgresAuthenticationHandler.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,16 @@ protected final AuthenticationHandlerExecutionResult authenticateOsfPostgresInte
165165
if (oneTimePassword == null) {
166166
throw new OneTimePasswordRequiredException("2FA TOTP required for user [" + username + "]");
167167
}
168+
final long transformedOneTimePassword = Long.parseLong(oneTimePassword);
169+
boolean checkPassed;
168170
try {
169-
final long transformedOneTimePassword = Long.parseLong(oneTimePassword);
170-
if (!TotpUtils.checkCode(osfTotp.getTotpSecretBase32(), transformedOneTimePassword)) {
171-
throw new InvalidOneTimePasswordException("Invalid 2FA TOTP for user [" + username + "] (Type 1)");
172-
}
173-
} catch (final Exception e) {
171+
checkPassed = TotpUtils.checkCode(osfTotp.getTotpSecretBase32(), transformedOneTimePassword);
172+
} catch (final Exception e){
174173
throw new InvalidOneTimePasswordException("Invalid 2FA TOTP for user [" + username + "] (Type 2)");
175174
}
175+
if (!checkPassed) {
176+
throw new InvalidOneTimePasswordException("Invalid 2FA TOTP for user [" + username + "] (Type 1)");
177+
}
176178
}
177179

178180
if (!osfUser.isTermsOfServiceAccepted() && !isTermsOfServiceChecked) {

src/main/java/io/cos/cas/osf/model/OsfTotp.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.Getter;
55
import lombok.NoArgsConstructor;
66
import lombok.ToString;
7+
import lombok.extern.slf4j.Slf4j;
78

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

@@ -28,6 +29,7 @@
2829
@NoArgsConstructor
2930
@Getter
3031
@ToString
32+
@Slf4j
3133
public class OsfTotp extends AbstractOsfModel {
3234

3335
@OneToOne
@@ -50,8 +52,14 @@ private boolean isDeleted() {
5052
}
5153

5254
public String getTotpSecretBase32() {
53-
final byte[] bytes = DatatypeConverter.parseHexBinary(totpSecret);
54-
return new Base32().encodeAsString(bytes);
55+
try {
56+
// Handle totpSecret generated before OSF Python 3.12 upgrade
57+
final byte[] bytes = DatatypeConverter.parseHexBinary(totpSecret);
58+
return new Base32().encodeAsString(bytes);
59+
} catch (final IllegalArgumentException e) {
60+
// Handle totpSecret generated after OSF Python 3.12 upgrade
61+
return new Base32().encodeAsString(totpSecret.getBytes());
62+
}
5563
}
5664

5765
public boolean isActive() {

0 commit comments

Comments
 (0)