Fork me on GitHub

CouchDB (v4.4)

pac4j allows you to validate username/password and create, update and delete users on a CouchDB database.

1) Dependency

You need to use the following module: pac4j-couch.

Example (Maven dependency):

<dependency>
    <groupId>org.pac4j</groupId>
    <artifactId>pac4j-couch</artifactId>
    <version>${pac4j.version}</version>
</dependency>

2) CouchProfileService

The CouchProfileService allows you to:

It works with a CouchProfile.

It is built from a org.ektorp.CouchDbConnector.

Example:

HttpClient httpClient = new StdHttpClient.Builder().url(couchDbUrl).build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector couchDbConnector = dbInstance.createConnector("users", true);
CouchProfileService couchProfileService = new CouchProfileService(couchDbConnector);

The choice of the database name is irrelevant to CouchProfileService. The database containing the users must contain the following design document:

{
	"_id": "_design/pac4j",
	"language": "javascript",
	"views": {
		"by_username": {
			"map": "function(doc) {if (doc.username) emit(doc.username, doc);}"
		},
		"by_linkedid": {
			"map": "function(doc) {if (doc.linkedid) emit(doc.linkedid, doc);}"
		}
	}
}

The id, username and password attribute names can be changed using the setIdAttribute, setUsernameAttribute and setPasswordAttribute methods. By default, the id attribute is CouchDB’s _id attribute. If you change the username or linkedid attribute, please change the design document accordingly. You can also get/set the ObjectMapper used to serialize the JSON data from CouchDB with getObjectMapper() and setObjectMapper(), the default one is simply new ObjectMapper().

The attributes of the user profile can be managed in the CouchDB collection in two ways:

This CouchProfileService supports the use of a specific PasswordEncoder to encode the passwords in the CouchDB database.

Starting with v3.9.0 in the 3.x stream, v4.2.0 in the 4.x stream and v5.0, the serializedprofile is written in JSON instead of using the Java serialization.