Java Code Examples for org.apache.axis2.context.ServiceContext#getProperty()

The following examples show how to use org.apache.axis2.context.ServiceContext#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthenticatorClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
public String login(String userName, String password, String host) throws
        Exception {
    Boolean loginStatus;
    ServiceContext serviceContext;
    String sessionCookie;
    loginStatus = authenticationAdminStub.login(userName, password, host);
    if (!loginStatus) {
        throw new Exception("Login Unsuccessful. Return false as a login status by Server");
    }
    log.info("Login Successful");
    serviceContext = authenticationAdminStub._getServiceClient().getLastOperationContext().getServiceContext();
    sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
    if (log.isDebugEnabled()) {
        log.debug("SessionCookie :" + sessionCookie);
    }
    return sessionCookie;
}
 
Example 2
Source File: AuthenticatorClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
public String login(String userName, String password, String host) throws
        Exception {
    Boolean loginStatus;
    ServiceContext serviceContext;
    String sessionCookie;
    loginStatus = authenticationAdminStub.login(userName, password, host);
    if (!loginStatus) {
        throw new Exception("Login Unsuccessful. Return false as a login status by Server");
    }
    log.info("Login Successful");
    serviceContext = authenticationAdminStub._getServiceClient().getLastOperationContext().getServiceContext();
    sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
    if (log.isDebugEnabled()) {
        log.debug("SessionCookie :" + sessionCookie);
    }
    return sessionCookie;
}
 
Example 3
Source File: RemoteAuthorizationManagerClient.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Query the remote user manager to find out whether the specified user has the
 * specified permission.
 *
 * @param user Username
 * @param permission A valid Carbon permission
 * @return true if the user has the specified permission and false otherwise
 * @throws APIManagementException If and error occurs while accessing the admin service
 */
public boolean isUserAuthorized(String user, String permission) throws APIManagementException {
    CarbonUtils.setBasicAccessSecurityHeaders(username, password, authorizationManager._getServiceClient());
    if (cookie != null) {
        authorizationManager._getServiceClient().getOptions().setProperty(HTTPConstants.COOKIE_STRING, cookie);
    }

    try {
        boolean authorized = authorizationManager.isUserAuthorized(user, permission,
                CarbonConstants.UI_PERMISSION_ACTION);
        ServiceContext serviceContext = authorizationManager.
                _getServiceClient().getLastOperationContext().getServiceContext();
        cookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
        return authorized;
    } catch (Exception e) {
        throw new APIManagementException("Error while accessing backend services for " +
                "user permission validation", e);
    }
}
 
Example 4
Source File: RemoteAuthorizationManagerClient.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Query the remote user manager and retrieve the list of role names associated for the given
 * user name. This is used when authorization for certain actions are appropriately delegated
 * to other components (ex:- Lifecycle Management).
 *
 * @param user Username
 * @return the list of roles to which the user belongs to.
 * @throws APIManagementException If and error occurs while accessing the admin service
 */
public String[] getRolesOfUser(String user) throws APIManagementException {
    CarbonUtils.setBasicAccessSecurityHeaders(username, password, userStoreManager._getServiceClient());
    if (cookie != null) {
        userStoreManager._getServiceClient().getOptions().setProperty(HTTPConstants.COOKIE_STRING, cookie);
    }

    try {
        String[] roles = userStoreManager.getRoleListOfUser(user);
        ServiceContext serviceContext = userStoreManager.
                _getServiceClient().getLastOperationContext().getServiceContext();
        cookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
        return roles;
    } catch (Exception e) {
        throw new APIManagementException("Error while accessing backend services for " +
                "user role list", e);
    }
}
 
Example 5
Source File: RemoteAuthorizationManagerClient.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Query the remote user manager and retrieve the list of role names in users-store
 *
 *

 * @return the list of roles
 * @throws APIManagementException If and error occurs while accessing the admin service
 */
public String[] getRoleNames() throws APIManagementException {
    CarbonUtils.setBasicAccessSecurityHeaders(username, password, userStoreManager._getServiceClient());
    if (cookie != null) {
        userStoreManager._getServiceClient().getOptions().setProperty(HTTPConstants.COOKIE_STRING, cookie);
    }

    try {
        String[] roles = userStoreManager.getRoleNames();
        ServiceContext serviceContext = userStoreManager.
                _getServiceClient().getLastOperationContext().getServiceContext();
        cookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
        return roles;
    } catch (Exception e) {
        throw new APIManagementException("Error while accessing backend services for " +
                                         "getting list of all the roles.", e);
    }
}
 
Example 6
Source File: AuthenticationClient.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
public boolean authenticate(String username, String password) throws AuthenticationExceptionException,
        RemoteException {
    try {
        boolean isAuthenticated = stub.login(username,password,NetworkUtils.getLocalHostname());
        if(isAuthenticated){
            ServiceContext serviceContext;
            serviceContext = stub._getServiceClient().getLastOperationContext().getServiceContext();
            String sessionCookie;
            sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);

            this.sessionCookie = sessionCookie;
        }else{
            throw new AuthenticationExceptionException("Authentication Failed");
        }
        return isAuthenticated;
    } catch (SocketException e) {
        throw new AuthenticationExceptionException(e);
    }
}
 
Example 7
Source File: AuthenticatorClient.java    From product-es with Apache License 2.0 6 votes vote down vote up
public String login(String userName, String password, String host)
        throws LoginAuthenticationExceptionException, RemoteException {
    Boolean loginStatus;
    ServiceContext serviceContext;
    String sessionCookie;

    loginStatus = authenticationAdminStub.login(userName, password, host);

    if (!loginStatus) {
        throw new LoginAuthenticationExceptionException("Login Unsuccessful. Return false as a login status by Server");
    }
    log.info("Login Successful");
    serviceContext = authenticationAdminStub._getServiceClient().getLastOperationContext().getServiceContext();
    sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
    if (log.isDebugEnabled()) {
        log.debug("SessionCookie :" + sessionCookie);
    }
    return sessionCookie;
}
 
Example 8
Source File: DSSessionManager.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an object stored in the session with the given name.
 */
private static Object getSessionObject(String name) {
	MessageContext messageContext = MessageContext.getCurrentMessageContext();
	if (messageContext != null) {
		ServiceContext serviceContext = messageContext.getServiceContext();
		if (serviceContext != null) {
			return serviceContext.getProperty(name);
		}			
	} else {
		return threadLocalSession.get().get(name);
	}
	return null;
}
 
Example 9
Source File: GlobalThrottleEngineClient.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private String login() throws RemoteException, LoginAuthenticationExceptionException, MalformedURLException {
    authenticationAdminStub = getAuthenticationAdminStub();
    String sessionCookie = null;

    if (authenticationAdminStub.login(getPolicyDeployer().getUsername(), getPolicyDeployer().getPassword(),
            getHost())) {
        ServiceContext serviceContext = authenticationAdminStub._getServiceClient().getLastOperationContext()
                .getServiceContext();
        sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
    }
    return sessionCookie;
}
 
Example 10
Source File: APIAuthenticationServiceClient.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public APIAuthenticationServiceClient(String backendServerURL, String username, String password)
        throws Exception {
    try {
        AuthenticationAdminStub authenticationAdminStub = new AuthenticationAdminStub(null, backendServerURL + "AuthenticationAdmin");
        ServiceClient authAdminServiceClient = authenticationAdminStub._getServiceClient();
        authAdminServiceClient.getOptions().setManageSession(true);
        authenticationAdminStub.login(username, password, new URL(backendServerURL).getHost());
        ServiceContext serviceContext = authenticationAdminStub.
                _getServiceClient().getLastOperationContext().getServiceContext();
        String authenticatedCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);

        if (log.isDebugEnabled()) {
            log.debug("Authentication Successful with AuthenticationAdmin. " +
                      "Authenticated Cookie ID : " + authenticatedCookie);
        }

        stub = new APIAuthenticationServiceStub(
                null, backendServerURL + "APIAuthenticationService");
        ServiceClient client = stub._getServiceClient();
        Options options = client.getOptions();
        options.setManageSession(true);
        options.setProperty(HTTPConstants.COOKIE_STRING,
                            authenticatedCookie);
    } catch (Exception e) {
        String errorMsg = "Error when instantiating APIAuthenticationServiceClient.";
        log.error(errorMsg, e);
        throw e;
    }
}
 
Example 11
Source File: LoginManager.java    From product-ei with Apache License 2.0 4 votes vote down vote up
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String userName = null, userPassword = null;
    ServletContext servletContext = this.getServletContext();
    String backendServerURL = servletContext.getInitParameter(HumanTaskSampleConstants.BACKEND_SERVER_URL);

    // set the required system properties
    System.setProperty("javax.net.ssl.trustStore", servletContext
            .getInitParameter(HumanTaskSampleConstants.CLIENT_TRUST_STORE_PATH).trim());
    System.setProperty("javax.net.ssl.trustStorePassword", servletContext
            .getInitParameter(HumanTaskSampleConstants.CLIENT_TRUST_STORE_PASSWORD).trim());
    System.setProperty("javax.net.ssl.trustStoreType", servletContext
            .getInitParameter(HumanTaskSampleConstants.CLIENT_TRUST_STORE_TYPE).trim());
    try {
        AuthenticationAdminStub authenticationAdminStub = new AuthenticationAdminStub(backendServerURL +
                                                                                      HumanTaskSampleConstants
                                                                                              .SERVICE_URL +
                                                                                      HumanTaskSampleConstants
                                                                                              .AUTHENTICATION_ADMIN_SERVICE_URL);
        // handles logout
        String logout = req.getParameter("logout");
        if (logout != null) {
            authenticationAdminStub.logout();
            req.getRequestDispatcher("/Login.jsp").forward(req, resp);
            return;
        }
        if (req.getParameter("userName") != null) {
            userName = req.getParameter("userName").trim();
        }
        if (req.getParameter("userPassword") != null) {
            userPassword = req.getParameter("userPassword").trim();
        }
        // login to server with given user name and password
        if (authenticationAdminStub.login(userName, userPassword, HumanTaskSampleConstants.HOSTNAME)) {
            ServiceContext serviceContext = authenticationAdminStub._getServiceClient().getLastOperationContext()
                    .getServiceContext();
            String sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
            HttpSession session = req.getSession();
            session.setAttribute(HumanTaskSampleConstants.USERNAME, userName);
            session.setAttribute(HumanTaskSampleConstants.SESSION_COOKIE, sessionCookie);
            req.getRequestDispatcher("/Home.jsp?queryType=assignedToMe&pageNumber=0").forward(req, resp);

        } else {
            log.warn(userName + " login failed.");
            req.setAttribute("message", "Please enter a valid user name and a password.");
            req.getRequestDispatcher("/Login.jsp").forward(req, resp);
        }

    } catch (Exception e) {
        log.error("Failed to retrieve the user session ", e);
        req.setAttribute(HumanTaskSampleConstants.MESSAGE, e);
        req.getRequestDispatcher("/Login.jsp").forward(req, resp);
    }

}
 
Example 12
Source File: OAuthService.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Unless otherwise specified by the Service Provider, the time-stamp is expressed in the number
 * of seconds since January 1, 1970 00:00:00 GMT. The time-stamp value MUST be a positive
 * integer and MUST be equal or greater than the time-stamp used in previous requests. The
 * Consumer SHALL then generate a Nonce value that is unique for all requests with that
 * timestamp. A nonce is a random string, uniquely generated for each request. The nonce allows
 * the Service Provider to verify that a request has never been made before and helps prevent
 * replay attacks when requests are made over a non-secure channel (such as HTTP).
 *
 * @param timestamp
 * @param nonce
 * @throws Exception
 */
private void validateTimestampAndNonce(String timestamp, String nonce) throws AuthenticationException {
    if (timestamp == null || nonce == null || nonce.trim().length() == 0) {
        // We are not going to give out the exact error why the request failed.
        throw new AuthenticationException("Invalid request for OAuth access token");
    }

    long time = Long.parseLong(timestamp);

    synchronized (this) {
        long latestTimeStamp = 0;
        String strTimestamp;
        ServiceContext context = MessageContext.getCurrentMessageContext().getServiceContext();

        if ((strTimestamp = (String) context.getProperty(OAUTH_LATEST_TIMESTAMP)) != null) {
            latestTimeStamp = Long.parseLong(strTimestamp);
        }

        if (time < 0 || time < latestTimeStamp) {
            // The time-stamp value MUST be a positive integer and MUST be equal or greater than
            // the time-stamp used in previous requests
            throw new AuthenticationException("Invalid timestamp");
        }
        context.setProperty(OAUTH_LATEST_TIMESTAMP, String.valueOf(time));

        List<String> nonceStore = null;

        if ((nonceStore = (List<String>) context.getProperty(OAUTH_NONCE_STORE)) != null) {
            if (nonceStore.contains(nonce)) {
                // We are not going to give out the exact error why the request failed.
                throw new AuthenticationException("Invalid request for OAuth access token");
            } else {
                nonceStore.add(nonce);
            }
        } else {
            nonceStore = new ArrayList<String>();
            nonceStore.add(nonce);
            context.setProperty(OAUTH_NONCE_STORE, nonceStore);
        }
    }

}