Skip to content

Commit

Permalink
Added: DataverseAuthenticatedUser for populating extra information in…
Browse files Browse the repository at this point in the history
… DataverseUserAdapter
  • Loading branch information
GPortas committed Jan 29, 2025
1 parent 68fbbb0 commit db34307
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.harvard.iq.keycloak.auth.spi;

import jakarta.persistence.*;

@NamedQueries({
@NamedQuery(name = "DataverseAuthenticatedUser.findByEmail",
query = "select au from DataverseAuthenticatedUser au WHERE LOWER(au.email)=LOWER(:email)"),
@NamedQuery(name = "DataverseAuthenticatedUser.findByIdentifier",
query = "select au from DataverseAuthenticatedUser au WHERE LOWER(au.userIdentifier)=LOWER(:identifier)"),
})
@Entity
@Table(name = "authenticateduser")
public class DataverseAuthenticatedUser {
@Id
private String id;
private String email;
private String lastName;
private String firstName;
private String userIdentifier;

public String getEmail() {
return email;
}

public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

public String getUserIdentifier() {
return userIdentifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.harvard.iq.keycloak.auth.spi;

import jakarta.persistence.*;

@NamedQueries({
@NamedQuery(name = "DataverseUser.findByUsername",
query = "SELECT u FROM DataverseBuiltinUser u WHERE LOWER(u.username)=LOWER(:username)")
})
@Entity
@Table(name = "builtinuser")
public class DataverseBuiltinUser {
@Id
private String id;
private String username;

public String getId() {
return id;
}

public String getUsername() {
return username;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,39 @@

public class DataverseUserAdapter extends AbstractUserAdapterFederatedStorage {

protected DataverseUser user;
protected DataverseBuiltinUser builtinUser;
protected DataverseAuthenticatedUser authenticatedUser;
protected String keycloakId;

public DataverseUserAdapter(KeycloakSession session, RealmModel realm, ComponentModel model, DataverseUser user) {
public DataverseUserAdapter(KeycloakSession session, RealmModel realm, ComponentModel model, DataverseBuiltinUser builtinUser, DataverseAuthenticatedUser authenticatedUser) {
super(session, realm, model);
this.user = user;
keycloakId = StorageId.keycloakId(model, user.getId());
this.builtinUser = builtinUser;
this.authenticatedUser = authenticatedUser;
keycloakId = StorageId.keycloakId(model, builtinUser.getId());
}

public String getEncryptedPassword() {
return user.getEncryptedPassword();
@Override
public void setUsername(String s) {
}

@Override
public String getUsername() {
return user.getUsername();
return builtinUser.getUsername();
}

@Override
public void setUsername(String s) {
public String getEmail() {
return authenticatedUser.getEmail();
}

@Override
public String getFirstName() {
return authenticatedUser.getFirstName();
}

@Override
public String getLastName() {
return authenticatedUser.getLastName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,34 @@ public class DataverseUserStorageProvider implements
@Override
public UserModel getUserById(RealmModel realmModel, String id) {
logger.info("getUserById: " + id);
DataverseUser user = em.find(DataverseUser.class, id);
DataverseBuiltinUser user = em.find(DataverseBuiltinUser.class, id);
if (user == null) {
logger.info("could not find user by id: " + id);
logger.info("could not find builtin user by id: " + id);
return null;
}
return new DataverseUserAdapter(session, realmModel, model, user);
String username = user.getUsername();
DataverseAuthenticatedUser authenticatedUser = getAuthenticatedUserByUsername(username);
if (authenticatedUser == null) {
return null;
}
return new DataverseUserAdapter(session, realmModel, model, user, authenticatedUser);
}

@Override
public UserModel getUserByUsername(RealmModel realmModel, String username) {
logger.info("getUserByUsername: " + username);
TypedQuery<DataverseUser> query = em.createNamedQuery("DataverseUser.findByUsername", DataverseUser.class);
TypedQuery<DataverseBuiltinUser> query = em.createNamedQuery("DataverseUser.findByUsername", DataverseBuiltinUser.class);
query.setParameter("username", username);
List<DataverseUser> result = query.getResultList();
if (result.isEmpty()) {
List<DataverseBuiltinUser> builtinUsersResult = query.getResultList();
if (builtinUsersResult.isEmpty()) {
logger.info("User not found: " + username);
return null;
}
logger.info("User found: " + result.get(0).getUsername());
return new DataverseUserAdapter(session, realmModel, model, result.get(0));
DataverseAuthenticatedUser authenticatedUser = getAuthenticatedUserByUsername(username);
if (authenticatedUser == null) {
return null;
}
return new DataverseUserAdapter(session, realmModel, model, builtinUsersResult.get(0), authenticatedUser);
}

@Override
Expand Down Expand Up @@ -101,9 +109,9 @@ public Stream<UserModel> searchForUserStream(RealmModel realm, Map<String, Strin
String search = params.get(UserModel.SEARCH);
logger.info("searchForUserStream: " + search);
String lower = search != null ? search.toLowerCase() : "";
TypedQuery<DataverseUser> query = em.createNamedQuery("DataverseUser.findByUsername", DataverseUser.class);
TypedQuery<DataverseBuiltinUser> query = em.createNamedQuery("DataverseUser.findByUsername", DataverseBuiltinUser.class);
query.setParameter("username", lower);
return query.getResultStream().map(entity -> new DataverseUserAdapter(session, realm, model, entity));
return query.getResultStream().map(entity -> new DataverseUserAdapter(session, realm, model, entity, getAuthenticatedUserByUsername(entity.getUsername())));
}

@Override
Expand All @@ -115,4 +123,14 @@ public Stream<UserModel> getGroupMembersStream(RealmModel realmModel, GroupModel
public Stream<UserModel> searchForUserByUserAttributeStream(RealmModel realmModel, String s, String s1) {
return Stream.empty();
}

private DataverseAuthenticatedUser getAuthenticatedUserByUsername(String username) {
TypedQuery<DataverseAuthenticatedUser> query = em.createNamedQuery("DataverseAuthenticatedUser.findByIdentifier", DataverseAuthenticatedUser.class);
query.setParameter("identifier", username);
DataverseAuthenticatedUser singleResult = query.getSingleResult();
if (singleResult == null) {
logger.info("Could not find authenticated user by username: " + username);
}
return singleResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="user-store" transaction-type="JTA">
<class>edu.harvard.iq.keycloak.auth.spi.DataverseUser</class>
<class>edu.harvard.iq.keycloak.auth.spi.DataverseBuiltinUser</class>
<class>edu.harvard.iq.keycloak.auth.spi.DataverseAuthenticatedUser</class>
<properties>
<!-- Set the Hibernate dialect for PostgreSQL -->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
Expand Down

0 comments on commit db34307

Please sign in to comment.