package com.airlock.iam.common.infrastructure.factory;

import com.airlock.iam.common.api.domain.model.authentication.Authentee;
import com.google.inject.Exposed;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.ProvisionException;
import jakarta.inject.Named;
import java.security.Principal;
import java.util.Optional;

public final class PrincipalModule extends PrivateModule {
   @Provides
   @Exposed
   Principal providePrincipal(@Named("authentee") Optional<Authentee> authentee) {
      if (authentee.isPresent()) {
         return () -> authentee.get().userIdentity().id().getValue();
      } else {
         throw new ProvisionException("Cannot provide Principal since no Authentee is present.");
      }
   }

   protected void configure() {
   }
}
