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());
}
}
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");
}
}
Aggregations