Skip to content

Commit

Permalink
add case identity_provider_authnAuthorities details exist
Browse files Browse the repository at this point in the history
  • Loading branch information
cgeorgilakis-grnet committed Jul 8, 2024
1 parent a2500fd commit e1d447d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.keycloak.metrics.representations;

public class AuthnAuthorityRepresentation {

private String id;
private String name;

public AuthnAuthorityRepresentation(){
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
14 changes: 11 additions & 3 deletions src/main/java/org/keycloak/metrics/representations/MetricsDto.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package org.keycloak.metrics.representations;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.LinkedList;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.keycloak.events.Event;
import org.keycloak.metrics.utils.MetricsUtils;
import org.keycloak.models.ClientModel;
import org.keycloak.models.IdentityProviderModel;
import org.keycloak.models.RealmModel;
import org.keycloak.util.JsonSerialization;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class MetricsDto {
Expand Down Expand Up @@ -42,7 +46,7 @@ public MetricsDto() {

}

public MetricsDto(Event event, RealmModel realm) {
public MetricsDto(Event event, RealmModel realm) throws IOException {
this.date = LocalDateTime.ofInstant(Instant.ofEpochMilli(event.getTime()), ZoneId.systemDefault());
this.tenenvId = realm.getAttribute(MetricsUtils.TENENV_ID);
this.source = realm.getAttribute(MetricsUtils.SOURCE);
Expand Down Expand Up @@ -78,12 +82,16 @@ public MetricsDto(Event event, RealmModel realm) {
}
}

private void setLogin(Event event, RealmModel realm) {
private void setLogin(Event event, RealmModel realm) throws IOException {
this.ipAddress = event.getIpAddress();
if (event.getDetails() == null)
event.setDetails(new HashMap<>());
this.voPersonId = event.getDetails().get(MetricsUtils.VO_PERSON_ID);
if (event.getDetails().get(MetricsUtils.AUTHN_AUTHORITY) != null) {
if (event.getDetails().get(MetricsUtils.IDENTITY_PROVIDER_AUTHN_AUTHORITIES) != null) {
AuthnAuthorityRepresentation lastAuthnAuthority = JsonSerialization.readValue(event.getDetails().get(MetricsUtils.IDENTITY_PROVIDER_AUTHN_AUTHORITIES),new TypeReference<LinkedList<AuthnAuthorityRepresentation>>(){}).getLast();
this.entityId = lastAuthnAuthority.getId();
this.idpName = lastAuthnAuthority.getName();
} else if (event.getDetails().get(MetricsUtils.AUTHN_AUTHORITY) != null) {
//authnAuthority of IdP in user session note (name = identity_provider_id)
this.entityId = event.getDetails().get(MetricsUtils.AUTHN_AUTHORITY);
this.idpName = event.getDetails().get(MetricsUtils.IDP_NAME);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/keycloak/metrics/utils/MetricsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class MetricsUtils {

public static final String AUTHN_AUTHORITY = "authnAuthority";
public static final String IDENTITY_PROVIDER_AUTHN_AUTHORITIES = "identity_provider_authnAuthorities";
public static final String IDP_NAME = "idpName";
public static final String VO_PERSON_ID = "voPersonID";
public static final String IDP_ALIAS = "identity_provider";
Expand Down

0 comments on commit e1d447d

Please sign in to comment.