2020-08-13 16:27:32 +02:00
|
|
|
package org.gcube.keycloak.avatar;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
2022-09-30 17:03:01 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2020-08-13 16:27:32 +02:00
|
|
|
|
2020-11-16 23:32:11 +01:00
|
|
|
import javax.ws.rs.NotFoundException;
|
|
|
|
import javax.ws.rs.core.Response;
|
2020-08-13 16:27:32 +02:00
|
|
|
import javax.ws.rs.core.StreamingOutput;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.gcube.keycloak.avatar.storage.AvatarStorageProvider;
|
|
|
|
import org.gcube.keycloak.avatar.storage.file.FileAvatarStorageProvider;
|
|
|
|
import org.jboss.logging.Logger;
|
|
|
|
import org.keycloak.models.KeycloakSession;
|
|
|
|
import org.keycloak.models.RealmModel;
|
|
|
|
import org.keycloak.models.UserModel;
|
2020-11-16 23:32:11 +01:00
|
|
|
import org.keycloak.services.managers.AppAuthManager;
|
|
|
|
import org.keycloak.services.managers.AuthenticationManager;
|
|
|
|
import org.keycloak.services.managers.AuthenticationManager.AuthResult;
|
2020-08-13 16:27:32 +02:00
|
|
|
|
|
|
|
public abstract class AbstractAvatarResource {
|
|
|
|
|
|
|
|
protected static final Logger logger = Logger.getLogger(AbstractAvatarResource.class);
|
|
|
|
|
|
|
|
protected static final String AVATAR_IMAGE_PARAMETER = "image";
|
|
|
|
|
|
|
|
public static final Class<?> DEFAULT_IMPLEMENTATION = FileAvatarStorageProvider.class;
|
|
|
|
|
|
|
|
protected KeycloakSession session;
|
2020-11-16 23:32:11 +01:00
|
|
|
protected AuthenticationManager.AuthResult auth;
|
2020-08-13 16:27:32 +02:00
|
|
|
|
|
|
|
public AbstractAvatarResource(KeycloakSession session) {
|
|
|
|
this.session = session;
|
2020-11-16 23:32:11 +01:00
|
|
|
auth = authenticate(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
private AuthResult authenticate(KeycloakSession session) {
|
|
|
|
logger.debug("Authenticating with bearer token");
|
2021-12-24 17:44:56 +01:00
|
|
|
AuthResult auth = this.auth = new AppAuthManager.BearerTokenAuthenticator(session).authenticate();
|
|
|
|
|
2020-11-16 23:32:11 +01:00
|
|
|
if (auth == null) {
|
|
|
|
logger.debug("Authenticating with identity cookie");
|
|
|
|
auth = new AppAuthManager().authenticateIdentityCookie(session, session.getContext().getRealm());
|
|
|
|
}
|
|
|
|
return auth;
|
2020-08-13 16:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public AvatarStorageProvider getAvatarStorageProvider() {
|
|
|
|
AvatarStorageProvider asp = lookupAvatarStorageProvider(session);
|
|
|
|
if (asp == null) {
|
2021-05-20 18:26:24 +02:00
|
|
|
logger.warnf("Provider not found via SPI configuration, defaulting to: %s",
|
2020-08-13 16:27:32 +02:00
|
|
|
DEFAULT_IMPLEMENTATION.getName());
|
|
|
|
|
|
|
|
try {
|
2022-09-30 17:03:01 +02:00
|
|
|
asp = (AvatarStorageProvider) DEFAULT_IMPLEMENTATION.getDeclaredConstructor().newInstance();
|
|
|
|
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
|
|
|
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
|
|
|
|
2020-08-13 16:27:32 +02:00
|
|
|
logger.error("Cannot instatiate storage implementation class", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return asp;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected AvatarStorageProvider lookupAvatarStorageProvider(KeycloakSession keycloakSession) {
|
|
|
|
return keycloakSession.getProvider(AvatarStorageProvider.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void saveUserImage(RealmModel realm, UserModel user, InputStream imageInputStream) {
|
|
|
|
getAvatarStorageProvider().saveAvatarImage(realm, user, imageInputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected StreamingOutput fetchUserImage(RealmModel realm, UserModel user) {
|
|
|
|
AvatarStorageProvider asp = getAvatarStorageProvider();
|
|
|
|
InputStream is = asp.loadAvatarImage(realm, user);
|
2020-09-04 18:10:13 +02:00
|
|
|
return is != null ? output -> IOUtils.copy(is, output) : null;
|
2020-08-13 16:27:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-16 23:32:11 +01:00
|
|
|
protected Response fetchAndCreateResponse(RealmModel realm, UserModel user) {
|
|
|
|
StreamingOutput so = fetchUserImage(realm, user);
|
|
|
|
if (so == null) {
|
|
|
|
throw new NotFoundException("Avatar image not found");
|
|
|
|
}
|
|
|
|
return Response.ok(so).build();
|
|
|
|
}
|
|
|
|
|
2020-08-13 16:27:32 +02:00
|
|
|
}
|