Skip to content

Commit

Permalink
Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
shanggeeth committed Jan 3, 2024
1 parent ba7d9a6 commit f16e12b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.io.Serializable;

import javax.script.ScriptEngine;

/**
* Serializable javascript function.
* This is required since the next javascript execution may happen on a different node than current node, when user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,43 @@ public GraalSerializableJsFunction(String source, boolean isFunction) {
this.isPolyglotFunction = true;
}

public String getSource() {

return source;
}

public void setSource(String source) {

this.source = source;
}

@Override
public boolean isFunction() {

return isHostFunction;
}

@Override
public void setFunction(boolean function) {

}

public Object apply(Context polyglotContext, Object... params) {

if (isPolyglotFunction) {
try {
polyglotContext.eval(Source.newBuilder("js", " var curFunc = " + getSource(), "src.js").build());
return polyglotContext.getBindings("js").getMember("curFunc").execute(params);
} catch (IOException e) {
log.error("Error when building the from function source", e);
} catch (PolyglotException e) {
log.error("Error when executing function", e);
}
}

return null;
}

/**
* This will return the converted NashornSerializableJsFunction if the given ScriptObjectMirror is a function.
*
Expand Down Expand Up @@ -89,43 +126,6 @@ public static GraalSerializableJsFunction toSerializableForm(Object functionObje

}

public Object apply(Context polyglotContext, Object... params) {

if (isPolyglotFunction) {
try {
polyglotContext.eval(Source.newBuilder("js", " var curFunc = " + getSource(), "src.js").build());
return polyglotContext.getBindings("js").getMember("curFunc").execute(params);
} catch (IOException e) {
log.error("Error when building the from function source", e);
} catch (PolyglotException e) {
log.error("Error when executing function", e);
}
}

return null;
}

public String getSource() {

return source;
}

@Override
public boolean isFunction() {

return isHostFunction;
}

@Override
public void setFunction(boolean function) {

}

public void setSource(String source) {

this.source = source;
}

public String getName() {

return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wso2.carbon.identity.application.authentication.framework.config.model.ExternalIdPConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
Expand All @@ -41,9 +42,10 @@
import org.wso2.carbon.idp.mgt.IdentityProviderManager;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.UserStoreClientException;
import org.wso2.carbon.user.core.claim.Claim;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.util.UserCoreUtil;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -53,9 +55,9 @@
public class JsClaims extends AbstractJSContextMemberObject {

protected static final Log LOG = LogFactory.getLog(JsClaims.class);
private String idp;
protected String idp;
protected boolean isRemoteClaimRequest;
private int step;
protected int step;
protected transient AuthenticatedUser authenticatedUser;

/**
Expand All @@ -78,6 +80,10 @@ public JsClaims(int step, String idp, boolean isRemoteClaimRequest) {
this.step = step;
}

public JsClaims() {

}

@Override
public void initializeContext(AuthenticationContext context) {

Expand Down Expand Up @@ -128,8 +134,8 @@ private StepConfig getCurrentSubjectIdentifierStep() {
}

/**
* Constructor to get user who is not directly from a authentication step. Eg. Associated user of authenticated
* federated user in a authentication step.
* Constructor to get user who is not directly from an authentication step. E.g. Associated user of authenticated
* federated user in an authentication step.
*
* @param authenticatedUser Authenticated user
* @param isRemoteClaimRequest Whether the request is for remote claim (false for local claim request)
Expand Down Expand Up @@ -193,7 +199,7 @@ protected void setLocalClaim(String claimUri, String claimValue) {
* @param localClaimURI Local claim URI
* @param claimValue Value to be set
*/
private void setLocalMappedClaim(String localClaimURI, String claimValue) {
protected void setLocalMappedClaim(String localClaimURI, String claimValue) {

Map<ClaimMapping, String> idpAttributesMap = authenticatedUser.getUserAttributes();
Map<String, String> remoteMapping = FrameworkUtils.getClaimMappings(idpAttributesMap, false);
Expand All @@ -213,17 +219,22 @@ protected void setLocalUserClaim(String claimUri, Object claimValue) {

int usersTenantId = IdentityTenantUtil.getTenantId(authenticatedUser.getTenantDomain());
RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
String usernameWithDomain =
UserCoreUtil.addDomainToName(authenticatedUser.getUserName(), authenticatedUser.getUserStoreDomain());
try {
UserRealm userRealm = realmService.getTenantUserRealm(usersTenantId);
Map<String, String> claimUriMap = new HashMap<>();
claimUriMap.put(claimUri, String.valueOf(claimValue));
userRealm.getUserStoreManager().setUserClaimValues(usernameWithDomain, claimUriMap, null);
((AbstractUserStoreManager) userRealm.getUserStoreManager())
.setUserClaimValuesWithID(authenticatedUser.getUserId(), claimUriMap, null);
} catch (UserStoreClientException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Error when setting claim : %s of user: %s to value: %s. Error Message: %s",
claimUri, authenticatedUser, claimValue, e.getMessage()));
}
} catch (UserStoreException e) {
LOG.error(
String.format("Error when setting claim : %s of user: %s to value: %s", claimUri, authenticatedUser,
claimValue), e);
LOG.error(String.format("Error when setting claim : %s of user: %s to value: %s", claimUri,
authenticatedUser, claimValue), e);
} catch (UserIdNotFoundException e) {
LOG.error("User id is not available for the user: " + authenticatedUser.getLoggableMaskedUserId(), e);
}
}

