All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.security.jpa.reactive.runtime.JpaReactiveIdentityProvider Maven / Gradle / Ivy

Go to download

Secure your applications with username/password stored in a database via Jakarta Persistence

There is a newer version: 3.17.0
Show newest version
package io.quarkus.security.jpa.reactive.runtime;

import java.util.function.Function;
import java.util.function.Predicate;

import jakarta.inject.Inject;
import jakarta.persistence.NonUniqueResultException;

import org.hibernate.FlushMode;
import org.hibernate.reactive.mutiny.Mutiny;
import org.jboss.logging.Logger;

import io.quarkus.security.AuthenticationFailedException;
import io.quarkus.security.identity.AuthenticationRequestContext;
import io.quarkus.security.identity.IdentityProvider;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.security.identity.request.UsernamePasswordAuthenticationRequest;
import io.smallrye.mutiny.Uni;

public abstract class JpaReactiveIdentityProvider implements IdentityProvider {

    private static final Logger LOG = Logger.getLogger(JpaReactiveIdentityProvider.class);

    @Inject
    Mutiny.SessionFactory sessionFactory;

    @Override
    public Class getRequestType() {
        return UsernamePasswordAuthenticationRequest.class;
    }

    @Override
    public Uni authenticate(UsernamePasswordAuthenticationRequest request,
            AuthenticationRequestContext context) {
        return sessionFactory.withSession(new Function>() {
            @Override
            public Uni apply(Mutiny.Session session) {
                session.setFlushMode(FlushMode.MANUAL);
                session.setDefaultReadOnly(true);
                return authenticate(session, request)
                        .onFailure(new Predicate() {
                            @Override
                            public boolean test(Throwable throwable) {
                                return throwable instanceof SecurityException || throwable instanceof NonUniqueResultException;
                            }
                        })
                        .transform(new Function() {
                            @Override
                            public Throwable apply(Throwable throwable) {
                                LOG.debug("Authentication failed", throwable);
                                return new AuthenticationFailedException();
                            }
                        });
            }
        });
    }

    public abstract Uni authenticate(Mutiny.Session session, UsernamePasswordAuthenticationRequest request);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy