opensingular / singular-keycloak-database-federation

Keycloak User Storage SPI for Relational Databases (Keycloak User Federation, supports postgresql, mysql, oracle and mysql)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Firstname, lastname and created date is not shown

mikkeljohnsen opened this issue · comments

I am using PostgreSQL and has this string:

select account_id AS id, username, email, firstname, lastname, created AS cpf, firstname || ' ' || lastname AS fullname from account where account_id::text = ?

The id, username and email is mapped fine. But firstname, lastname and created is not ?

It is not working with "first_name" and "last_name"

Yeah, sorry, I deleted my previous suggestion, my memory was wrong.

Could you add this.

diff -Nur singular-keycloak-database-federation-2.4.orig/src/main/java/org/opensingular/dbuserprovider/model/UserAdapter.java singular-keycloak-database-federation-2.4/src/main/java/org/opensingular/dbuserprovider/model/UserAdapter.java
--- singular-keycloak-database-federation-2.4.orig/src/main/java/org/opensingular/dbuserprovider/model/UserAdapter.java	2022-10-20 20:00:25.000000000 +0200
+++ singular-keycloak-database-federation-2.4/src/main/java/org/opensingular/dbuserprovider/model/UserAdapter.java	2022-11-23 15:16:40.609045120 +0100
@@ -21,11 +21,15 @@
 
     private final String keycloakId;
     private       String username;
+    private       String firstname;
+    private       String lastname;
 
     public UserAdapter(KeycloakSession session, RealmModel realm, ComponentModel model, Map<String, String> data, boolean allowDatabaseToOverwriteKeycloak) {
         super(session, realm, model);
         this.keycloakId = StorageId.keycloakId(model, data.get("id"));
         this.username = data.get("username");
+        this.firstname = data.get("firstname");
+        this.lastname = data.get("lastname");
         try {
           Map<String, List<String>> attributes = this.getAttributes();
           for (Entry<String, String> e : data.entrySet()) {
@@ -56,6 +60,16 @@
     }
 
     @Override
+    public String getFirstName() {
+        return firstname;
+    }
+
+    @Override
+    public String getLastName() {
+        return lastname;
+    }
+
+    @Override
     public void setUsername(String username) {
         this.username = username;
     }

Or should I make a PR ?

The current code already sets all the attributes on the user at line 40 (this.setAttribute(e.getKey(), newValues...). You should be able to get any of them using getAttribute.

Note: If these attributes change in your database after they have been read by Keycloak and you still want those changes to the DB reflected in Keycloak, you'll probably want to activate the option to "Allow DB Attributes to Overwrite Keycloak".

Yes I see all the attributes, but the firstname and lastname is not visible when you search for a user.

In my case I use the keycloak to send back to Jitsi meet, where it automatically sets the name of the user. Which is undefined, if this patch is not applied.

I do not see any reason not to apply this patch ?

Looking at the Keycloak source code, there are already methods named getFirstName and getLastName, which reads the from the attributes set. Looking at the attribute name for the official Keycloak First Name, it's possible that my first advice was close to the answer if the method to get attributes is case-sensitive (likely).
Then according to the following code, the attributes may need to be uppercased?
public static String FIRST_NAME_ATTRIBUTE = "FIRST_NAME";
public static String LAST_NAME_ATTRIBUTE = "LAST_NAME";
AbstractUserAdapterFederatedStorage.java

In any case, overriding these methods with a value set at the level that you propose may always override the Keycloak value (vs the default behaviour which is to keep the existing Keycloak values, unless you have the option "Allow DB Attributes to Overwrite Keycloak" activated). I'm not sure how it would behave with caching either.
If hyphen+uppercasing does not work for the column names returned by the DB, you could always do a PR a see what the plugin owner thinks.