use of org.wso2.carbon.context.CarbonContext in project core-util by WSO2Telco.
the class UserRoleProsser method getRolesByUserName.
public List<String> getRolesByUserName(String userName) {
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
List<String> currentUserRoleList = null;
try {
RealmConfiguration realmConfiguration = new RealmConfiguration();
String[] currentUserRoles = realmService.getUserRealm(realmConfiguration).getUserStoreManager().getRoleListOfUser(userName);
currentUserRoleList = Arrays.asList(currentUserRoles);
} catch (org.wso2.carbon.user.api.UserStoreException e) {
log.error("unable to retrieve user roles for user " + userName + " : ", e);
}
if (currentUserRoleList != null && !currentUserRoleList.isEmpty()) {
return currentUserRoleList;
} else {
return Collections.emptyList();
}
}
use of org.wso2.carbon.context.CarbonContext in project core-util by WSO2Telco.
the class BasicAuthenticator method isAuthenticatedUser.
public boolean isAuthenticatedUser(String userName, String password) {
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
RegistryService registryService = (RegistryService) carbonContext.getOSGiService(RegistryService.class, null);
String tenantDomain = MultitenantUtils.getTenantDomain(userName);
try {
UserRealm userRealm = null;
userRealm = AnonymousSessionUtil.getRealmByTenantDomain(registryService, realmService, tenantDomain);
if (userRealm == null) {
log.error("invalid domain or unactivated tenant login");
return false;
}
String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(userName);
if (userRealm.getUserStoreManager().authenticate(tenantAwareUsername, password)) {
return true;
} else {
log.error("authentication failed. please check your username/password");
return false;
}
} catch (CarbonException | UserStoreException e) {
log.error("authentication failed for user : " + userName, e);
return false;
}
}
use of org.wso2.carbon.context.CarbonContext in project carbon-business-process by wso2.
the class AuthenticationHandler method authenticate.
/**
* Checks whether a given userName:password combination authenticates correctly against carbon userStore
* Upon successful authentication returns true, false otherwise
*
* @param userName
* @param password
* @return
* @throws RestApiBasicAuthenticationException wraps and throws exceptions occur when trying to authenticate
* the user
*/
private boolean authenticate(String userName, String password) throws RestApiBasicAuthenticationException {
boolean authStatus;
try {
IdentityService identityService = BPMNOSGIService.getIdentityService();
authStatus = identityService.checkPassword(userName, password);
if (!authStatus) {
return false;
}
} catch (BPMNAuthenticationException e) {
throw new RestApiBasicAuthenticationException(e.getMessage(), e);
}
String tenantDomain = MultitenantUtils.getTenantDomain(userName);
String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(userName);
String userNameWithTenantDomain = tenantAwareUserName + "@" + tenantDomain;
RealmService realmService = RegistryContext.getBaseInstance().getRealmService();
TenantManager mgr = realmService.getTenantManager();
int tenantId = 0;
try {
tenantId = mgr.getTenantId(tenantDomain);
// tenantId == -1, means an invalid tenant.
if (tenantId == -1) {
if (log.isDebugEnabled()) {
log.debug("Basic authentication request with an invalid tenant : " + userNameWithTenantDomain);
}
return false;
}
} catch (UserStoreException e) {
throw new RestApiBasicAuthenticationException("Identity exception thrown while getting tenant ID for user : " + userNameWithTenantDomain, e);
}
/* Upon successful authentication existing thread local carbon context
* is updated to mimic the authenticated user */
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
carbonContext.setUsername(tenantAwareUserName);
carbonContext.setTenantId(tenantId);
carbonContext.setTenantDomain(tenantDomain);
return true;
}
use of org.wso2.carbon.context.CarbonContext in project jaggery by wso2.
the class RhinoTopLevel method setInterval.
public static String setInterval(Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "setTimeout";
int argsCount = args.length;
if (argsCount < 2) {
HostObjectUtil.invalidNumberOfArgs(EngineConstants.GLOBAL_OBJECT_NAME, functionName, argsCount, false);
}
Function function = null;
long interval;
if (args[0] instanceof Function) {
function = (Function) args[0];
} else if (args[0] instanceof String) {
function = getFunction(cx, thisObj, (String) args[0], functionName);
} else {
HostObjectUtil.invalidArgsError(EngineConstants.GLOBAL_OBJECT_NAME, EngineConstants.GLOBAL_OBJECT_NAME, "1", "string|function", args[0], false);
}
if (!(args[1] instanceof Number)) {
HostObjectUtil.invalidArgsError(EngineConstants.GLOBAL_OBJECT_NAME, EngineConstants.GLOBAL_OBJECT_NAME, "2", "number", args[1], false);
}
if (function == null) {
String error = "Callback cannot be null in " + functionName;
log.error(error);
throw new ScriptException(error);
}
final JaggeryContext context = getJaggeryContext();
final Object[] params = Arrays.copyOfRange(args, 2, args.length);
final Function callback = function;
final ContextFactory factory = cx.getFactory();
interval = ((Number) args[1]).longValue();
String uuid = UUID.randomUUID().toString();
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
final int tenantId = carbonContext.getTenantId();
final String tenantDomain = carbonContext.getTenantDomain();
final String applicationName = carbonContext.getApplicationName();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ScheduledFuture future = timerExecutor.scheduleAtFixedRate(new Runnable() {
private boolean firstTime = true;
@Override
public void run() {
// set the context classloader
Thread currentThread = Thread.currentThread();
ClassLoader originalClassLoader = currentThread.getContextClassLoader();
Thread.currentThread().setContextClassLoader(contextClassLoader);
// child inherits context properties form the parent thread.
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
carbonContext.setTenantId(tenantId);
carbonContext.setTenantDomain(tenantDomain);
carbonContext.setApplicationName(applicationName);
try {
Context cx = RhinoEngine.enterContext(factory);
RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, context);
callback.call(cx, thisObj, thisObj, params);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
RhinoEngine.exitContext();
currentThread.setContextClassLoader(originalClassLoader);
}
}
}, interval, interval, TimeUnit.MILLISECONDS);
Map<String, ScheduledFuture> tasks = intervals.get(context.getTenantDomain());
if (tasks == null) {
tasks = new HashMap<String, ScheduledFuture>();
intervals.put(context.getTenantDomain(), tasks);
}
tasks.put(uuid, future);
return uuid;
}
use of org.wso2.carbon.context.CarbonContext in project jaggery by wso2.
the class RhinoTopLevel method setTimeout.
public static String setTimeout(Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "setTimeout";
int argsCount = args.length;
if (argsCount < 2) {
HostObjectUtil.invalidNumberOfArgs(EngineConstants.GLOBAL_OBJECT_NAME, functionName, argsCount, false);
}
Function function = null;
long timeout;
if (args[0] instanceof Function) {
function = (Function) args[0];
} else if (args[0] instanceof String) {
function = getFunction(cx, thisObj, (String) args[0], functionName);
} else {
HostObjectUtil.invalidArgsError(EngineConstants.GLOBAL_OBJECT_NAME, EngineConstants.GLOBAL_OBJECT_NAME, "1", "string|function", args[0], false);
}
if (!(args[1] instanceof Number)) {
HostObjectUtil.invalidArgsError(EngineConstants.GLOBAL_OBJECT_NAME, EngineConstants.GLOBAL_OBJECT_NAME, "2", "number", args[1], false);
}
if (function == null) {
String error = "Callback cannot be null in " + functionName;
log.error(error);
throw new ScriptException(error);
}
final JaggeryContext context = getJaggeryContext();
final Object[] params = Arrays.copyOfRange(args, 2, args.length);
final Function callback = function;
final ContextFactory factory = cx.getFactory();
timeout = ((Number) args[1]).longValue();
final String uuid = UUID.randomUUID().toString();
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
final int tenantId = carbonContext.getTenantId();
final String tenantDomain = carbonContext.getTenantDomain();
final String applicationName = carbonContext.getApplicationName();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ScheduledFuture future = timerExecutor.schedule(new Callable<Void>() {
public Void call() throws Exception {
// set the context classloader
Thread currentThread = Thread.currentThread();
ClassLoader originalClassLoader = currentThread.getContextClassLoader();
Thread.currentThread().setContextClassLoader(contextClassLoader);
// child inherits context properties form the parent thread.
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
carbonContext.setTenantId(tenantId);
carbonContext.setTenantDomain(tenantDomain);
carbonContext.setApplicationName(applicationName);
try {
Context ctx = RhinoEngine.enterContext(factory);
RhinoEngine.putContextProperty(EngineConstants.JAGGERY_CONTEXT, context);
callback.call(ctx, thisObj, thisObj, params);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
clearTimeout(uuid);
PrivilegedCarbonContext.endTenantFlow();
RhinoEngine.exitContext();
currentThread.setContextClassLoader(originalClassLoader);
}
return null;
}
}, timeout, TimeUnit.MILLISECONDS);
Map<String, ScheduledFuture> tasks = timeouts.get(context.getTenantDomain());
if (tasks == null) {
tasks = new HashMap<String, ScheduledFuture>();
timeouts.put(context.getTenantDomain(), tasks);
}
tasks.put(uuid, future);
return uuid;
}
Aggregations