fix fed example
This commit is contained in:
parent
24415f2186
commit
60a564dcfd
9 changed files with 51 additions and 17 deletions
|
@ -36,21 +36,19 @@ public abstract class BasePropertiesFederationFactory implements UserFederationP
|
|||
|
||||
|
||||
props = new Properties();
|
||||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
|
||||
if (is == null) {
|
||||
throw new IllegalStateException("Path attribute not configured for provider");
|
||||
|
||||
}
|
||||
InputStream is = getPropertiesFileStream(path);
|
||||
try {
|
||||
props.load(is);
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
props.put(path, props);
|
||||
files.put(path, props);
|
||||
return createProvider(session, model, props);
|
||||
}
|
||||
|
||||
protected abstract InputStream getPropertiesFileStream(String path);
|
||||
|
||||
protected abstract BasePropertiesFederationProvider createProvider(KeycloakSession session, UserFederationProviderModel model, Properties props);
|
||||
|
||||
|
||||
|
|
|
@ -59,7 +59,8 @@ public abstract class BasePropertiesFederationProvider implements UserFederation
|
|||
String password = properties.getProperty(username);
|
||||
if (password != null) {
|
||||
UserModel userModel = session.userStorage().addUser(realm, username);
|
||||
userModel.updateCredential(UserCredentialModel.password(password));
|
||||
userModel.setEnabled(true);
|
||||
userModel.setFederationLink(model.getId());
|
||||
return userModel;
|
||||
}
|
||||
return null;
|
||||
|
@ -72,14 +73,18 @@ public abstract class BasePropertiesFederationProvider implements UserFederation
|
|||
|
||||
@Override
|
||||
public List<UserModel> searchByAttributes(Map<String, String> attributes, RealmModel realm, int maxResults) {
|
||||
if (attributes.containsKey(USERNAME)) {
|
||||
UserModel user = getUserByUsername(realm, attributes.get(USERNAME));
|
||||
String username = attributes.get(USERNAME);
|
||||
if (username != null) {
|
||||
// make sure user isn't already in storage
|
||||
if (session.userStorage().getUserByUsername(username, realm) == null) {
|
||||
UserModel user = getUserByUsername(realm, username);
|
||||
if (user != null) {
|
||||
List<UserModel> list = new ArrayList<UserModel>(1);
|
||||
list.add(user);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,16 @@ public class ClasspathPropertiesFederationFactory extends BasePropertiesFederati
|
|||
return new ClasspathPropertiesFederationProvider(session, model, props);
|
||||
}
|
||||
|
||||
protected InputStream getPropertiesFileStream(String path) {
|
||||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
|
||||
if (is == null) {
|
||||
throw new IllegalStateException("Path not found for properties file");
|
||||
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
|
|
|
@ -3,6 +3,9 @@ package org.keycloak.examples.federation.properties;
|
|||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.UserFederationProviderModel;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
|
@ -15,6 +18,15 @@ public class FilePropertiesFederationFactory extends BasePropertiesFederationFac
|
|||
protected BasePropertiesFederationProvider createProvider(KeycloakSession session, UserFederationProviderModel model, Properties props) {
|
||||
return new FilePropertiesFederationProvider(session, props, model);
|
||||
}
|
||||
protected InputStream getPropertiesFileStream(String path) {
|
||||
try {
|
||||
return new FileInputStream(path);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
|
|
|
@ -51,16 +51,15 @@ public class WritableUserModelProxy extends UserModelDelegate {
|
|||
@Override
|
||||
public void updateCredential(UserCredentialModel cred) {
|
||||
if (cred.getType().equals(UserCredentialModel.PASSWORD)) {
|
||||
|
||||
} else {
|
||||
super.updateCredential(cred);
|
||||
synchronized (provider.getProperties()) {
|
||||
if (!provider.getProperties().containsKey(delegate.getUsername())) {
|
||||
throw new IllegalStateException("no user of that in properties file");
|
||||
}
|
||||
provider.getProperties().put(delegate.getUsername(), cred.getValue());
|
||||
provider.getProperties().setProperty(delegate.getUsername(), cred.getValue());
|
||||
provider.save();
|
||||
}
|
||||
} else {
|
||||
super.updateCredential(cred);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
tbrady=superbowl
|
|
@ -0,0 +1,3 @@
|
|||
#
|
||||
#Sun Aug 03 10:52:57 EDT 2014
|
||||
belichick=superbowl
|
|
@ -473,6 +473,7 @@ public class AccountService {
|
|||
try {
|
||||
session.users().updateCredential(realm, user, UserCredentialModel.password(passwordNew));
|
||||
} catch (Exception ape) {
|
||||
logger.error("Failed to update password", ape);
|
||||
return account.setError(ape.getMessage()).createResponse(AccountPages.PASSWORD);
|
||||
}
|
||||
|
||||
|
|
|
@ -124,6 +124,11 @@
|
|||
<artifactId>keycloak-undertow-adapter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.keycloak</groupId>
|
||||
<artifactId>federation-properties-example</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jboss.logging</groupId>
|
||||
<artifactId>jboss-logging</artifactId>
|
||||
|
|
Loading…
Reference in a new issue