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

Always execute a JPA password action #37244

Merged
merged 1 commit into from
Nov 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ public static void buildIdentity(Index index, JpaSecurityDefinition jpaSecurityD
PanacheEntityPredicateBuildItem panacheEntityPredicate, FieldDescriptor passwordProviderField,
MethodCreator outerMethod, ResultHandle userVar, BytecodeCreator innerMethod) {
// if(user == null) throw new AuthenticationFailedException();

PasswordType passwordType = passwordTypeValue != null ? PasswordType.valueOf(passwordTypeValue.asEnum())
: PasswordType.MCF;

try (BytecodeCreator trueBranch = innerMethod.ifNull(userVar).trueBranch()) {

ResultHandle exceptionInstance = trueBranch
.newInstance(MethodDescriptor.ofConstructor(AuthenticationFailedException.class));
trueBranch.invokeStaticMethod(passwordActionMethod(), trueBranch.load(passwordType));
trueBranch.throwException(exceptionInstance);
}

// :pass = user.pass | user.getPass()
ResultHandle pass = jpaSecurityDefinition.password.readValue(innerMethod, userVar);

PasswordType passwordType = passwordTypeValue != null ? PasswordType.valueOf(passwordTypeValue.asEnum())
: PasswordType.MCF;

if (passwordType == PasswordType.CUSTOM && passwordProviderValue == null) {
throw new RuntimeException("Missing password provider for password type: " + passwordType);
}
Expand Down Expand Up @@ -245,4 +248,8 @@ private static MethodDescriptor getUtilMethod(String passwordProviderMethod) {
return MethodDescriptor.ofMethod(JpaIdentityProviderUtil.class, passwordProviderMethod,
org.wildfly.security.password.Password.class, String.class);
}

private static MethodDescriptor passwordActionMethod() {
return MethodDescriptor.ofMethod(JpaIdentityProviderUtil.class, "passwordAction", void.class, PasswordType.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.security.spec.InvalidKeySpecException;
import java.util.List;
import java.util.UUID;

import org.wildfly.security.credential.PasswordCredential;
import org.wildfly.security.evidence.PasswordGuessEvidence;
Expand All @@ -10,9 +11,11 @@
import org.wildfly.security.password.util.ModularCrypt;
import org.wildfly.security.provider.util.ProviderUtil;

import io.quarkus.elytron.security.common.BcryptUtil;
import io.quarkus.security.AuthenticationFailedException;
import io.quarkus.security.identity.request.TrustedAuthenticationRequest;
import io.quarkus.security.identity.request.UsernamePasswordAuthenticationRequest;
import io.quarkus.security.jpa.PasswordType;
import io.quarkus.security.runtime.QuarkusPrincipal;
import io.quarkus.security.runtime.QuarkusSecurityIdentity;

Expand Down Expand Up @@ -70,4 +73,13 @@ public static Password getMcfPassword(String pass) {
throw new RuntimeException(e);
}
}

public static void passwordAction(PasswordType type) {
String uuid = UUID.randomUUID().toString();
if (type == PasswordType.CLEAR) {
ClearPassword.createRaw(ClearPassword.ALGORITHM_CLEAR, uuid.toCharArray());
} else {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a custom type but I think a BcryptUtil action is enough to cover it

BcryptUtil.bcryptHash(uuid);
}
}
}