use of org.openhab.core.auth.AuthenticationException in project openhab-addons by openhab.
the class GoogleCloudAPI method getAccessToken.
/**
* Fetches the OAuth2 tokens from Google Cloud Platform if the auth-code is set in the configuration. If successful
* the auth-code will be removed from the configuration.
*
* @throws AuthenticationException
* @throws CommunicationException
*/
@SuppressWarnings("null")
private void getAccessToken() throws AuthenticationException, CommunicationException {
String authcode = config.authcode;
if (authcode != null && !authcode.isEmpty()) {
logger.debug("Trying to get access and refresh tokens.");
try {
oAuthService.getAccessTokenResponseByAuthorizationCode(authcode, GCP_REDIRECT_URI);
} catch (OAuthException | OAuthResponseException e) {
logger.debug("Error fetching access token: {}", e.getMessage(), e);
throw new AuthenticationException("Error fetching access token. Invalid authcode? Please generate a new one.");
} catch (IOException e) {
throw new CommunicationException(String.format("An unexpected IOException occurred: %s", e.getMessage()));
}
config.authcode = null;
try {
Configuration serviceConfig = configAdmin.getConfiguration(GoogleTTSService.SERVICE_PID);
Dictionary<String, Object> configProperties = serviceConfig.getProperties();
if (configProperties != null) {
configProperties.put(GoogleTTSService.PARAM_AUTHCODE, "");
serviceConfig.update(configProperties);
}
} catch (IOException e) {
// should not happen
logger.warn("Failed to update configuration for Google Cloud TTS service. Please clear the 'authcode' configuration parameter manualy.");
}
}
}
use of org.openhab.core.auth.AuthenticationException in project openhab-core by openhab.
the class JaasAuthenticationProvider method authenticate.
@Override
public Authentication authenticate(final Credentials credentials) throws AuthenticationException {
if (realmName == null) {
// configuration is not yet ready or set
realmName = DEFAULT_REALM;
}
if (!(credentials instanceof UsernamePasswordCredentials)) {
throw new AuthenticationException("Unsupported credentials passed to provider.");
}
UsernamePasswordCredentials userCredentials = (UsernamePasswordCredentials) credentials;
final String name = userCredentials.getUsername();
final char[] password = userCredentials.getPassword().toCharArray();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Principal userPrincipal = new GenericUser(name);
Subject subject = new Subject(true, Set.of(userPrincipal), Collections.emptySet(), Set.of(userCredentials));
Thread.currentThread().setContextClassLoader(ManagedUserLoginModule.class.getClassLoader());
LoginContext loginContext = new LoginContext(realmName, subject, new CallbackHandler() {
@Override
public void handle(@NonNullByDefault({}) Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(password);
} else if (callback instanceof NameCallback) {
((NameCallback) callback).setName(name);
} else {
throw new UnsupportedCallbackException(callback);
}
}
}
}, new ManagedUserLoginConfiguration());
loginContext.login();
return getAuthentication(name, loginContext.getSubject());
} catch (LoginException e) {
String message = e.getMessage();
throw new AuthenticationException(message != null ? message : "An unexpected LoginException occurred");
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
use of org.openhab.core.auth.AuthenticationException 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.AuthenticationException in project openhab-core by openhab.
the class AuthFilter method authenticateBearerToken.
private SecurityContext authenticateBearerToken(String token) throws AuthenticationException {
if (token.startsWith(API_TOKEN_PREFIX)) {
UserApiTokenCredentials credentials = new UserApiTokenCredentials(token);
Authentication auth = userRegistry.authenticate(credentials);
User user = userRegistry.get(auth.getUsername());
if (user == null) {
throw new AuthenticationException("User not found in registry");
}
return new UserSecurityContext(user, auth, "ApiToken");
} else {
Authentication auth = jwtHelper.verifyAndParseJwtAccessToken(token);
return new JwtSecurityContext(auth);
}
}
use of org.openhab.core.auth.AuthenticationException in project openhab-core by openhab.
the class AuthFilter method authenticateBasicAuth.
private SecurityContext authenticateBasicAuth(String credentialString) throws AuthenticationException {
final String cacheKey = getCacheKey(credentialString);
if (cacheKey != null) {
final UserSecurityContext cachedValue = authCache.get(cacheKey);
if (cachedValue != null) {
return cachedValue;
}
}
String[] decodedCredentials = new String(Base64.getDecoder().decode(credentialString), StandardCharsets.UTF_8).split(":");
if (decodedCredentials.length != 2) {
throw new AuthenticationException("Invalid Basic authentication credential format");
}
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(decodedCredentials[0], decodedCredentials[1]);
Authentication auth = userRegistry.authenticate(credentials);
User user = userRegistry.get(auth.getUsername());
if (user == null) {
throw new AuthenticationException("User not found in registry");
}
UserSecurityContext context = new UserSecurityContext(user, auth, "Basic");
if (cacheKey != null) {
authCache.put(cacheKey, context);
}
return context;
}
Aggregations