Added support for 'client_id' and backward compatibility with 'clientId'

claim #25802
This commit is contained in:
Luca Frosini 2024-05-02 15:47:10 +02:00
parent 0f1a5ad246
commit f18dd864da
3 changed files with 21 additions and 4 deletions

View File

@ -2,6 +2,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for Authorization Utils
## [v2.3.0-SNAPSHOT]
- Added support for 'client_id' and backward compatibility with 'clientId' claim #25802
## [v2.2.0]
- Switched to the new version of keycloak-client [#25295]

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<version>2.2.0</version>
<version>2.3.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -15,7 +15,8 @@ public class KeycloakUser extends AccessToken implements User {
*/
private static final long serialVersionUID = -7083648026885406300L;
public static final String CLIENT_ID_PROPERTY = "clientId";
public static final String CLIENT_ID_PROPERTY_OLD_KEY = "clientId";
public static final String CLIENT_ID_PROPERTY = "client_id";
protected Collection<String> roles;
protected Boolean application;
@ -26,10 +27,19 @@ public class KeycloakUser extends AccessToken implements User {
return getPreferredUsername();
}
@JsonIgnore
protected String getClientId() {
Object clientIdObj = getOtherClaims().get(CLIENT_ID_PROPERTY);
if(clientIdObj==null) {
clientIdObj = getOtherClaims().get(CLIENT_ID_PROPERTY_OLD_KEY);
}
return clientIdObj==null ? null : clientIdObj.toString();
}
@Override
public boolean isApplication() {
if(application==null) {
application = getOtherClaims().get(CLIENT_ID_PROPERTY)!=null;
application = getClientId()!=null;
}
return application;
}
@ -59,7 +69,10 @@ public class KeycloakUser extends AccessToken implements User {
@Override
public String getFullName(boolean nameSurname) {
if(isApplication()) {
String clientID = (String) getOtherClaims().getOrDefault("clientId", getUsername());
String clientID = getClientId();
if(clientID==null) {
clientID = getUsername();
}
return clientID;
}