Search in sources :

Example 1 with AuthenticationService

use of com.haulmont.cuba.security.auth.AuthenticationService in project cuba by cuba-platform.

the class Connection method logout.

public void logout() {
    try {
        AuthenticationService authenticationService = AppBeans.get(AuthenticationService.NAME);
        authenticationService.logout();
        AppContext.setSecurityContext(null);
        log.info("Logged out: " + session);
    } catch (Exception e) {
        log.warn("Error on logout", e);
    }
    connected = false;
    try {
        fireConnectionListeners();
    } catch (LoginException e) {
        log.warn("Error on logout", e);
    }
    session = null;
}
Also used : LoginException(com.haulmont.cuba.security.global.LoginException) AuthenticationService(com.haulmont.cuba.security.auth.AuthenticationService) LoginException(com.haulmont.cuba.security.global.LoginException) UnknownHostException(java.net.UnknownHostException)

Example 2 with AuthenticationService

use of com.haulmont.cuba.security.auth.AuthenticationService in project cuba by cuba-platform.

the class Connection method doLogin.

/**
 * Forward login logic to {@link com.haulmont.cuba.security.auth.AuthenticationService}.
 * Can be overridden to change login logic.
 *
 * @param login       login name
 * @param password    encrypted password
 * @param locale      client locale
 * @param loginParams login parameters
 * @return created user session
 * @throws LoginException in case of unsuccessful login
 */
protected UserSession doLogin(String login, String password, Locale locale, Map<String, Object> loginParams) throws LoginException {
    AbstractClientCredentials credentials = new LoginPasswordCredentials(login, password, locale);
    setCredentialsParams(credentials, loginParams);
    AuthenticationService authenticationService = AppBeans.get(AuthenticationService.NAME);
    return authenticationService.login(credentials).getSession();
}
Also used : LoginPasswordCredentials(com.haulmont.cuba.security.auth.LoginPasswordCredentials) AbstractClientCredentials(com.haulmont.cuba.security.auth.AbstractClientCredentials) AuthenticationService(com.haulmont.cuba.security.auth.AuthenticationService)

Example 3 with AuthenticationService

use of com.haulmont.cuba.security.auth.AuthenticationService in project cuba by cuba-platform.

the class LoginServiceController method doLogout.

protected void doLogout(String sessionUUID, HttpServletResponse response) throws IOException, JSONException {
    try {
        if (authentication.begin(sessionUUID)) {
            AuthenticationService authenticationService = AppBeans.get(AuthenticationService.NAME);
            authenticationService.logout();
        }
    } catch (Throwable e) {
        log.error("Error processing logout request", e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : AuthenticationService(com.haulmont.cuba.security.auth.AuthenticationService)

Example 4 with AuthenticationService

use of com.haulmont.cuba.security.auth.AuthenticationService in project cuba by cuba-platform.

the class LoginServiceController method doLogin.

protected void doLogin(String username, String password, String localeStr, HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {
    Locale locale = localeFromString(localeStr);
    AuthenticationService authenticationService = AppBeans.get(AuthenticationService.NAME);
    try {
        AbstractClientCredentials credentials = new LoginPasswordCredentials(username, passwordEncryption.getPlainHash(password), locale);
        UserSession userSession = authenticationService.login(credentials).getSession();
        if (!userSession.isSpecificPermitted(Authentication.PERMISSION_NAME)) {
            log.info(String.format("User %s is not allowed to use REST-API", username));
            AppContext.setSecurityContext(new SecurityContext(userSession));
            try {
                authenticationService.logout();
            } finally {
                AppContext.setSecurityContext(null);
            }
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        try {
            AppContext.setSecurityContext(new SecurityContext(userSession));
            setSessionInfo(request, userSession);
        } finally {
            AppContext.setSecurityContext(null);
        }
        response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8));
        writer.write(userSession.getId().toString());
        writer.close();
        log.debug(String.format("User %s logged in with REST-API, session id: %s", username, userSession.getId()));
    } catch (LoginException e) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }
}
Also used : Locale(java.util.Locale) UserSession(com.haulmont.cuba.security.global.UserSession) LoginPasswordCredentials(com.haulmont.cuba.security.auth.LoginPasswordCredentials) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) AbstractClientCredentials(com.haulmont.cuba.security.auth.AbstractClientCredentials) LoginException(com.haulmont.cuba.security.global.LoginException) OutputStreamWriter(java.io.OutputStreamWriter) AuthenticationService(com.haulmont.cuba.security.auth.AuthenticationService) PrintWriter(java.io.PrintWriter)

Aggregations

AuthenticationService (com.haulmont.cuba.security.auth.AuthenticationService)4 AbstractClientCredentials (com.haulmont.cuba.security.auth.AbstractClientCredentials)2 LoginPasswordCredentials (com.haulmont.cuba.security.auth.LoginPasswordCredentials)2 LoginException (com.haulmont.cuba.security.global.LoginException)2 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)1 UserSession (com.haulmont.cuba.security.global.UserSession)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 UnknownHostException (java.net.UnknownHostException)1 Locale (java.util.Locale)1