Skip to content

Commit

Permalink
feat(jans-keycloak-integration): enhancements to jans-keycloak-integr…
Browse files Browse the repository at this point in the history
…ation #8614

* fixes suggested by static analyser

Signed-off-by: Rolain Djeumen <uprightech@gmail.com>
  • Loading branch information
uprightech committed Jun 24, 2024
1 parent 64575c7 commit b571739
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ public static void main(String[] args) throws InterruptedException, ParserCreate
}
System.exit(-1);
return;
}catch(InterruptedException e) {
log.error("Application interrupted",e);
throw e;
}catch(Exception e) {
log.error("Fatal error starting application",e);
if(jobScheduler != null ) {
jobScheduler.stop();
}
System.exit(-1);
return;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,6 @@ private void addReleasedAttributesToManagedSamlClient(ManagedSamlClient client,

List<ProtocolMapper> protmappers = releasedattributes.stream().map((r)-> {
log.debug("Preparing to add released attribute {} to managed saml client with clientId {}",r.getName(),client.clientId());
/*return ProtocolMapper
.samlUserAttributeMapper(samlUserAttributeMapperId)
.name(generateKeycloakUniqueProtocolMapperName(r))
.userAttribute(r.getName())
.friendlyName(r.getDisplayName()!=null?r.getDisplayName():r.getName())
.attributeName(r.getSaml2Uri())
.attributeNameFormatUriReference()
.build(); */
return ProtocolMapper
.samlUserAttributeMapper(samlUserAttributeMapperId)
.name(generateKeycloakUniqueProtocolMapperName(r))
Expand All @@ -228,13 +220,6 @@ private void addReleasedAttributesToManagedSamlClient(ManagedSamlClient client,
private void updateManagedSamlClientProtocolMapper(ManagedSamlClient client, ProtocolMapper mapper, JansAttributeRepresentation releasedattribute) {

log.debug("Updating managed client released attribute. Client id: {} / Attribute name: {}",client.clientId(),releasedattribute.getName());
/*ProtocolMapper newmapper = ProtocolMapper
.samlUserAttributeMapper(mapper)
.userAttribute(releasedattribute.getName())
.friendlyName(releasedattribute.getDisplayName()!=null?releasedattribute.getDisplayName():releasedattribute.getName())
.attributeName(releasedattribute.getSaml2Uri())
.attributeNameFormatUriReference()
.build(); */
ProtocolMapper newmapper = ProtocolMapper
.samlUserAttributeMapper(mapper)
.jansAttributeName(releasedattribute.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
io.jans.kc.scheduler.job.Job job = (io.jans.kc.scheduler.job.Job) constructor.newInstance();
ExecutionContext effectivecontext = new QuartzExecutionContext(context.getMergedJobDataMap());
job.run(effectivecontext);
} catch(ReflectiveOperationException e) {
throw new JobExecutionException("Failed to run job " + jobname,e);
}catch(Exception e) {
throw new JobExecutionException("Failed to run job " + jobname,e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import org.apache.commons.lang3.StringUtils;

import org.keycloak.dom.saml.v2.assertion.AttributeStatementType;
import org.keycloak.dom.saml.v2.assertion.AttributeType;

import org.keycloak.saml.common.constants.JBossSAMLURIConstants;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.jans.kc.model;

import io.jans.kc.model.internal.JansPerson;
import io.jans.orm.model.base.CustomObjectAttribute;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -20,8 +19,6 @@
import org.keycloak.storage.ReadOnlyException;
import org.keycloak.storage.StorageId;

import org.jboss.logging.Logger;

public class JansUserModel implements UserModel {

private static final String INUM_ATTR_NAME = "inum";
Expand All @@ -31,18 +28,13 @@ public class JansUserModel implements UserModel {
private static final String GIVEN_NAME_ATTR_NAME = "givenName";
private static final String MAIL_ATTR_NAME = "mail";
private static final String EMAIL_VERIFIED_ATTR_NAME = "emailVerified";

private static final Logger log = Logger.getLogger(JansUserModel.class);
private static final String USER_READ_ONLY_EXCEPTION_MSG = "User is read-only for this update";

private final JansPerson jansPerson;
private final StorageId storageId;
private final ComponentModel storageProviderModel;
private final KeycloakSession session;

public JansUserModel(KeycloakSession session, ComponentModel storageProviderModel, JansPerson jansPerson) {

this.session = session;
this.storageProviderModel = storageProviderModel;
this.jansPerson = jansPerson;
String userId = jansPerson.customAttributeValue(INUM_ATTR_NAME);
this.storageId = new StorageId(storageProviderModel.getId(),userId);
Expand All @@ -63,7 +55,7 @@ public String getUsername() {
@Override
public void setUsername(String username) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -83,7 +75,7 @@ public Long getCreatedTimestamp() {
@Override
public void setCreatedTimestamp(Long timestamp) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -93,31 +85,28 @@ public boolean isEnabled() {
if(enabledStr == null) {
return false;
}
if("active".equals(enabledStr)) {
return true;
}
return false;
return "active".equals(enabledStr);
}

@Override
public void setEnabled(boolean enabled) {
throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
public void setSingleAttribute(String name, String value) {
throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
public void setAttribute(String name, List<String> value) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
public void removeAttribute(String name) {
throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}


Expand Down Expand Up @@ -170,12 +159,12 @@ public Stream<String> getRequiredActionsStream() {

@Override
public void addRequiredAction(String action) {
throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
public void removeRequiredAction(String action) {
throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -187,7 +176,7 @@ public String getFirstName() {
@Override
public void setFirstName(String firstName) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -199,7 +188,7 @@ public String getLastName() {
@Override
public void setLastName(String lastName) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -211,7 +200,7 @@ public String getEmail() {
@Override
public void setEmail(final String email) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -231,7 +220,7 @@ public boolean isEmailVerified() {
@Override
public void setEmailVerified(boolean verified) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -255,13 +244,13 @@ public long getGroupsCountByNameContaining(String search) {
@Override
public void joinGroup(GroupModel group) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
public void leaveGroup(GroupModel group) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -279,7 +268,7 @@ public String getFederationLink() {
@Override
public void setFederationLink(String link) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -291,7 +280,7 @@ public String getServiceAccountClientLink() {
@Override
public void setServiceAccountClientLink(String clientInternalId) {

throw new ReadOnlyException("User is read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand Down Expand Up @@ -321,7 +310,7 @@ public boolean hasRole(RoleModel role) {
@Override
public void grantRole(RoleModel role) {

throw new ReadOnlyException("User is in read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

@Override
Expand All @@ -333,7 +322,7 @@ public Stream<RoleModel> getRoleMappingsStream() {
@Override
public void deleteRoleMapping(RoleModel role) {

throw new ReadOnlyException("User is in read-only for this update");
throw new ReadOnlyException(USER_READ_ONLY_EXCEPTION_MSG);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.ArrayList;
import java.util.List;

import io.jans.model.JansAttribute;
import io.jans.orm.annotation.*;
import io.jans.orm.model.base.CustomObjectAttribute;

Expand All @@ -21,11 +20,6 @@ public class JansPerson implements Serializable {
@AttributesList(name="name",value="values",multiValued="multiValued")
private List<CustomObjectAttribute> customAttributes = new ArrayList<>();


public JansPerson() {

}

public String getDn() {

return this.dn;
Expand Down Expand Up @@ -72,7 +66,7 @@ public List<String> customAttributeValues(final String name) {
for(CustomObjectAttribute customAttribute : customAttributes) {
if(customAttribute.getName().equals(name)) {
List<Object> values = customAttribute.getValues();
if(values == null || values.size() == 0) {
if(values == null || values.isEmpty()) {
return new ArrayList<>();
}
return convertToString(values);
Expand All @@ -95,7 +89,7 @@ public String customAttributeValue(final String attributeName) {
for(CustomObjectAttribute customAttribute : customAttributes) {
if(customAttribute.getName().equals(attributeName)) {
List<Object> values = customAttribute.getValues();
if(values == null || values.size() == 0) {
if(values == null || values.isEmpty()) {
return null;
}
List<String> ret = convertToString(values);
Expand All @@ -113,8 +107,8 @@ private List<String> convertToString(List<Object> values) {

List<String> ret = new ArrayList<>();
for(Object val : values) {
if(val instanceof String) {
ret.add((String) val);
if(val instanceof String strval) {
ret.add((String) strval);
}else {
ret.add(val.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public OIDCAuthRequest() {
this.clientId = null;
this.state = null;
this.nonce = null;
this.scopes = new ArrayList<String>();
this.responseTypes = new ArrayList<String>();
this.scopes = new ArrayList<>();
this.responseTypes = new ArrayList<>();
this.redirectUri = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ public class OIDCMetaCacheKeys {
public static final String AUTHORIZATION_URL = "oidc.authorization.url";
public static final String TOKEN_URL = "oidc.token.url";
public static final String USERINFO_URL = "oidc.userinfo.url";

private OIDCMetaCacheKeys() {
//private constructor
}
}
Loading

0 comments on commit b571739

Please sign in to comment.