You are viewing the documentation for Blueriq 17. Documentation for other versions is available in our documentation directory.

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Blueriq provides AuthenticationMapper interface that should be implementation when a CustomBean authentication provider is used and the authentication provider returns a custom Authentication implementation. Default implementations are already provided by Blueriq for the following Authentication implementations:

  • UsernamePasswordAuthenticationToken
  • AnonymousAuthenticationToken


To implementation a custom AuthenticationMapper  the following has to be done:

  • Add the blueriq-component-api to the project dependencies.
pom.xml
<project ...>
  ...  
  <dependencies>
    ...
    <dependency>
	  <groupId>com.blueriq</groupId>
	  <artifactId>blueriq-component-api</artifactId>
	  <version>${blueriq.version}</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>
  • Define the custom implementation like below:
CustomExternalFlowStore.java
package ...;

import com.blueriq.component.api.externalflow.data.mapping.AuthenticationMapper;
// other imports ...

@Component
public class CustomAuthenticationMapper implements AuthenticationMapper {

  @Override
  public String authenticationType() {
    // add implementation here
    return null;
  }

  @Override
  public boolean supportsContract(ContractVersion contractVersion) {
    // add implementation here
    return false;
  }

  @Override
  public boolean canMap(Authentication authentication) {
    // add implementation here
    return false;
  }

  @Override
  public void fillModel(ObjectModelCreation objectModel, Authentication authentication) {
    // add implementation here
  }

  @Override
  public Authentication toAuthentication(ObjectModelRetrieval objectModel) {
    // add implementation here
    return null;
  }
}

Example CustomBlueriqAuthentication AuthenticationMapper

Here is an example implementation of the authentication mapper for a custom BlueriqAuthentication implementation. This implementation has an additional token field.

The CustomBlueriqAuthentication

CustomBlueriqAuthentcation
public class CustomBlueriqAuthentication implements BlueriqAuthentication {

  private final String username;
  private final String token;
  private final List<String> teams;
  private final List<String> roles;
  private final List<? extends GrantedAuthority> authorities;
  private final Map<String, List<String>> claims;
  private final boolean authenticated;

  public CustomBlueriqAuthentication(String username, String token, List<String> teams, List<String> roles,
      Map<String, List<String>> claims, boolean authenticated) {
    this.username = username;
    this.token = token;
    this.teams = teams;
    this.roles = roles;
    this.authorities = roles.stream().map(SimpleGrantedAuthority::new).toList();
    this.claims = claims;
    this.authenticated = authenticated;
  }

  @Override
  public List<String> getTeams() {
    return teams;
  }

  @Override
  public List<String> getRoles() {
    return roles;
  }

  @Override
  public boolean isAnonymous() {
    return false;
  }

  @Override
  public boolean isAutomatic() {
    return false;
  }

  @Override
  public Set<String> getClaimNames() {
    return claims.keySet();
  }

  @Override
  public List<String> getClaim(String name) {
    return claims.get(name);
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return authorities;
  }

  @Override
  public Object getCredentials() {
    return null;
  }

  @Override
  public Object getDetails() {
    return null;
  }

  @Override
  public Object getPrincipal() {
    return username;
  }

  @Override
  public boolean isAuthenticated() {
    return authenticated;
  }

  @Override
  public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
    throw new IllegalArgumentException("Cannot set authenticated after construction");
  }

  @Override
  public String getName() {
    return username;
  }

  public String getToken() {
    return token;
  }
}

Adding the dependencies

The following dependencies are required in the pom.xml

pom.xml
<project ...>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>com.blueriq</groupId>
      <artifactId>blueriq-component-api</artifactId>
      <version>${blueriq.version}</version>
    </dependency>
    ...
  <dependencies>
  ...
</project>

Implementing the AuthenticationMapper Interface

Please make sure that CustomBlueriqAuthenticationMapper component is scanned by Spring.

HazelcastExternalFlowStore.java
package ...;

import com.blueriq.component.api.store.externalflow.IExternalFlowStore;
// other imports ...

@Component
public class HazelcastExternalFlowStore implements IExternalFlowStore {

  private final IMap<String, byte[]> map;

  public HazelcastExternalFlowStore() {
    ClientConfig config = new ClientConfig();
    // configure Hazelcast properties here
    HazelcastInstance hazelcastInstance
      = HazelcastClient.newHazelcastClient(config);
    map = hazelcastInstance.getMap("blueriq");
  }

  @Override
  public void delete(String key) 
    throws ExternalFlowStoreException, IllegalArgumentException {
    if (isInvalid(key)) {
      throw new IllegalArgumentException("Invalid key.");
    }

    try {
      map.delete(key);
    } catch (Exception e) {
      throw new ExternalFlowStoreException("Something went wrong", e);
    }
  }

  @Override
  public ObjectModelRetrieval T get(String key)
      throws ExternalFlowStoreException, IllegalArgumentException {
    if (isInvalid(key)) {
      throw new IllegalArgumentException("Invalid key.");
    }

    try {
      byte[] storedValue = map.get(key);
      if (storedValue == null) {
        return null;
      }
      return new ObjectModel(storedValue);
    } catch (Exception e) {
      throw new ExternalFlowStoreException("Something went wrong", e);
    }
  }

  @Override
  public boolean hasKey(String key) 
    throws ExternalFlowStoreException, IllegalArgumentException {
    if (isInvalid(key)) {
      throw new IllegalArgumentException("Invalid key.");
    }

    try {
      return map.get(key) != null;
    } catch (Exception e) {
      throw new ExternalFlowStoreException("Something went wrong", e);
    }
  }

  @Override
  public void set(String key, ObjectModelStorage value) 
    throws ExternalFlowStoreException, IllegalArgumentException {
    if (isInvalid(key)) {
      throw new IllegalArgumentException("Invalid key.");
    }

	if (model == null) {
      throw new IllegalArgumentException("external flow model must not be null");
    }

    try {
      map.set(key, value.toBytes(), 1, TimeUnit.MINUTES);
    } catch (Exception e) {
      throw new ExternalFlowStoreException("Something went wrong", e);
    }

  }

  private boolean isInvalid(String key) {
    // define what an invalid key means
    return false;
  }

}
  • No labels