2023-10-20

What is the best way to create user config at first user login?

In my quarkus application, I want to have some data inserted in my database for each user.

Database uses neo4j. As a consequence, I would like to have some code in my application which creates the various datas and send them in database.

Currently, I use the RolesAugmentor, as described in Security Tips and Tricks. Unfortunatly, as it is invoked for each request, we have multiple nodes generated for each new user. How can I have data inserted only once for each user ?

EDIT 1 More clearly, I have a RolesAugmentor class containing the following code

@ApplicationScoped
public class RolesAugmentor implements SecurityIdentityAugmentor {

    @Override
    public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context) {
        return Uni.createFrom().item(build(identity));
        // Do 'return context.runBlocking(build(identity));'
        // if a blocking call is required to customize the identity
    }
    private Supplier<SecurityIdentity> build(SecurityIdentity identity) {
        if (identity.isAnonymous()) {
            return () -> identity;
        } else {
            // create a new builder and copy principal, attributes, credentials and roles
            // from the original identity
            QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity);
    
            JWTCallerPrincipal caller = (JWTCallerPrincipal) identity.getPrincipal();
            String email = caller.getClaim("email");
            if (!isUserAlreadyInDB(email)) {
                synchronized(RolesAugmentor.class) {
                    if (!isUserAlreadyInDB(email)) {
                        String name = caller.getClaim("given_name");
                        String famillyName = caller.getClaim("family_name");
                        addUserToDB(email, name, famillyName);
                    }
                }
            }
    
            // add custom role source here
            builder.addRoles(extractUserRoles(email));
    
            return builder::build;
        }
    }

And I'm 99% sure it's not the good place to add user creation code. Am I right? What is the correct way?



No comments:

Post a Comment