Expand All @@ -240,7 +251,7 @@ private String getRemoteClaimMappedToLocalClaim(String localClaim, Map<String, S
Map<String, String> localToIdpClaimMapping;
String tenantDomain = getContext().getTenantDomain();
try {
// Check if the IDP use an standard dialect (like oidc), If it does, dialect claim mapping are
// Check if the IDP use a standard dialect (like oidc), If it does, dialect claim mapping are
// prioritized over IdP claim mapping
ApplicationAuthenticator authenticator =
getContext().getSequenceConfig().getStepMap().get(step).getAuthenticatedAutenticator()
Expand Down Expand Up @@ -297,7 +308,7 @@ protected boolean hasLocalClaim(String claimUri) {
* Check if the user has a federated claim with given name.
*
* @param claimUri Federated claim URI
* @return <code>true</code> if the IdP is federated and it has a claim for user with given URI.
* @return <code>true</code> if the IdP is federated, and it has a claim for user with given URI.
* <code>false</code> otherwise
*/
protected boolean hasFederatedClaim(String claimUri) {
Expand Down Expand Up @@ -390,19 +401,26 @@ private String getLocalMappedClaim(String claimUri) {
return null;
}

/**
* Get the local user claim value specified by the Claim URI.
*
* @param claimUri Local claim URI
* @return Claim value of the given claim URI for the local user if available. Null Otherwise.
*/
protected String getLocalUserClaim(String claimUri) {

int usersTenantId = IdentityTenantUtil.getTenantId(authenticatedUser.getTenantDomain());
String usernameWithDomain =
UserCoreUtil.addDomainToName(authenticatedUser.getUserName(), authenticatedUser.getUserStoreDomain());
RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
try {
UserRealm userRealm = realmService.getTenantUserRealm(usersTenantId);
Map<String, String> claimValues = userRealm.getUserStoreManager()
.getUserClaimValues(usernameWithDomain, new String[]{claimUri}, null);
Map<String, String> claimValues =
((AbstractUserStoreManager) userRealm.getUserStoreManager())
.getUserClaimValuesWithID(authenticatedUser.getUserId(), new String[] {claimUri}, null);
return claimValues.get(claimUri);
} catch (UserStoreException e) {
LOG.error(String.format("Error when getting claim : %s of user: %s", claimUri, authenticatedUser), e);
} catch (UserIdNotFoundException e) {
LOG.error("User id is not available for the user: " + authenticatedUser.getLoggableMaskedUserId(), e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public class JsGraalClaims extends JsClaims implements ProxyObject {

private final transient Map<String, String> localClaimUriToValueReadCache = new HashMap<>();
private static final Log LOG = LogFactory.getLog(JsNashornClaims.class);
private String idp;
private boolean isRemoteClaimRequest;
private int step;
protected transient AuthenticatedUser authenticatedUser;

public JsGraalClaims(AuthenticationContext context, int step, String idp, boolean isRemoteClaimRequest) {

Expand Down
Loading

0 comments on commit f16e12b

Please sign in to comment.