Search in sources :

Example 1 with Credentials

use of org.openhab.core.auth.Credentials in project openhab-core by openhab.

the class ManagedUserLoginModule method login.

@Override
public boolean login() throws LoginException {
    try {
        // try to get the UserRegistry instance
        BundleContext bundleContext = FrameworkUtil.getBundle(UserRegistry.class).getBundleContext();
        ServiceReference<UserRegistry> serviceReference = bundleContext.getServiceReference(UserRegistry.class);
        userRegistry = bundleContext.getService(serviceReference);
    } catch (Exception e) {
        logger.error("Cannot initialize the ManagedLoginModule", e);
        throw new LoginException("Authorization failed");
    }
    try {
        Credentials credentials = (Credentials) this.subject.getPrivateCredentials().iterator().next();
        userRegistry.authenticate(credentials);
        return true;
    } catch (AuthenticationException e) {
        throw new LoginException(e.getMessage());
    }
}
Also used : AuthenticationException(org.openhab.core.auth.AuthenticationException) UserRegistry(org.openhab.core.auth.UserRegistry) LoginException(javax.security.auth.login.LoginException) LoginException(javax.security.auth.login.LoginException) AuthenticationException(org.openhab.core.auth.AuthenticationException) Credentials(org.openhab.core.auth.Credentials) BundleContext(org.osgi.framework.BundleContext)

Example 2 with Credentials

use of org.openhab.core.auth.Credentials in project openhab-core by openhab.

the class AuthenticationHandler method handle.

@Override
public void handle(final HttpServletRequest request, final HttpServletResponse response, final HandlerContext context) throws Exception {
    String requestUri = request.getRequestURI();
    if (this.enabled && isSecured(requestUri, request.getMethod())) {
        if (authenticationManager == null) {
            throw new AuthenticationException("Failed to authenticate request.");
        }
        int found = 0, failed = 0;
        for (CredentialsExtractor<HttpServletRequest> extractor : extractors) {
            Optional<Credentials> extracted = extractor.retrieveCredentials(request);
            if (extracted.isPresent()) {
                found++;
                Credentials credentials = extracted.get();
                try {
                    Authentication authentication = authenticationManager.authenticate(credentials);
                    request.setAttribute(Authentication.class.getName(), authentication);
                    context.execute(request, response);
                    return;
                } catch (AuthenticationException e) {
                    failed++;
                    if (logger.isDebugEnabled()) {
                        logger.debug("Failed to authenticate using credentials {}", credentials, e);
                    } else {
                        logger.info("Failed to authenticate using credentials {}", credentials);
                    }
                }
            }
        }
        throw new AuthenticationException("Could not authenticate request. Found " + found + " credentials in request out of which " + failed + " were invalid");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationException(org.openhab.core.auth.AuthenticationException) Authentication(org.openhab.core.auth.Authentication) Credentials(org.openhab.core.auth.Credentials)

Aggregations

AuthenticationException (org.openhab.core.auth.AuthenticationException)2 Credentials (org.openhab.core.auth.Credentials)2 LoginException (javax.security.auth.login.LoginException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Authentication (org.openhab.core.auth.Authentication)1 UserRegistry (org.openhab.core.auth.UserRegistry)1 BundleContext (org.osgi.framework.BundleContext)1