Merge pull request #583 from patriot1burke/master
delete auth prov example
This commit is contained in:
commit
69656d2775
6 changed files with 0 additions and 255 deletions
|
@ -1,31 +0,0 @@
|
||||||
Example Authentication Provider based on property file values
|
|
||||||
=============================================================
|
|
||||||
|
|
||||||
* To deploy copy "target/authentication-properties-example.jar" to "standalone/deployments/auth-server.war/WEB-INF/lib" .
|
|
||||||
|
|
||||||
* Then you can configure location of property file, from which will be users and their passwords retrieved. If you omit it and won't configure, provider will use default "users.properties" file bundled inside this example.
|
|
||||||
So for custom location, edit "standalone/configuration/keycloak-server.json" and add this:
|
|
||||||
````
|
|
||||||
"authentication": {
|
|
||||||
"properties": {
|
|
||||||
"propertiesFileLocation": "/tmp/your-own-property-file.properties"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
````
|
|
||||||
|
|
||||||
Assumption is that your file has format like:
|
|
||||||
|
|
||||||
````
|
|
||||||
username1=password1
|
|
||||||
username2=password2
|
|
||||||
````
|
|
||||||
|
|
||||||
* Then start (or restart)the server. Once started open the admin console, select your realm, then click on "Authentication" and then "Add provider" and select "properties" from the list.
|
|
||||||
This will mean that realm will use PropertiesAuthenticationProvider for authentication.
|
|
||||||
|
|
||||||
* Once you try to login to realm, you can login with username/password like "joe/password1" or "james/password2" (In case that you use default users.properties file) . Once joe is authenticated,
|
|
||||||
you can see in Keycloak admin console in "Users" list that user "joe" was added to the list.
|
|
||||||
|
|
||||||
* You can try to login as joe and then go to [http://localhost:8080/auth/realms/demo/account/password](http://localhost:8080/auth/realms/demo/account/password) and change the password.
|
|
||||||
You will then be able to logout and login with new password because properties were updated.
|
|
||||||
WARNING: If you use default location, properties will be updated just in memory and won't survive server restart. So in this case, you will have again "joe" with password "password1" after restart.
|
|
|
@ -1,54 +0,0 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>examples-providers-pom</artifactId>
|
|
||||||
<groupId>org.keycloak</groupId>
|
|
||||||
<version>1.0-beta-4-SNAPSHOT</version>
|
|
||||||
<relativePath>../pom.xml</relativePath>
|
|
||||||
</parent>
|
|
||||||
<name>Properties Authentication Provider Example</name>
|
|
||||||
<description/>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<artifactId>authentication-properties-example</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.keycloak</groupId>
|
|
||||||
<artifactId>keycloak-core</artifactId>
|
|
||||||
<version>${project.version}</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.keycloak</groupId>
|
|
||||||
<artifactId>keycloak-model-api</artifactId>
|
|
||||||
<version>${project.version}</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.keycloak</groupId>
|
|
||||||
<artifactId>keycloak-authentication-api</artifactId>
|
|
||||||
<version>${project.version}</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.jboss.logging</groupId>
|
|
||||||
<artifactId>jboss-logging</artifactId>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<finalName>authentication-properties-example</finalName>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-deploy-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<skip>true</skip>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
|
|
@ -1,75 +0,0 @@
|
||||||
package org.keycloak.examples.providers.authentication;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
|
||||||
import org.keycloak.authentication.AuthProviderStatus;
|
|
||||||
import org.keycloak.authentication.AuthUser;
|
|
||||||
import org.keycloak.authentication.AuthenticationProvider;
|
|
||||||
import org.keycloak.authentication.AuthenticationProviderException;
|
|
||||||
import org.keycloak.models.RealmModel;
|
|
||||||
import org.keycloak.models.UserModel;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
|
||||||
*/
|
|
||||||
public class PropertiesAuthenticationProvider implements AuthenticationProvider {
|
|
||||||
|
|
||||||
private static final Logger log = Logger.getLogger(PropertiesAuthenticationProvider.class);
|
|
||||||
|
|
||||||
private final Properties properties;
|
|
||||||
|
|
||||||
public PropertiesAuthenticationProvider(Properties properties) {
|
|
||||||
this.properties = properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "properties";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getAvailableOptions() {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AuthUser getUser(RealmModel realm, Map<String, String> configuration, String username) throws AuthenticationProviderException {
|
|
||||||
if (properties.getProperty(username) != null) {
|
|
||||||
return new AuthUser(username, username, getName());
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String registerUser(RealmModel realm, Map<String, String> configuration, UserModel user) throws AuthenticationProviderException {
|
|
||||||
// Registration ignored
|
|
||||||
return user.getUsername();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AuthProviderStatus validatePassword(RealmModel realm, Map<String, String> configuration, String username, String password) throws AuthenticationProviderException {
|
|
||||||
String propertyFilePassword = properties.getProperty(username);
|
|
||||||
if (propertyFilePassword != null && propertyFilePassword.equals(password)) {
|
|
||||||
return AuthProviderStatus.SUCCESS;
|
|
||||||
} else {
|
|
||||||
return AuthProviderStatus.INVALID_CREDENTIALS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean updateCredential(RealmModel realm, Map<String, String> configuration, String username, String password) throws AuthenticationProviderException {
|
|
||||||
log.info("Going to update password for user " + username + " in PropertiesAuthenticationProvider");
|
|
||||||
properties.put(username, password);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,92 +0,0 @@
|
||||||
package org.keycloak.examples.providers.authentication;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
|
||||||
import org.keycloak.Config;
|
|
||||||
import org.keycloak.authentication.AuthenticationProvider;
|
|
||||||
import org.keycloak.authentication.AuthenticationProviderFactory;
|
|
||||||
import org.keycloak.models.KeycloakSession;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
|
||||||
*/
|
|
||||||
public class PropertiesAuthenticationProviderFactory implements AuthenticationProviderFactory {
|
|
||||||
|
|
||||||
private static final Logger log = Logger.getLogger(PropertiesAuthenticationProviderFactory.class);
|
|
||||||
|
|
||||||
private Properties properties;
|
|
||||||
private String propsFileLocation;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AuthenticationProvider create(KeycloakSession session) {
|
|
||||||
return new PropertiesAuthenticationProvider(properties);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(Config.Scope config) {
|
|
||||||
this.propsFileLocation = config.get("propertiesFileLocation");
|
|
||||||
|
|
||||||
InputStream propertiesStream = null;
|
|
||||||
this.properties = new Properties();
|
|
||||||
try {
|
|
||||||
if (propsFileLocation == null) {
|
|
||||||
log.info("propertiesFileLocation not configured. Using default users.properties file from classpath.");
|
|
||||||
log.warn("Password updates won't be persisted!");
|
|
||||||
propertiesStream = getClass().getClassLoader().getResourceAsStream("users.properties");
|
|
||||||
} else {
|
|
||||||
log.info("Using properties file from location: " + propsFileLocation);
|
|
||||||
propertiesStream = new FileInputStream(propsFileLocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.properties.load(propertiesStream);
|
|
||||||
} catch (IOException ioException) {
|
|
||||||
throw new RuntimeException(ioException);
|
|
||||||
} finally {
|
|
||||||
if (propertiesStream != null) {
|
|
||||||
try {
|
|
||||||
propertiesStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("Error when closing InputStream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
// Update properties file now, just in case that we are using custom location from filesystem
|
|
||||||
if (propsFileLocation != null) {
|
|
||||||
storePasswords();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void storePasswords() {
|
|
||||||
log.info("Going to store passwords back to file: " + propsFileLocation);
|
|
||||||
OutputStream propertiesStream = null;
|
|
||||||
try {
|
|
||||||
OutputStream stream = new FileOutputStream(propsFileLocation);
|
|
||||||
this.properties.store(stream, "User passwords");
|
|
||||||
} catch (IOException ioException) {
|
|
||||||
throw new RuntimeException(ioException);
|
|
||||||
} finally {
|
|
||||||
if (propertiesStream != null) {
|
|
||||||
try {
|
|
||||||
propertiesStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("Error when closing InputStream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getId() {
|
|
||||||
return "properties";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
org.keycloak.examples.providers.authentication.PropertiesAuthenticationProviderFactory
|
|
|
@ -1,2 +0,0 @@
|
||||||
joe=password1
|
|
||||||
james=password2
|
|
Loading…
Reference in a new issue