Search in sources :

Example 6 with AbstractClientCredentials

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

the class ConnectionImpl method preprocessCredentials.

protected void preprocessCredentials(Credentials credentials) {
    if (credentials instanceof AbstractClientCredentials) {
        AbstractClientCredentials clientCredentials = (AbstractClientCredentials) credentials;
        clientCredentials.setClientType(ClientType.WEB);
        clientCredentials.setClientInfo(makeClientInfo());
        clientCredentials.setTimeZone(detectTimeZone());
        String currentUserRemoteAddress = getUserRemoteAddress();
        // update userRemoteAddress if current HTTP request is available
        if (currentUserRemoteAddress != null) {
            this.userRemoteAddress = currentUserRemoteAddress;
        }
        clientCredentials.setIpAddress(userRemoteAddress);
    }
}
Also used : AbstractClientCredentials(com.haulmont.cuba.security.auth.AbstractClientCredentials)

Example 7 with AbstractClientCredentials

use of com.haulmont.cuba.security.auth.AbstractClientCredentials 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

AbstractClientCredentials (com.haulmont.cuba.security.auth.AbstractClientCredentials)7 LoginPasswordCredentials (com.haulmont.cuba.security.auth.LoginPasswordCredentials)3 LoginException (com.haulmont.cuba.security.global.LoginException)3 AuthenticationService (com.haulmont.cuba.security.auth.AuthenticationService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 Locale (java.util.Locale)2 ClientType (com.haulmont.cuba.core.global.ClientType)1 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)1 Credentials (com.haulmont.cuba.security.auth.Credentials)1 IpMatcher (com.haulmont.cuba.security.global.IpMatcher)1 UserIpRestrictedException (com.haulmont.cuba.security.global.UserIpRestrictedException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 EventListener (org.springframework.context.event.EventListener)1 Order (org.springframework.core.annotation.Order)1