Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new Identity Context to the carbon core #6325

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.core.context;

import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.identity.core.context.model.Actor;
import org.wso2.carbon.identity.core.context.model.ApplicationActor;
import org.wso2.carbon.identity.core.context.model.Flow;
import org.wso2.carbon.identity.core.context.model.UserActor;
import org.wso2.carbon.identity.core.internal.IdentityContextDataHolder;
import org.wso2.carbon.utils.CarbonUtils;

/**
* This class is used to store the identity context information of the current thread.
*/
public class IdentityContext extends CarbonContext {

private final IdentityContextDataHolder identityContextDataHolder;

/**
* Creates a IdentityContext using the given IdentityContext data holder as its backing instance.
*
* @param identityContextDataHolder the IdentityContext data holder that backs this CarbonContext object.
*/
protected IdentityContext(IdentityContextDataHolder identityContextDataHolder) {

super();
this.identityContextDataHolder = identityContextDataHolder;
}

public static IdentityContext getThreadLocalIdentityContext() {

return new IdentityContext(IdentityContextDataHolder.getThreadLocalIdentityContextHolder());
}

/**
* Set the flow of the request.
*
* @param flow flow of the request.
*/
public void setFlow(Flow flow) {

if (identityContextDataHolder.getFlow() != null) {
throw new IllegalStateException("Flow is already set in the IdentityContext.");
}
identityContextDataHolder.setFlow(flow);
}

/**
* Get the flow id of the request.
*
* @return Flow of the request.
*/
public Flow getFlow() {

return identityContextDataHolder.getFlow();
}

/**
* Set the actor of the request.
*
* @param actor actor of the request.
*/
public void setActor(Actor actor) {

if (identityContextDataHolder.getActor() != null) {
throw new IllegalStateException("Actor is already set in the IdentityContext.");
}
identityContextDataHolder.setActor(actor);
}

/**
* Get the actor of the request.
*
* @return Actor of the request.
*/
public Actor getActor() {

return identityContextDataHolder.getActor();
}

/**
* Get the User actor of the request.
*
* @return UserActor of the request.
*/
public UserActor getUserActor() {

if (isUserActor()) {
return (UserActor) identityContextDataHolder.getActor();
}
return null;
}

/**
* Check whether the actor is a User actor.
*
* @return true if the actor is a User actor.
*/
public boolean isUserActor() {

return identityContextDataHolder.getActor() instanceof UserActor;
}

/**
* Get the Application actor of the request.
*
* @return ApplicationActor of the request.
*/
public ApplicationActor getApplicationActor() {

if (isApplicationActor()) {
return (ApplicationActor) identityContextDataHolder.getActor();
}
return null;
}

/**
* Check whether the actor is an Application actor.
*
* @return true if the actor is an Application actor.
*/
public boolean isApplicationActor() {

return identityContextDataHolder.getActor() instanceof ApplicationActor;
}

public static void destroyCurrentContext() {

CarbonUtils.checkSecurity();
IdentityContextDataHolder.destroyCurrentIdentityContextDataHolder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.core.context.model;

/**
* Interface for accessing/authenticating Entity.
* This interface is implemented by the entities that initiate a flow.
*/
public interface Actor {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.core.context.model;

/**
* Actor class for Application.
* This class holds the application actor details for a given flow.
*/
public class ApplicationActor implements Actor {

/**
* Enum for authentication types.
* Specifies the type of authentication used by the application.
*/
public enum AuthType {
OAUTH2
}

private final String applicationId;
private final String applicationName;
private final AuthType authenticationType;
private final String entityId;

private ApplicationActor(Builder builder) {

this.applicationId = builder.applicationId;
this.applicationName = builder.applicationName;
this.authenticationType = builder.authenticationType;
this.entityId = builder.entityId;
}

public String getApplicationId() {

return applicationId;
}

public String getApplicationName() {

return applicationName;
}

/**
* Builder for the ApplicationEntity.
*/
public static class Builder {

private String applicationId;
private String applicationName;
private AuthType authenticationType;
private String entityId;

public Builder() {

}

public Builder(ApplicationActor applicationActor) {

this.applicationId = applicationActor.applicationId;
this.applicationName = applicationActor.applicationName;
this.authenticationType = applicationActor.authenticationType;
this.entityId = applicationActor.entityId;
}

public Builder applicationId(String applicationId) {

this.applicationId = applicationId;
return this;
}

public Builder applicationName(String applicationName) {

this.applicationName = applicationName;
return this;
}

public Builder authenticationType(AuthType authenticationType) {

this.authenticationType = authenticationType;
return this;
}

public Builder entityId(String entityId) {

this.entityId = entityId;
return this;
}

public ApplicationActor build() {

if (entityId != null && authenticationType == null) {
throw new IllegalArgumentException("Authentication type should be provided with the entity id.");
}
return new ApplicationActor(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.core.context.model;

/**
* A Flow represents the complete journey of a particular process in the identity system.
* It can contain multiple requests and is initiated by a specific entity.
*/
public class Flow {

/**
* Enum for names.
* Identifies the flow.
*/
public enum Name {
PASSWORD_UPDATE,
PASSWORD_RESET,
USER_REGISTRATION_INVITE_WITH_PASSWORD
}

/**
* Enum for Initiator persona.
* Specifies the type of entity responsible for initiating the Flow
*/
ashanthamara marked this conversation as resolved.
Show resolved Hide resolved
public enum InitiatingPersona {
ADMIN,
APPLICATION,
USER
}

private final Name name;
private final InitiatingPersona initiatingPersona;

public Flow(Name name, InitiatingPersona initiatingPersona) {

this.name = name;
this.initiatingPersona = initiatingPersona;
}

public Name getName() {

return name;
}

public InitiatingPersona getInitiatingPersona() {

return initiatingPersona;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.core.context.model;

/**
* Actor class for User.
* This class holds the authenticated user actor details for a given flow.
*/
public class UserActor implements Actor {

private final String userId;

private UserActor(String userId) {

this.userId = userId;
}

public String getUserId() {

return userId;
}
}
Loading
Loading